Repository navigation
fix: give repeated headings unique ids - #2609
Conversation
Pages with the same heading twice (like the two Example sections on non_json_data) rendered the same id, so clicking the second one jumped to the first and both got highlighted. Repeated headings now get -1, -2 ... ids, the first one keeps its id so existing links still work. Fixes json-schema-org#2343
built with Refined Cloudflare Pages Action⚡ Cloudflare Pages Deployment
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2609 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 31 31
Lines 737 737
Branches 229 229
=========================================
Hits 737 737 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Hi @drishtyraghav0001 👋 Nice work on #2343! The way you handled the duplicate IDs is really clean. I was checking the red Codecov check. Your actual code is 100% covered, but it looks like Do you think mocking those extra components or testing with a small custom wrapper would keep them out and make the check green? What do you think? |
… coverage Render the repeated-heading tests with markdown-to-jsx and the Headline components instead of mounting StyledMarkdown and TableOfContentMarkdown, which brought StyledMarkdownBlock, TabsGroup and the rest of the TOC into coverage without tests. The tab group case is checked on dedupeHeadingIds directly, and the TOC fragment stripping moves to lib/dedupeHeadingIds as stripHeadingFragments. Signed-off-by: Drishty Raghav <273601396+drishtyraghav0001@users.noreply.github.com>
|
thanks @kunalverma2512, good catch! switched the tests to a small wrapper so they don't pull in StyledMarkdownBlock, coverage is back to 100% in 03a934a. @jagpreetrahi could you take a look when you get a chance? |
vtushar06
left a comment
There was a problem hiding this comment.
LGTM @drishtyraghav0001
What kind of change does this PR introduce?
Bugfix
Issue Number:
Screenshots/videos:
n/a, ids only. on the built non_json_data page the two Example headings are now
#exampleand#example-1, clicking the second one goes to the second one and only that one gets the ¶If relevant, did you update the documentation?
no
Summary
when a page has the same heading twice they both got the same id, so the second Example on non_json_data jumped back to the first one and both lit up. now repeated headings get
-1,-2... ids like GitHub does. the first one keeps its id so existing links don't move.it's done in a small
lib/dedupeHeadingIds.tsthat runs on the markdown before rendering and adds a[#id]fragment to the repeats, so the headings and the table of contents pick up the same id. it reusesslugifyMarkdownHeadlinefor the base slug, so it doesn't touch that file and doesn't clash with #2579. it skips code blocks, headings that already have a[#id], h5/h6, and headings that slug to nothing. the TOC also stops showing the raw[#id]text now.compared every heading id and TOC link in the static build before and after. only 5 ids changed, all of them the second copy of a repeated heading (non_json_data
example, glossarysee-also, and 3 on the lexical/dynamic scopes blog post). no visible text changed on any page and nothing in the repo linked to those.added
cypress/components/dedupeHeadingIds.cy.tsx(18 tests, 14 of them fail on main). lint, typecheck, format check, build and test:coverage:all pass locally.Does this PR introduce a breaking change?
no
Checklist