Fix heading auto-link hijacking nested link labels (#668) - #949
Merged
xoofx merged 2 commits intoSep 19, 2026
Merged
Conversation
dualfroz
force-pushed
the
dualfroz/fix-autolink-autoidentifier-668
branch
from
September 5, 2026 23:19
32c14bb to
67a2e49
Compare
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.
Problem
#668
When both the
AutoLinksandAutoIdentifiersextensions are enabled, a Markdown link whose labelcontains a nested
[...]fragment that happens to match the text of an existing heading gets parsedincorrectly. Example:
Header
Testing [Header]
Testing [test]
Actual (on
upstream/main, commitb07b98e1):Root cause
AutoIdentifierExtension(src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs), whenits default
AutoLinkoption is enabled (it is on by default viaAutoIdentifierOptions.Default),registers a
HeadingLinkReferenceDefinitionat the document level for every heading, keyed by theheading's raw text. This turns any occurrence of
[HeadingText]in the document into an implicitshortcut reference link to that heading, without the author ever writing a
[HeadingText]: urlreference definition.
Markdig's core link parser (
src/Markdig/Parsers/Inlines/LinkInlineParser.cs) resolves brackets usingthe CommonMark "nearest active opener wins" algorithm: when it hits the
]right afterHeader, itlooks for a link reference definition matching
Headerfirst. Because the heading### Headerimplicitly registered one, the inner
[Header]resolves into a link before the outer bracket ever getsa chance to see its own
](https://www.bing.com). Per CommonMark's "links cannot contain links" rule,successfully resolving the inner link also deactivates the outer
[bracket, so the outer bracket canno longer become a link and is emitted as literal text instead.
This exact resolution order is correct and spec-compliant for a reference definition the author defined
on purpose (verified: a plain CommonMark document containing an explicit
[Header]: /somewheredefinition produces the identical "broken-looking" nesting, with no extensions involved at all). The bug
is that
AutoIdentifierExtension's heading auto-link references are created implicitly, for everyheading, with no way for the author to know that a particular word will silently start behaving as a
link reference elsewhere in the document — including inside brackets that were never meant to be link
labels for the heading, such as the label of a larger, still-open link.
Fix
Added an internal extensibility point,
LinkReferenceDefinition.AllowResolutionInsideOpenLink(defaulttrue), and overrode it tofalseonHeadingLinkReferenceDefinition(
src/Markdig/Extensions/AutoIdentifiers/HeadingLinkReferenceDefinition.cs). InLinkInlineParser.ProcessLinkReference, before resolving a shortcut/collapsed/full reference link, theparser now checks whether the reference definition disallows resolution while nested inside another
still-open, active link/image bracket (
LinkInlineParser.HasActiveAncestorLink). If so, resolution isskipped and the normal CommonMark fallback (treat the inner bracket as literal text, let the outer
bracket complete) takes over, exactly as it already does for any word that doesn't match a heading.
This is scoped narrowly to implicitly generated heading references:
[label]: url) are completely unaffected -AllowResolutionInsideOpenLinkdefaults totrue, preserving existing CommonMark-compliant behaviorfor nested brackets with explicit reference definitions.
See [Header] for details.,continue to work exactly as before.
LinkReferenceDefinitionare unaffected.With the fix (full suite)
(The single skip,
ListUnorderedLooseTop, is pre-existing and unrelated to this change - present beforethis fix as well.)