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 under Dialect::Pandoc is shape-only: any non-empty link text or label resolves regardless of refdef presence, matching the historical legacy reference_resolves-returns-true behavior. (Pandoc emits LINK nodes for unresolved shortcut/collapsed/ full-reference shapes so downstream features — linter, LSP, formatter — have a typed wrapper to walk. Refdef-aware resolution under Pandoc is bug #1/#2 territory and is a parser-linter-LSP cross-cut deferred to a future workstream.)