Skip to content

fix: key section folds by unique heading id instead of heading text#219

Open
PathGao wants to merge 1 commit into
alecdotdev:masterfrom
PathGao:fix/fold-key-unique-ids
Open

fix: key section folds by unique heading id instead of heading text#219
PathGao wants to merge 1 commit into
alecdotdev:masterfrom
PathGao:fix/fold-key-unique-ids

Conversation

@PathGao

@PathGao PathGao commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Bug

Fold state (collapsedHeaders) is keyed by h.id || textContent. comrak's header_ids extension puts the deduplicated id (title, title-1, …) on an empty inner <a class="anchor">, not on the heading element itself — so h.id is always empty and every consumer silently falls back to heading text. Two consequences:

  1. Duplicate titles share one fold state. Collapse one "## Notes" section, and after the next re-render every section titled "Notes" is collapsed.
  2. The ToC fold button and the preview disagree on keys. The ToC keys by anchor id (notes), the preview restore keys by text (Notes) — they only coincide when a title equals its own slug (single lowercase word), so folding from the ToC usually doesn't collapse the section body at all.

Fix

Promote the anchor's id onto the heading element (and remove it from the anchor, keeping document ids unique) in processMarkdownHtml's heading pass. All four fold consumers — preview chevron, render restore, ToC fold button, and toggleFold's [id=…].foldable-header lookup — already prefer h.id and now agree on one unique key, with no change to their logic. In-document #anchor links still resolve (the id moves from the empty inline anchor to the heading that contains it, same scroll target).

Verification

  • npm run check: 0 errors / 0 warnings
  • All script tests pass (59/59), including a new scripts/foldKeys.test.ts covering the id promotion and the key precedence at each consumer

🤖 Generated with Claude Code

comrak puts the deduplicated heading id on an empty inner anchor, so
h.id was always empty and fold state fell back to heading-text keys:
duplicate titles shared one fold state after any re-render, and the
ToC fold button's id-based key never matched the preview's text key
(so folding from the ToC didn't collapse the section unless the title
happened to equal its own slug). Promoting the anchor id onto the
heading unifies all four consumers — preview chevron, render restore,
ToC fold button, and toggleFold's id lookup — on one unique key,
with no change to any of their logic.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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