Skip to content

fix: support custom anchor IDs in headings ({#my-anchor} syntax)#770

Open
mrueg wants to merge 1 commit into
kovetskiy:masterfrom
mrueg:fix/heading-custom-anchor-ids
Open

fix: support custom anchor IDs in headings ({#my-anchor} syntax)#770
mrueg wants to merge 1 commit into
kovetskiy:masterfrom
mrueg:fix/heading-custom-anchor-ids

Conversation

@mrueg
Copy link
Copy Markdown
Collaborator

@mrueg mrueg commented Apr 9, 2026

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread markdown/markdown.go
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
parser.WithAttribute(),
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
parser.WithAttribute(),

Copilot uses AI. Check for mistakes.
Comment thread markdown/markdown.go
Comment on lines 109 to 113
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
parser.WithAttribute(),
),
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@mrueg mrueg force-pushed the fix/heading-custom-anchor-ids branch from d9bd171 to bd007f5 Compare April 9, 2026 13:20
@mrueg mrueg requested review from Copilot April 9, 2026 15:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
<p><a href="#custom-id">link to custom</a></p>

Copilot uses AI. Check for mistakes.
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>
@mrueg mrueg force-pushed the fix/heading-custom-anchor-ids branch from bd007f5 to c43dd5c Compare April 9, 2026 20:48
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.

support for header custom anchor ids

2 participants