fix: support custom anchor IDs in headings ({#my-anchor} syntax)#770
fix: support custom anchor IDs in headings ({#my-anchor} syntax)#770mrueg wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the Goldmark markdown compiler configuration to correctly parse extended Markdown heading attribute syntax (e.g., {#custom-id}) so custom heading anchor IDs are applied as HTML attributes instead of being rendered as literal heading text, addressing issue #519.
Changes:
- Enable Goldmark attribute parsing by adding
parser.WithAttribute()to the parser options. - Allow headings like
# Title {#id}to render as<h1 id="id">Title</h1>rather than including{#id}in the visible text.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ), | ||
| goldmark.WithParserOptions( | ||
| parser.WithAutoHeadingID(), | ||
| parser.WithAttribute(), |
There was a problem hiding this comment.
parser.WithAttribute() enables parsing (and, via the existing RenderAttributes calls in multiple renderers, output) of inline attribute lists for more than just headings (e.g., paragraphs, blockquotes, lists, links). If the intent is only custom heading IDs, consider either limiting which node types render attributes (e.g., keep attributes for headings only) or documenting/accepting the expanded supported syntax as a behavior change.
| parser.WithAttribute(), |
| ), | ||
| goldmark.WithParserOptions( | ||
| parser.WithAutoHeadingID(), | ||
| parser.WithAttribute(), | ||
| ), |
There was a problem hiding this comment.
Add a regression test covering heading attribute IDs (e.g., # Title {#custom-id}) to ensure the {#...} is not rendered as literal text and that the generated <h*> uses exactly the provided id (and is not overridden/modified by WithAutoHeadingID). The current golden tests under testdata/*.md don't include this syntax, so this behavior isn't protected.
d9bd171 to
bd007f5
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -0,0 +1,4 @@ | |||
| <h2 id="another-id">Another Heading</h2> | |||
| <h3 id="No-Custom-ID">No Custom ID</h3> | |||
| <p><a href="#custom-id">link to custom</a></p> | |||
There was a problem hiding this comment.
In the DropFirstH1 expected output, the document contains a link to #custom-id but the <h1 id="custom-id">... anchor is dropped, leaving a dead in-page link. Consider adjusting the fixture markdown (or the DropFirstH1 expectations) so DropFirstH1 variants don't assert broken anchors, while still testing custom heading IDs.
| <p><a href="#custom-id">link to custom</a></p> |
Add parser.WithAttribute() to the goldmark parser options so that
extended Markdown heading attributes like {#custom-id} are parsed
correctly instead of appearing as literal text in the heading.
Before: # My Heading {#custom-id} → <h1>My Heading {#custom-id}</h1>
After: # My Heading {#custom-id} → <h1 id="custom-id">My Heading</h1>
Fixes kovetskiy#519
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
bd007f5 to
c43dd5c
Compare
Add parser.WithAttribute() to the goldmark parser options so that extended Markdown heading attributes like {#custom-id} are parsed correctly instead of appearing as literal text in the heading.
Before: # My Heading {#custom-id} →
My Heading {#custom-id}
After: # My Heading {#custom-id} →
My Heading
Fixes #519