Commit f978a9f
fix/site: Fix broken tables of contents (#1859)
Linear [FE-499: Fix doc site
issues](https://linear.app/sourcegraph/issue/FE-499/fix-doc-site-issues)
## 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) |
| --- | --- |
| <img
src="https://ampcode.com/user-content/artifacts/ee9ba290aa34b608e52fa272b2abfdc0cf6484f93120fa42b2c1c31441aab30f-file.png"
width="400"> | <img
src="https://ampcode.com/user-content/artifacts/96b96b558b53a28b6f65f786485818bb28dce5812a8c32995a1725242f1ad6dd-file.png"
width="400"> |
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.
Co-authored-by: Amp <amp@ampcode.com>1 parent e5c8190 commit f978a9f
1 file changed
Lines changed: 30 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
12 | 41 | | |
13 | 42 | | |
14 | 43 | | |
| |||
28 | 57 | | |
29 | 58 | | |
30 | 59 | | |
31 | | - | |
32 | 60 | | |
33 | 61 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | 62 | | |
41 | | - | |
| 63 | + | |
42 | 64 | | |
43 | 65 | | |
44 | 66 | | |
| |||
0 commit comments