Skip to content

fix(core): render heading ids in Markdown so Outline hash links scroll (#4173) - #4765

Merged
cixzhang merged 2 commits into
meta:mainfrom
jiunshinn:fix/4173-markdown-heading-ids
Aug 19, 2026
Merged

cixzhang merged 2 commits into
meta:mainfrom
jiunshinn:fix/4173-markdown-heading-ids

Conversation

@jiunshinn

Copy link
Copy Markdown
Contributor

Fixes #4173. Tracked under "Known accessibility bugs" in the accessibility program tracker #4475.

Root cause

Outline documents a contract that Markdown never fulfilled. The items prop docs state the id "should match the target heading DOM id", and useOutlineFromMarkdown exists specifically to derive that id list from markdown source. parseOutlineFromMarkdown slugifies heading text (inlineText -> slugify -> uniqueSlug), but the Markdown renderer never wrote any id attribute onto the h1-h6 elements it produces, and the components.heading override only received {level, children}, so there was no supported way to inject one. Result: clicking an outline item changes location.hash, document.getElementById(id) returns null, and the scroll silently no-ops, exactly as the reporter's repro shows.

What changed

  • Single source of truth for slugs. inlineText, slugify, and uniqueSlug now live in packages/core/src/Markdown/parser.ts (exported, internal to the package). Outline/parseOutlineFromMarkdown.ts deletes its private copies and imports the shared helpers. Rendered ids and outline ids can no longer drift because they are computed by the same functions.
  • Markdown renders id on h1-h6. A headingIdMap memo walks the parsed top-level blocks with one shared duplicate-numbering sequence, mirroring parseOutlineFromMarkdown's traversal exactly (top-level blocks only; headings inside blockquotes and list items are skipped, because the outline never lists them and letting them consume slugs would shift the duplicate numbering and point outline links at the wrong heading).
  • components.heading override receives id?: string. Backward-compatible type extension; existing overrides that ignore it keep compiling and rendering unchanged. Custom heading components can now attach the id themselves.
  • Docs. Markdown.doc.mjs gains a usage note (en/dense) about pairing with Outline; Outline.doc.mjs's "Generate items from markdown" example notes that Markdown renders matching ids.
  • Changeset. Patch bump per the pre-1.0 patch-only policy.

Hook-placement pitfall

The headingIdMap memo sits with the other hooks, directly after the blocks memo and well above the display === 'inline' early return, with a NOTE comment explaining why it must stay there. A revision of the prior attempt was tripped up by a Rules of Hooks violation here; this placement is covered by the existing inline-display tests plus the new suite.

The invariant, as a test

Markdown.test.tsx gains a heading ids suite (5 tests):

  1. headings render generated ids (overview, installation, the issue's exact repro shape)
  2. duplicate headings disambiguate (setup, setup-1, setup-2)
  3. parity test: for one gnarly source (bold/italic formatting, duplicates, punctuation-only heading hitting the section fallback, a code-fence decoy, inline code), every id from parseOutlineFromMarkdown resolves via querySelector to a rendered h1-h6 whose text matches the item label
  4. a custom components.heading receives the generated ids, including the deduped one
  5. headings nested in blockquotes get no id and do not consume slug numbering (locks the traversal-parity rule)

Full run: packages/core/src/Markdown + packages/core/src/Outline = 7 files, 313 tests, all passing, including parser.perf.test.ts (slug helpers are appended utilities; the parse hot path is untouched).

Prior attempt

#4191 (closed unmerged) had the right blueprint and this PR credits its direction. Differences here: the slug helpers are truly deduplicated (Outline imports the parser's functions instead of keeping a second copy), the parity invariant is enforced by a test that runs both code paths against the same source, nested-heading behavior is explicit and tested rather than incidental, and the components.heading id prop is documented in the type plus the doc files.

Reviewer flags

  • Nested headings intentionally get no id. Matching parseOutlineFromMarkdown exactly was prioritized over decorating every heading. If ids on nested headings are wanted, both traversals should be extended together in a follow-up so numbering stays in sync.
  • Slug helpers are exported from parser.ts but not re-exported from Markdown/index.ts, keeping the public API surface unchanged. Happy to expose them publicly if maintainers prefer.
  • id is applied to the default heading element as a plain JSX attribute ahead of the mergeProps(themeProps(...), stylex.props(...)) spread; neither source emits an id, so there is no collision today.
  • During streaming, the memo recomputes per settled-block update; cost is O(headings) per parse, negligible next to the parse itself.

@vercel

vercel Bot commented Aug 6, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview Aug 19, 2026 10:50am

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 6, 2026
@jiunshinn
jiunshinn marked this pull request as ready for review August 6, 2026 18:02

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this!

@github-actions github-actions Bot added the community Authored by a community contributor (not on the eng/design team) label Aug 19, 2026
@cixzhang
cixzhang enabled auto-merge (squash) August 19, 2026 10:46
@cixzhang
cixzhang merged commit 6bd4a75 into meta:main Aug 19, 2026
19 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

Markdown (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 2970 -
Complexity N/A Very High (205) -
Outline (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 671 -
Complexity N/A High (28) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.8KB 1.2KB

Accessibility Audit

Status: 1 accessibility violation(s) found — 1 serious.

Outline - 1 issue(s)
  • 🟠 serious: Ensure elements that have scrollable content are accessible by keyboard in Safari
    • Rule: scrollable-region-focusable · Affects 1/10 stories · Learn more
    • WCAG: 2.1.1, 2.1.3 (Level A)

Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

github-actions Bot added a commit that referenced this pull request Aug 19, 2026
@github-actions
github-actions Bot deleted the fix/4173-markdown-heading-ids branch August 20, 2026 06:45

This branch was successfully deployed

1 active deployment
Preview — a1e38e4a Deployed Aug 19, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Markdown-rendered headings have no id — Outline links never scroll to their target

2 participants