Skip to main content

Module refdef_map

Module refdef_map 

Source
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::parse call from the original input string and shared (via Arc) 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 for Dialect::CommonMark; callers should pass an empty (or None) map for other dialects.
normalize_label
Normalise a refdef label per CommonMark §4.7.

Type Aliases§

RefdefMap
Set of normalised refdef labels collected from the document. Wrapped in Arc so the (immutable) set can be cheaply cloned into every inline parsing call.