Repository navigation
fix(markdown): add anchor ids to generated headings - #1765
Open
officialbidisha wants to merge 2 commits into
Open
officialbidisha wants to merge 2 commits into
officialbidisha wants to merge 2 commits into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1495.
Problem: marked's default heading renderer doesn't add an id attribute to generated h1-h6 tags. Since the "Additional documentation" feature (--includes/summary.json) relies on anchor links (e.g. a hand-written table of contents linking to #some-heading) to jump to headings inside a rendered markdown page, those links silently do nothing since there's no matching id to scroll to.
Fix: MarkdownEngine already customizes the marked renderer for code/table/image. This adds a heading override that slugifies the heading text into a GitHub-style anchor (lowercase, punctuation stripped, spaces to hyphens); disambiguates repeated headings within the same document with a -1, -2, ... suffix, matching GitHub's own anchor behavior; and resets the seen-slugs map on every renderMarkdown call, so ids don't leak between separately rendered documents.
Testing: Added unit tests in test/src/engines/markdown.engine.spec.ts covering id generation, duplicate-heading disambiguation, and no state leaking across renders. Manually built docs for a fixture project with --includes pointing at test/fixtures/todomvc-ng2/additional-doc and confirmed the generated additional-documentation/*.html pages now contain e.g.
Introduction
, so a link like href="#introduction" now resolves correctly. npm run build succeeds and the existing code/table/image renderer overrides are untouched.