Skip to main content

process_brackets

Function process_brackets 

Source
pub fn process_brackets(
    events: &mut [IrEvent],
    text: &str,
    refdefs: Option<&RefdefMap>,
    dialect: Dialect,
)
Expand description

Resolve [/![/] markers into link/image nodes per CommonMark §6.3 (with Pandoc-aware variations under Dialect::Pandoc).

Walks the IR forward looking for ] markers. For each one, finds the nearest active matching [/![ and tries to resolve the bracket pair as a link or image. Resolution is tried in spec order:

  1. Inline link / image: [text](dest) or [text](dest "title").
  2. Full reference: [text][label], where label is in refdefs.
  3. Collapsed reference: [text][], where text (normalised) is in refdefs.
  4. Shortcut reference: [text] not followed by ( or [, where text (normalised) is in refdefs.

On a match, the opener gets a BracketResolution and the closer is flagged matched. Under Dialect::CommonMark, all earlier active link openers are deactivated to implement the §6.3 “links may not contain other links” rule (image brackets do not deactivate earlier link openers — only links do). Under Dialect::Pandoc, the deactivate-pass is skipped: pandoc-native is outer-wins for nested links (the inner [inner](u2) of [link [inner](u2)](u1) is literal text inside the outer link), and the dispatcher enforces this via a suppress_inner_links flag during LINK-text recursion. So under Pandoc the IR can leave both outer and inner resolved and trust the dispatcher to suppress inner LINK emission.

On a miss the bracket pair stays opaque-as-literal and the closer is dropped from the bracket stack so the next ] can re-pair.

Reference-form resolution consults the refdef map under both dialects (CommonMark §6.3 and Pandoc-markdown agree on the document-scoped lookup rule). Under Pandoc, when a bracket-shape pattern ([text][label], [text][], [text]) doesn’t resolve to a refdef, the opener is tagged with unresolved_ref = Some(...) and the closer’s matched is set to true so that build_bracket_plan emits a BracketDispo::UnresolvedReference keyed at the opener. Emission then wraps [start, end) in an UNRESOLVED_REFERENCE node — distinct from LINK — so downstream tools (linter, LSP) can attach behavior to the bracket-shape pattern without the parser having to lie about resolution.

Under CommonMark, no unresolved_ref is recorded; the no-resolution fall-through behaves as today (opener deactivated, brackets emit as literal text).