Skip to content

fix/site: Fix broken tables of contents - #1859

Open
marcleblanc2 wants to merge 1 commit into
mainfrom
fix-toc-code-fence-stripping
Open

fix/site: Fix broken tables of contents#1859
marcleblanc2 wants to merge 1 commit into
mainfrom
fix-toc-code-fence-stripping

Conversation

@marcleblanc2

@marcleblanc2 marcleblanc2 commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Problem

The right-hand TOC (headings computed field in contentlayer.config.ts) strips fenced code blocks with the non-greedy regex /```[\s\S]*?```/g, then treats any remaining # line as a heading.

Any inline triple-backtick run in prose is taken as a fence opener and flips every later fence pairing. docs/batch-changes/batch-spec-yaml-reference.mdx has two (`"true``` on line 376, `"*``` on line 709), so from there on the "inside/outside a fence" state is inverted: YAML # comment lines leak into the TOC as headings whose anchors don't exist, and real headings are dropped.

Live repro: https://sourcegraph.com/docs/batch-changes/batch-spec-yaml-reference has a TOC entry linking to #do-not-meddle-in-the-affairs-of-wizards-for-they-are-subtle-and-quick-to-anger (line 572 of the MDX, a YAML comment inside a fence). No element with that id exists. This page alone accounted for 14 of the broken anchors found by lychee in the investigation behind #1858.

Before / after

TOC on /batch-changes/batch-spec-yaml-reference, local next dev, 1600px viewport.

Before (main) After (this PR)

Before: 12 YAML comments (if: is true, step always executes., Mount a Python script and run the script, Do not meddle in the affairs of wizards…) render as TOC entries with dead anchors, and every changesetTemplate.* heading except .fork is missing. After: the comments are gone and steps.mount, importChangesets*, changesetTemplate*, Publishing only specific changesets are back.

Fix

Walk the body line by line: a fence opens on a line starting with 3+ backticks or tildes and closes on a line of the same character at least as long, matching how the MDX renderer treats fences. Also handles the four-backtick fences in `cody/troubleshooting.mdx` and `code-navigation/writing-an-indexer.mdx` that contain literal ``` ```` text.

Verification

  • Compared old vs new heading output across every .mdx under docs/: only batch-spec-yaml-reference.mdx changes — 12 bogus comment entries removed, 13 real headings restored.
  • npx contentlayer build: generated Post for that page has 66 headings, 0 bogus, all changesetTemplate.* ids present.
  • npx tsc --noEmit clean, next lint clean.

Follow-up from the link-check work in #1858.

The headings computed field removed fenced code blocks with the
non-greedy regex /```[\s\S]*?```/g. Any inline triple-backtick run in
prose (e.g. `"true``` on batch-spec-yaml-reference.mdx line 376) was
taken as a fence opener, flipping every later fence pairing. YAML
`# comment` lines inside fences then leaked into the TOC as headings
with anchors that don't exist, while real headings were dropped.

Walk the body line by line instead: a fence opens on a line starting
with 3+ backticks or tildes and closes on a line of the same character
at least as long, matching how the MDX renderer treats fences.

On /batch-changes/batch-spec-yaml-reference this removes 12 bogus TOC
entries and restores 13 real ones. No other page's headings change.

Amp-Thread-ID: https://ampcode.com/threads/T-01a07599-bfc6-773f-abc1-7e0cd96db912
Co-authored-by: Amp <amp@ampcode.com>
@vercel

vercel Bot commented Sep 6, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
sourcegraph-docs Ready Ready Preview Sep 6, 2026 7:30am UTC

Request Review

@marcleblanc2 marcleblanc2 changed the title fix/site: Strip code fences line-by-line when building the TOC fix/site: Fix broken tables of contents Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant