Fix heading auto-link hijacking nested link labels (#668)

This commit is contained in:
dualfroz committed 2026-09-04 03:24:13 +02:00
1 parent 10ebd55fd4
commit cf9456728c
4 files changed
+74

No files matched your search

@@ -150,6 +150,14 @@ public class LinkInlineParser : InlineParser
return false;
}
// Implicitly generated reference definitions (e.g. automatic heading anchors from the auto-identifier
// extension) must not hijack a bracket pair nested inside another still-open link, otherwise they would
// silently break the outer, explicitly authored link (see https://github.com/xoofx/markdig/issues/668).
if (!linkRef.AllowResolutionInsideOpenLink && HasActiveAncestorLink(parent))
{
return false;
}
Inline? link = null;
// Try to use a callback directly defined on the LinkReferenceDefinition
if (linkRef.CreateLinkInline != null)
@@ -430,6 +438,24 @@ public class LinkInlineParser : InlineParser
}
}
/// <summary>
/// Determines whether the given link delimiter is nested inside another link/image delimiter that is
/// still active, i.e. it may still be resolved into a real link/image once fully parsed.
/// </summary>
private static bool HasActiveAncestorLink(LinkDelimiterInline delimiter)
{
var inline = delimiter.Parent;
while (inline != null)
{
if (inline is LinkDelimiterInline { IsActive: true })
{
return true;
}
inline = inline.Parent;
}
return false;
}
private static void MarkParentAsInactive(Inline? inline)
{
while (inline != null)