Expand description
Document-level link reference definition map for CommonMark inline parsing.
CommonMark §6.3 says reference links are valid only when the label
matches a definition that appears anywhere in the document, including
after the use site. The block-level parser already recognises
[label]: dest lines and emits them as separate blocks, but inline
parsing has historically treated every [bracket pair] as opaque on
shape alone — without checking whether the label resolves.
The fix is a single forward scan over the input before inline
parsing runs, collecting every refdef label into a RefdefMap.
The IR’s bracket resolution pass consults this map to decide whether
a [...] (or [...][...]) opens a link or falls through to literal
text.
§Scope
-
The set is computed once per
Parser::parsecall from the original input string and shared (viaArc) with every inline parsing invocation that needs it. Inline fragments (e.g. heading text, paragraph text, table cell text) do not contain refdef definitions themselves, so a fragment-level scan is insufficient. -
Labels are normalised per CommonMark §4.7: case-folded, leading and trailing whitespace stripped, internal whitespace runs collapsed to a single space. The same normalisation applies on the lookup side in the bracket resolution pass.
-
The scan does not attempt to detect refdefs inside code fences or raw HTML blocks; it accepts a small over-approximation in exchange for being a context-free linear walk. A bracket label that happens to spell a defined refdef inside a fenced code block would still resolve correctly under emission because emission walks the CST, which already excludes the fenced region. The over-approximation only matters if a bogus refdef-shaped line outside a code block would shadow real text — that case is also wrong under CommonMark semantics, so the approximation is fine.
Functions§
- collect_
refdef_ labels - Walk the input string once and collect all reference definitions into
a
RefdefMap. Only used forDialect::CommonMark; callers should pass an empty (orNone) map for other dialects. - normalize_
label - Normalise a refdef label per CommonMark §4.7.
Type Aliases§
- Refdef
Map - Set of normalised refdef labels collected from the document. Wrapped
in
Arcso the (immutable) set can be cheaply cloned into every inline parsing call.