Skip to content

fix(markdown): add anchor ids to generated headings - #1765

Open
officialbidisha wants to merge 2 commits into
compodoc:developfrom
officialbidisha:fix/markdown-heading-anchor-ids
Open

officialbidisha wants to merge 2 commits into
compodoc:developfrom
officialbidisha:fix/markdown-heading-anchor-ids

Conversation

@officialbidisha

Copy link
Copy Markdown

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.

This branch has not been deployed

No deployments
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.

[FEATURE] Adding ID to markdown headers

1 participant