Skip to main content

panache_parser/parser/inlines/
core.rs

1//! Inline emission walk.
2//!
3//! Consumes the IR plans built by [`super::inline_ir::build_full_plans`]
4//! (emphasis pairings, bracket resolutions, standalone Pandoc constructs)
5//! and emits the inline CST tokens / nodes in source order. Resolution
6//! decisions for emphasis, brackets, and standalone Pandoc constructs
7//! are entirely IR-driven for both dialects; the dispatcher's
8//! `try_parse_*` recognizers are still called to *parse* a matched byte
9//! range into a CST subtree, but "what is this byte range?" is answered
10//! exclusively by the IR.
11
12use crate::options::{Dialect, ParserOptions};
13use crate::syntax::SyntaxKind;
14use rowan::GreenNodeBuilder;
15
16use super::inline_ir::{
17    BracketPlan, ConstructDispo, ConstructPlan, DelimChar, EmphasisKind, EmphasisPlan,
18};
19
20// Import inline element parsers from sibling modules
21use super::bookdown::{
22    try_parse_bookdown_definition, try_parse_bookdown_reference, try_parse_bookdown_text_reference,
23};
24use super::bracketed_spans::{emit_bracketed_span, try_parse_bracketed_span};
25use super::citations::{
26    emit_bare_citation, emit_bracketed_citation, try_parse_bare_citation,
27    try_parse_bracketed_citation,
28};
29use super::code_spans::{emit_code_span, try_parse_code_span};
30use super::emoji::{emit_emoji, try_parse_emoji};
31use super::escapes::{EscapeType, emit_escape, try_parse_escape};
32use super::inline_executable::{emit_inline_executable, try_parse_inline_executable};
33use super::inline_footnotes::{
34    emit_footnote_reference, emit_inline_footnote, try_parse_footnote_reference,
35    try_parse_inline_footnote,
36};
37use super::inline_html::{emit_inline_html, try_parse_inline_html};
38use super::latex::{parse_latex_command, try_parse_latex_command};
39use super::links::{
40    LinkScanContext, emit_autolink, emit_bare_uri_link, emit_inline_image, emit_inline_link,
41    emit_reference_image, emit_reference_link, emit_unresolved_reference, try_parse_autolink,
42    try_parse_bare_uri, try_parse_inline_image, try_parse_inline_link, try_parse_reference_image,
43    try_parse_reference_link,
44};
45use super::mark::{emit_mark, try_parse_mark};
46use super::math::{
47    emit_display_math, emit_display_math_environment, emit_double_backslash_display_math,
48    emit_double_backslash_inline_math, emit_gfm_inline_math, emit_inline_math,
49    emit_single_backslash_display_math, emit_single_backslash_inline_math, try_parse_display_math,
50    try_parse_double_backslash_display_math, try_parse_double_backslash_inline_math,
51    try_parse_gfm_inline_math, try_parse_inline_math, try_parse_math_environment,
52    try_parse_single_backslash_display_math, try_parse_single_backslash_inline_math,
53};
54use super::native_spans::{emit_native_span, try_parse_native_span};
55use super::raw_inline::is_raw_inline;
56use super::shortcodes::{emit_shortcode, try_parse_shortcode};
57use super::strikeout::{emit_strikeout, try_parse_strikeout};
58use super::subscript::{emit_subscript, try_parse_subscript};
59use super::superscript::{emit_superscript, try_parse_superscript};
60
61/// Parse inline text into the CST builder.
62///
63/// Top-level entry point for inline parsing. Builds the IR plans
64/// (emphasis pairings, bracket resolutions, standalone Pandoc constructs)
65/// once via [`super::inline_ir::build_full_plans`], then walks the byte
66/// range left-to-right consulting those plans plus the dispatcher's
67/// ordered-try chain for non-IR-resolved constructs (autolinks, code
68/// spans, escapes, math, etc.). Dialect-specific behavior is selected
69/// inside `build_full_plans`.
70///
71/// # Arguments
72/// * `text` - The inline text to parse
73/// * `config` - Configuration for extensions and formatting
74/// * `builder` - The CST builder to emit nodes to
75pub fn parse_inline_text_recursive(
76    builder: &mut GreenNodeBuilder,
77    text: &str,
78    config: &ParserOptions,
79) {
80    log::trace!(
81        "Recursive inline parsing: {:?} ({} bytes)",
82        &text[..text.len().min(40)],
83        text.len()
84    );
85
86    let mask = structural_byte_mask(config);
87    if try_emit_plain_text_fast_path_with_mask(builder, text, &mask) {
88        log::trace!("Recursive inline parsing complete (plain-text fast path)");
89        return;
90    }
91
92    let plans = super::inline_ir::build_full_plans(text, 0, text.len(), config);
93    parse_inline_range_impl(
94        text,
95        0,
96        text.len(),
97        config,
98        builder,
99        false,
100        &plans.emphasis,
101        &plans.brackets,
102        &plans.constructs,
103        false,
104        &mask,
105    );
106
107    log::trace!("Recursive inline parsing complete");
108}
109
110/// Parse inline elements from text content nested inside a link/image/span.
111///
112/// Used for recursive inline parsing of link text, image alt, span content, etc.
113/// Suppresses constructs that would create nested links (CommonMark §6.3 forbids
114/// links inside links), notably extended bare-URI autolinks under GFM.
115///
116/// `suppress_inner_links` should be `true` when the recursion is for a
117/// LINK or REFERENCE-LINK's text, where inner link / reference-link
118/// brackets must emit as literal text (pandoc-native:
119/// `[link [inner](u2)](u1)` → outer `Link` with `Str "[inner](u2)"`).
120/// Image alt text and all non-link contexts pass `false`:
121/// pandoc-native verifies `![alt with [inner](u)](u2)` keeps the inner
122/// `Link`, and bracketed spans / native spans / inline footnotes /
123/// emphasis all allow nested links.
124pub fn parse_inline_text(
125    builder: &mut GreenNodeBuilder,
126    text: &str,
127    config: &ParserOptions,
128    suppress_inner_links: bool,
129) {
130    log::trace!(
131        "Parsing inline text (nested in link): {:?} ({} bytes)",
132        &text[..text.len().min(40)],
133        text.len()
134    );
135
136    let mask = structural_byte_mask(config);
137    if try_emit_plain_text_fast_path_with_mask(builder, text, &mask) {
138        return;
139    }
140
141    let plans = super::inline_ir::build_full_plans(text, 0, text.len(), config);
142    parse_inline_range_impl(
143        text,
144        0,
145        text.len(),
146        config,
147        builder,
148        true,
149        &plans.emphasis,
150        &plans.brackets,
151        &plans.constructs,
152        suppress_inner_links,
153        &mask,
154    );
155}
156
157/// Plain-text fast path for inline ranges with no structural bytes.
158///
159/// Returns `true` if the range was emitted as a single `TEXT` token and
160/// the caller should skip the IR + dispatcher pipeline. Returns `false`
161/// if any structural byte appears (or the range is empty), letting the
162/// caller proceed normally. Empty input returns `false` so the caller's
163/// existing "no events → no output" path is preserved exactly.
164///
165/// The structural byte set is computed from `config.dialect` and
166/// `config.extensions` so prose containing dialect-irrelevant punctuation
167/// (e.g. `-` outside a citation flavor) doesn't unnecessarily disable the
168/// fast path. `\n` and `\r` are always structural — multi-line inline
169/// content must still split into TEXT + NEWLINE tokens like the slow path.
170fn try_emit_plain_text_fast_path_with_mask(
171    builder: &mut GreenNodeBuilder,
172    text: &str,
173    mask: &[bool; 256],
174) -> bool {
175    if text.is_empty() {
176        return false;
177    }
178    for &b in text.as_bytes() {
179        if mask[b as usize] {
180            return false;
181        }
182    }
183    builder.token(SyntaxKind::TEXT.into(), text);
184    true
185}
186
187/// Build a 256-entry byte mask: `mask[b]` is `true` iff byte `b` could
188/// trigger any IR-recognised construct or dispatcher branch under the
189/// current dialect/extensions. Used by the plain-text fast path to scan
190/// inline ranges in a single pass.
191fn structural_byte_mask(config: &ParserOptions) -> [bool; 256] {
192    let mut mask = [false; 256];
193    let exts = &config.extensions;
194    let pandoc = config.dialect == Dialect::Pandoc;
195
196    // Always structural: line breaks (CST splits TEXT/NEWLINE), backslash
197    // (escape / hard break / backslash-math / latex / bookdown ref),
198    // backtick (code span / inline executable), `*`/`_` (emphasis is a
199    // core CommonMark construct, not extension-gated), and `[`/`]` if
200    // any bracket-shaped construct is reachable.
201    mask[b'\n' as usize] = true;
202    mask[b'\r' as usize] = true;
203    mask[b'\\' as usize] = true;
204    mask[b'`' as usize] = true;
205    mask[b'*' as usize] = true;
206    mask[b'_' as usize] = true;
207
208    // Brackets: the IR/dispatcher only acts on `[`/`]` if some
209    // bracket-shaped feature is reachable. `!` is the leading byte of
210    // `![alt]` image brackets — the IR's `BracketPlan` keys image
211    // openers at the `!` position, so the dispatcher must stop here
212    // to consult the plan.
213    if exts.inline_links
214        || exts.reference_links
215        || exts.inline_images
216        || exts.bracketed_spans
217        || exts.footnotes
218        || exts.citations
219    {
220        mask[b'[' as usize] = true;
221        mask[b']' as usize] = true;
222    }
223    if exts.inline_images || exts.reference_links {
224        mask[b'!' as usize] = true;
225    }
226
227    // `<` covers autolinks, raw HTML, and Pandoc native spans.
228    if exts.autolinks || exts.raw_html || exts.native_spans {
229        mask[b'<' as usize] = true;
230    }
231
232    // `^` covers Pandoc inline footnotes (`^[...]`), CM inline footnotes
233    // (when explicitly enabled), and superscript (`^text^`).
234    if exts.inline_footnotes || exts.superscript {
235        mask[b'^' as usize] = true;
236    }
237
238    // `@` and `-` cover Pandoc citation forms (`@cite`, `-@cite`,
239    // `[@cite]`). Under Pandoc dialect, the IR's `ConstructPlan` keys
240    // bare citations at the `@` or `-` position, so the dispatcher
241    // must stop at either to consult the plan. Including `-` is
242    // pessimistic — most prose hyphens won't form `-@` — but missing
243    // it would skip past valid suppress-author citations.
244    if exts.citations || exts.quarto_crossrefs {
245        mask[b'@' as usize] = true;
246        if pandoc {
247            mask[b'-' as usize] = true;
248        }
249    }
250
251    // `$` covers dollar-math and GFM math.
252    if exts.tex_math_dollars || exts.tex_math_gfm {
253        mask[b'$' as usize] = true;
254    }
255
256    // `~` covers subscript and strikeout (both `~text~` and `~~text~~`).
257    if exts.subscript || exts.strikeout {
258        mask[b'~' as usize] = true;
259    }
260
261    if exts.mark {
262        mask[b'=' as usize] = true;
263    }
264    if exts.emoji {
265        mask[b':' as usize] = true;
266    }
267    if exts.bookdown_references {
268        mask[b'(' as usize] = true;
269    }
270    // `{{< ... >}}` shortcodes: the dispatcher tries them on any
271    // `{` regardless of the `quarto_shortcodes` extension flag, so
272    // `{` must always be flagged here.
273    mask[b'{' as usize] = true;
274
275    // Bare-URI autolinks (`http://...` without `<>`) have no
276    // leading-byte gate in the dispatcher — `try_parse_bare_uri`
277    // probes for a URI scheme starting at every byte. Flag all
278    // ASCII alphabetic bytes so the bulk-skip stops on every
279    // potential scheme starter. This effectively disables the
280    // bulk-skip benefit for prose under GFM-style flavors but
281    // preserves correctness; ASCII digits / punctuation / non-ASCII
282    // bytes still skip cleanly.
283    if exts.autolink_bare_uris {
284        for b in b'a'..=b'z' {
285            mask[b as usize] = true;
286        }
287        for b in b'A'..=b'Z' {
288            mask[b as usize] = true;
289        }
290    }
291
292    mask
293}
294
295fn is_emoji_boundary(text: &str, pos: usize) -> bool {
296    if pos > 0 {
297        let prev = text.as_bytes()[pos - 1] as char;
298        if prev.is_ascii_alphanumeric() || prev == '_' {
299            return false;
300        }
301    }
302    true
303}
304
305#[inline]
306fn advance_char_boundary(text: &str, pos: usize, end: usize) -> usize {
307    if pos >= end || pos >= text.len() {
308        return pos;
309    }
310    let ch_len = text[pos..]
311        .chars()
312        .next()
313        .map_or(1, std::primitive::char::len_utf8);
314    (pos + ch_len).min(end)
315}
316
317#[allow(clippy::too_many_arguments)]
318fn parse_inline_range_impl(
319    text: &str,
320    start: usize,
321    end: usize,
322    config: &ParserOptions,
323    builder: &mut GreenNodeBuilder,
324    nested_in_link: bool,
325    plan: &EmphasisPlan,
326    bracket_plan: &BracketPlan,
327    construct_plan: &ConstructPlan,
328    suppress_inner_links: bool,
329    mask: &[bool; 256],
330) {
331    log::trace!(
332        "parse_inline_range: start={}, end={}, text={:?}",
333        start,
334        end,
335        &text[start..end]
336    );
337    let mut pos = start;
338    let mut text_start = start;
339    let bytes = text.as_bytes();
340
341    while pos < end {
342        // Bulk-skip plain bytes between structural bytes. Plans
343        // (`construct_plan`, `bracket_plan`, emphasis `plan`) only
344        // resolve at structural byte positions, so skipping here
345        // never elides a real match. `text_start` is preserved
346        // across the skip; the next emitted construct flushes the
347        // accumulated TEXT span.
348        if !mask[bytes[pos] as usize] {
349            let mut next = pos + 1;
350            while next < end && !mask[bytes[next] as usize] {
351                next += 1;
352            }
353            pos = next;
354            if pos >= end {
355                break;
356            }
357        }
358        // IR-driven dispatch: if the IR identified a Pandoc standalone
359        // construct starting here, emit it directly. Bypasses the
360        // dispatcher's ordered-try chain for inline footnotes, native
361        // spans, footnote references, citations, and bracketed spans
362        // under `Dialect::Pandoc`. The IR scan gates these on
363        // `!is_commonmark` and the relevant extension flag, so this
364        // branch is empty under CommonMark dialect (where the legacy
365        // dispatcher branches still run when the extension is enabled).
366        if let Some(dispo) = construct_plan.lookup(pos) {
367            match *dispo {
368                ConstructDispo::InlineFootnote { end: dispo_end } => {
369                    if dispo_end <= end
370                        && let Some((len, content)) = try_parse_inline_footnote(&text[pos..])
371                        && pos + len == dispo_end
372                    {
373                        if pos > text_start {
374                            builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
375                        }
376                        log::trace!("IR: matched inline footnote at pos {}", pos);
377                        emit_inline_footnote(builder, content, config);
378                        pos += len;
379                        text_start = pos;
380                        continue;
381                    }
382                }
383                ConstructDispo::NativeSpan { end: dispo_end } => {
384                    if dispo_end <= end
385                        && let Some((len, content, attributes)) =
386                            try_parse_native_span(&text[pos..])
387                        && pos + len == dispo_end
388                    {
389                        if pos > text_start {
390                            builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
391                        }
392                        log::trace!("IR: matched native span at pos {}", pos);
393                        emit_native_span(builder, content, &attributes, config);
394                        pos += len;
395                        text_start = pos;
396                        continue;
397                    }
398                }
399                ConstructDispo::FootnoteReference { end: dispo_end } => {
400                    if dispo_end <= end
401                        && let Some((len, id)) = try_parse_footnote_reference(&text[pos..])
402                        && pos + len == dispo_end
403                    {
404                        if pos > text_start {
405                            builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
406                        }
407                        log::trace!("IR: matched footnote reference at pos {}", pos);
408                        emit_footnote_reference(builder, &id);
409                        pos += len;
410                        text_start = pos;
411                        continue;
412                    }
413                }
414                ConstructDispo::BracketedCitation { end: dispo_end } => {
415                    if dispo_end <= end
416                        && let Some((len, content)) = try_parse_bracketed_citation(&text[pos..])
417                        && pos + len == dispo_end
418                    {
419                        if pos > text_start {
420                            builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
421                        }
422                        log::trace!("IR: matched bracketed citation at pos {}", pos);
423                        emit_bracketed_citation(builder, content);
424                        pos += len;
425                        text_start = pos;
426                        continue;
427                    }
428                }
429                ConstructDispo::BareCitation { end: dispo_end } => {
430                    if dispo_end <= end
431                        && let Some((len, key, has_suppress)) =
432                            try_parse_bare_citation(&text[pos..])
433                        && pos + len == dispo_end
434                    {
435                        let is_crossref = config.extensions.quarto_crossrefs
436                            && super::citations::is_quarto_crossref_key(key);
437                        if is_crossref || config.extensions.citations {
438                            if pos > text_start {
439                                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
440                            }
441                            if is_crossref {
442                                log::trace!("IR: matched Quarto crossref at pos {}: {}", pos, key);
443                                super::citations::emit_crossref(builder, key, has_suppress);
444                            } else {
445                                log::trace!("IR: matched bare citation at pos {}: {}", pos, key);
446                                emit_bare_citation(builder, key, has_suppress);
447                            }
448                            pos += len;
449                            text_start = pos;
450                            continue;
451                        }
452                    }
453                }
454                ConstructDispo::BracketedSpan { end: dispo_end } => {
455                    if dispo_end <= end
456                        && let Some((len, content, attrs)) = try_parse_bracketed_span(&text[pos..])
457                        && pos + len == dispo_end
458                    {
459                        if pos > text_start {
460                            builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
461                        }
462                        log::trace!("IR: matched bracketed span at pos {}", pos);
463                        emit_bracketed_span(builder, &content, &attrs, config);
464                        pos += len;
465                        text_start = pos;
466                        continue;
467                    }
468                }
469            }
470        }
471
472        // IR-driven bracket dispatch: if the IR's `process_brackets`
473        // resolved a bracket pair starting at this position, emit it
474        // directly via the appropriate helper. The
475        // dispatcher's `try_parse_*` recognizers compute the actual
476        // byte length and extract content / attributes; the IR's
477        // `suffix_end` is used to constrain the dispatcher's match
478        // shape so the two pipelines agree on which link variant
479        // resolved (e.g. `[foo][bar]` with `bar` undefined and `foo`
480        // defined: IR resolves `[foo]` as shortcut, but the
481        // dispatcher's `try_parse_reference_link` would otherwise
482        // greedily return the full-ref shape). Suppression of inner
483        // LINK / REFERENCE LINK during LINK-text recursion is applied
484        // here (pandoc-native: outer-wins for nested links).
485        //
486        // Pandoc-extended `{.attrs}` after a link can extend the
487        // dispatcher's match length past the IR's `suffix_end`. The
488        // dispatcher's len is therefore constrained to
489        // `[suffix_end, end]` rather than required to equal
490        // `suffix_end` exactly.
491        // IR-driven dispatch: Pandoc unresolved bracket-shape pattern.
492        // Before emitting the `UNRESOLVED_REFERENCE` wrapper, give the
493        // dispatcher's lenient inline-link / inline-image parsers a
494        // chance to override. The IR's `try_inline_suffix` is stricter
495        // than pandoc-markdown for some destination shapes (URLs with
496        // spaces, titles with embedded quotes, shortcode-style braces);
497        // the dispatcher accepts those and produces a real LINK / IMAGE
498        // node — pandoc-native agrees. Without this override, valid
499        // pandoc links would degrade to `UNRESOLVED_REFERENCE` here.
500        if let Some(super::inline_ir::BracketDispo::UnresolvedReference {
501            is_image,
502            text_start: ref_text_start,
503            text_end: ref_text_end,
504            end: ref_end,
505        }) = bracket_plan.lookup(pos)
506        {
507            let is_image = *is_image;
508            let dispo_suffix_end = *ref_end;
509            let suppress = suppress_inner_links && !is_image;
510            if !suppress {
511                let ctx = LinkScanContext::from_options(config);
512                let is_commonmark = config.dialect == Dialect::CommonMark;
513                if is_image {
514                    if config.extensions.inline_images
515                        && let Some((len, alt_text, dest, attributes)) =
516                            try_parse_inline_image(&text[pos..], ctx)
517                        && pos + len >= dispo_suffix_end
518                        && pos + len <= end
519                    {
520                        if pos > text_start {
521                            builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
522                        }
523                        log::trace!(
524                            "IR: dispatcher overrode UnresolvedReference with inline image at pos {}",
525                            pos
526                        );
527                        emit_inline_image(
528                            builder,
529                            &text[pos..pos + len],
530                            alt_text,
531                            dest,
532                            attributes,
533                            config,
534                        );
535                        pos += len;
536                        text_start = pos;
537                        continue;
538                    }
539                } else if config.extensions.inline_links
540                    && let Some((len, link_text, dest, attributes)) =
541                        try_parse_inline_link(&text[pos..], is_commonmark, ctx)
542                    && pos + len >= dispo_suffix_end
543                    && pos + len <= end
544                {
545                    if pos > text_start {
546                        builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
547                    }
548                    log::trace!(
549                        "IR: dispatcher overrode UnresolvedReference with inline link at pos {}",
550                        pos
551                    );
552                    emit_inline_link(
553                        builder,
554                        &text[pos..pos + len],
555                        link_text,
556                        dest,
557                        attributes,
558                        config,
559                    );
560                    pos += len;
561                    text_start = pos;
562                    continue;
563                }
564            }
565
566            // Dispatcher didn't override; emit the wrapper.
567            let inner_text = &text[*ref_text_start..*ref_text_end];
568            let suffix_start = *ref_text_end + 1;
569            let label_suffix = if suffix_start < *ref_end {
570                Some(&text[suffix_start..*ref_end])
571            } else {
572                None
573            };
574            if pos > text_start {
575                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
576            }
577            log::trace!(
578                "IR: unresolved Pandoc reference shape at pos {}..{}",
579                pos,
580                ref_end
581            );
582            emit_unresolved_reference(builder, is_image, inner_text, label_suffix, config);
583            pos = *ref_end;
584            text_start = pos;
585            continue;
586        }
587
588        if let Some(super::inline_ir::BracketDispo::Open {
589            is_image,
590            suffix_end,
591            ..
592        }) = bracket_plan.lookup(pos)
593        {
594            let is_image = *is_image;
595            let dispo_suffix_end = *suffix_end;
596            let suppress = suppress_inner_links && !is_image;
597            if !suppress {
598                let ctx = LinkScanContext::from_options(config);
599                let allow_shortcut = config.extensions.shortcut_reference_links;
600                let is_commonmark = config.dialect == Dialect::CommonMark;
601                if is_image {
602                    if config.extensions.inline_images
603                        && let Some((len, alt_text, dest, attributes)) =
604                            try_parse_inline_image(&text[pos..], ctx)
605                        && pos + len >= dispo_suffix_end
606                        && pos + len <= end
607                    {
608                        if pos > text_start {
609                            builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
610                        }
611                        log::trace!("IR: matched inline image at pos {}", pos);
612                        emit_inline_image(
613                            builder,
614                            &text[pos..pos + len],
615                            alt_text,
616                            dest,
617                            attributes,
618                            config,
619                        );
620                        pos += len;
621                        text_start = pos;
622                        continue;
623                    }
624                    if config.extensions.reference_links
625                        && let Some((len, alt_text, reference, is_shortcut)) =
626                            try_parse_reference_image(&text[pos..], allow_shortcut)
627                        && pos + len == dispo_suffix_end
628                        && pos + len <= end
629                    {
630                        if pos > text_start {
631                            builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
632                        }
633                        log::trace!("IR: matched reference image at pos {}", pos);
634                        emit_reference_image(builder, alt_text, &reference, is_shortcut, config);
635                        pos += len;
636                        text_start = pos;
637                        continue;
638                    }
639                } else {
640                    if config.extensions.inline_links
641                        && let Some((len, link_text, dest, attributes)) =
642                            try_parse_inline_link(&text[pos..], is_commonmark, ctx)
643                        && pos + len >= dispo_suffix_end
644                        && pos + len <= end
645                    {
646                        if pos > text_start {
647                            builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
648                        }
649                        log::trace!("IR: matched inline link at pos {}", pos);
650                        emit_inline_link(
651                            builder,
652                            &text[pos..pos + len],
653                            link_text,
654                            dest,
655                            attributes,
656                            config,
657                        );
658                        pos += len;
659                        text_start = pos;
660                        continue;
661                    }
662                    if config.extensions.reference_links
663                        && let Some((len, link_text, reference, is_shortcut)) =
664                            try_parse_reference_link(
665                                &text[pos..],
666                                allow_shortcut,
667                                config.extensions.inline_links,
668                                ctx,
669                            )
670                        && pos + len == dispo_suffix_end
671                        && pos + len <= end
672                    {
673                        if pos > text_start {
674                            builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
675                        }
676                        log::trace!("IR: matched reference link at pos {}", pos);
677                        emit_reference_link(builder, link_text, &reference, is_shortcut, config);
678                        pos += len;
679                        text_start = pos;
680                        continue;
681                    }
682                }
683            }
684        }
685
686        let byte = text.as_bytes()[pos];
687
688        // Backslash math (highest priority if enabled)
689        if byte == b'\\' {
690            // Try double backslash display math first: \\[...\\]
691            if config.extensions.tex_math_double_backslash {
692                if let Some((len, content)) = try_parse_double_backslash_display_math(&text[pos..])
693                {
694                    if pos > text_start {
695                        builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
696                    }
697                    log::trace!("Matched double backslash display math at pos {}", pos);
698                    emit_double_backslash_display_math(builder, content);
699                    pos += len;
700                    text_start = pos;
701                    continue;
702                }
703
704                // Try double backslash inline math: \\(...\\)
705                if let Some((len, content)) = try_parse_double_backslash_inline_math(&text[pos..]) {
706                    if pos > text_start {
707                        builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
708                    }
709                    log::trace!("Matched double backslash inline math at pos {}", pos);
710                    emit_double_backslash_inline_math(builder, content);
711                    pos += len;
712                    text_start = pos;
713                    continue;
714                }
715            }
716
717            // Try single backslash display math: \[...\]
718            if config.extensions.tex_math_single_backslash {
719                if let Some((len, content)) = try_parse_single_backslash_display_math(&text[pos..])
720                {
721                    if pos > text_start {
722                        builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
723                    }
724                    log::trace!("Matched single backslash display math at pos {}", pos);
725                    emit_single_backslash_display_math(builder, content);
726                    pos += len;
727                    text_start = pos;
728                    continue;
729                }
730
731                // Try single backslash inline math: \(...\)
732                if let Some((len, content)) = try_parse_single_backslash_inline_math(&text[pos..]) {
733                    if pos > text_start {
734                        builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
735                    }
736                    log::trace!("Matched single backslash inline math at pos {}", pos);
737                    emit_single_backslash_inline_math(builder, content);
738                    pos += len;
739                    text_start = pos;
740                    continue;
741                }
742            }
743
744            // Try math environments \begin{equation}...\end{equation}
745            if config.extensions.raw_tex
746                && let Some((len, begin_marker, content, end_marker)) =
747                    try_parse_math_environment(&text[pos..])
748            {
749                if pos > text_start {
750                    builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
751                }
752                log::trace!("Matched math environment at pos {}", pos);
753                emit_display_math_environment(builder, begin_marker, content, end_marker);
754                pos += len;
755                text_start = pos;
756                continue;
757            }
758
759            // Try bookdown reference: \@ref(label)
760            if config.extensions.bookdown_references
761                && let Some((len, label)) = try_parse_bookdown_reference(&text[pos..])
762            {
763                if pos > text_start {
764                    builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
765                }
766                log::trace!("Matched bookdown reference at pos {}: {}", pos, label);
767                super::citations::emit_bookdown_crossref(builder, label);
768                pos += len;
769                text_start = pos;
770                continue;
771            }
772
773            // Try escapes (after bookdown refs and backslash math)
774            if let Some((len, ch, escape_type)) = try_parse_escape(&text[pos..]) {
775                let escape_enabled = match escape_type {
776                    EscapeType::HardLineBreak => config.extensions.escaped_line_breaks,
777                    EscapeType::NonbreakingSpace => config.extensions.all_symbols_escapable,
778                    EscapeType::Literal => {
779                        // BASE_ESCAPABLE matches Pandoc's markdown_strict /
780                        // original Markdown set, plus `|` and `~` which the
781                        // formatter emits as escapes for pipe-table separators
782                        // and strikethrough delimiters. Recognising those here
783                        // keeps round-trips idempotent in flavors that don't
784                        // enable all_symbols_escapable.
785                        //
786                        // Under CommonMark dialect, the spec (§2.4) explicitly
787                        // allows ANY ASCII punctuation to be backslash-escaped,
788                        // independent of the all_symbols_escapable extension
789                        // (which also widens to whitespace, a Pandoc-only
790                        // construct).
791                        const BASE_ESCAPABLE: &str = "\\`*_{}[]()>#+-.!|~";
792                        BASE_ESCAPABLE.contains(ch)
793                            || config.extensions.all_symbols_escapable
794                            || (config.dialect == crate::Dialect::CommonMark
795                                && ch.is_ascii_punctuation())
796                    }
797                };
798                if !escape_enabled {
799                    // Don't treat as hard line break - skip the escape and continue
800                    // The backslash will be included in the next TEXT token
801                    pos = advance_char_boundary(text, pos, end);
802                    continue;
803                }
804
805                // Emit accumulated text
806                if pos > text_start {
807                    builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
808                }
809
810                log::trace!("Matched escape at pos {}: \\{}", pos, ch);
811                emit_escape(builder, ch, escape_type);
812                pos += len;
813                text_start = pos;
814                continue;
815            }
816
817            // Try LaTeX commands (after escapes, before shortcodes)
818            if config.extensions.raw_tex
819                && let Some(len) = try_parse_latex_command(&text[pos..])
820            {
821                if pos > text_start {
822                    builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
823                }
824                log::trace!("Matched LaTeX command at pos {}", pos);
825                parse_latex_command(builder, &text[pos..], len);
826                pos += len;
827                text_start = pos;
828                continue;
829            }
830        }
831
832        // Try Quarto shortcodes: {{< shortcode >}}
833        if byte == b'{'
834            && pos + 1 < text.len()
835            && text.as_bytes()[pos + 1] == b'{'
836            && let Some((len, name, attrs)) = try_parse_shortcode(&text[pos..])
837        {
838            if pos > text_start {
839                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
840            }
841            log::trace!("Matched shortcode at pos {}: {}", pos, &name);
842            emit_shortcode(builder, &name, attrs);
843            pos += len;
844            text_start = pos;
845            continue;
846        }
847
848        // Try inline executable code spans (`... `r expr`` and `... `{r} expr``)
849        if byte == b'`'
850            && let Some(m) = try_parse_inline_executable(
851                &text[pos..],
852                config.extensions.rmarkdown_inline_code,
853                config.extensions.quarto_inline_code,
854            )
855        {
856            if pos > text_start {
857                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
858            }
859            log::trace!("Matched inline executable code at pos {}", pos);
860            emit_inline_executable(builder, &m);
861            pos += m.total_len;
862            text_start = pos;
863            continue;
864        }
865
866        // Try code spans
867        if byte == b'`' {
868            if let Some((len, content, backtick_count, attributes)) =
869                try_parse_code_span(&text[pos..])
870            {
871                // Emit accumulated text
872                if pos > text_start {
873                    builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
874                }
875
876                log::trace!(
877                    "Matched code span at pos {}: {} backticks",
878                    pos,
879                    backtick_count
880                );
881
882                // Check for raw inline
883                if let Some(ref attrs) = attributes
884                    && config.extensions.raw_attribute
885                    && let Some(format) = is_raw_inline(attrs)
886                {
887                    use super::raw_inline::emit_raw_inline;
888                    log::trace!("Matched raw inline span at pos {}: format={}", pos, format);
889                    emit_raw_inline(builder, content, backtick_count, format);
890                } else if !config.extensions.inline_code_attributes && attributes.is_some() {
891                    let code_span_len = backtick_count * 2 + content.len();
892                    emit_code_span(builder, content, backtick_count, None);
893                    pos += code_span_len;
894                    text_start = pos;
895                    continue;
896                } else {
897                    emit_code_span(builder, content, backtick_count, attributes);
898                }
899
900                pos += len;
901                text_start = pos;
902                continue;
903            }
904
905            // Unmatched backtick run.
906            //
907            // CommonMark (and GFM) treat the whole run as literal text — the
908            // run cannot be re-entered as a shorter opener. Pandoc-markdown
909            // instead lets a longer run shadow a shorter one (e.g.
910            // `` ```foo`` `` parses as `` ` `` + ``<code>foo</code>``), so
911            // for the Pandoc dialect we fall through and advance one byte at
912            // a time, allowing the inner run to be tried on a later iteration.
913            if config.dialect == Dialect::CommonMark {
914                let run_len = text[pos..].bytes().take_while(|&b| b == b'`').count();
915                pos += run_len;
916                continue;
917            }
918        }
919
920        // Try textual emoji aliases: :smile:
921        if byte == b':'
922            && config.extensions.emoji
923            && is_emoji_boundary(text, pos)
924            && let Some((len, _alias)) = try_parse_emoji(&text[pos..])
925        {
926            if pos > text_start {
927                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
928            }
929            log::trace!("Matched emoji at pos {}", pos);
930            emit_emoji(builder, &text[pos..pos + len]);
931            pos += len;
932            text_start = pos;
933            continue;
934        }
935
936        // Try inline footnotes: ^[note]. Under Pandoc dialect this is
937        // consumed via the IR's `ConstructPlan` at the top of the loop;
938        // this dispatcher branch only fires for CommonMark dialect with
939        // the extension explicitly enabled.
940        if byte == b'^'
941            && pos + 1 < text.len()
942            && text.as_bytes()[pos + 1] == b'['
943            && config.dialect == Dialect::CommonMark
944            && config.extensions.inline_footnotes
945            && let Some((len, content)) = try_parse_inline_footnote(&text[pos..])
946        {
947            if pos > text_start {
948                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
949            }
950            log::trace!("Matched inline footnote at pos {}", pos);
951            emit_inline_footnote(builder, content, config);
952            pos += len;
953            text_start = pos;
954            continue;
955        }
956
957        // Try superscript: ^text^
958        if byte == b'^'
959            && config.extensions.superscript
960            && let Some((len, content)) = try_parse_superscript(&text[pos..])
961        {
962            if pos > text_start {
963                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
964            }
965            log::trace!("Matched superscript at pos {}", pos);
966            emit_superscript(builder, content, config);
967            pos += len;
968            text_start = pos;
969            continue;
970        }
971
972        // Try bookdown definition: (\#label) or (ref:label)
973        if byte == b'(' && config.extensions.bookdown_references {
974            if let Some((len, label)) = try_parse_bookdown_definition(&text[pos..]) {
975                if pos > text_start {
976                    builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
977                }
978                log::trace!("Matched bookdown definition at pos {}: {}", pos, label);
979                builder.token(SyntaxKind::TEXT.into(), &text[pos..pos + len]);
980                pos += len;
981                text_start = pos;
982                continue;
983            }
984            if let Some((len, label)) = try_parse_bookdown_text_reference(&text[pos..]) {
985                if pos > text_start {
986                    builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
987                }
988                log::trace!("Matched bookdown text reference at pos {}: {}", pos, label);
989                builder.token(SyntaxKind::TEXT.into(), &text[pos..pos + len]);
990                pos += len;
991                text_start = pos;
992                continue;
993            }
994        }
995
996        // Try strikeout: ~~text~~
997        // Must run before subscript so `~~text~~` is matched as a single
998        // Strikeout rather than two empty Subscripts. Subscript falls back
999        // to consuming `~~` as an empty subscript only when strikeout
1000        // didn't match (e.g. `~~unclosed`).
1001        if byte == b'~'
1002            && config.extensions.strikeout
1003            && let Some((len, content)) = try_parse_strikeout(&text[pos..])
1004        {
1005            if pos > text_start {
1006                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1007            }
1008            log::trace!("Matched strikeout at pos {}", pos);
1009            emit_strikeout(builder, content, config);
1010            pos += len;
1011            text_start = pos;
1012            continue;
1013        }
1014
1015        // Try subscript: ~text~ or `~~` as empty subscript when strikeout
1016        // didn't match (matches pandoc: `~~unclosed` → `Subscript [] + text`).
1017        if byte == b'~'
1018            && config.extensions.subscript
1019            && let Some((len, content)) = try_parse_subscript(&text[pos..])
1020        {
1021            if pos > text_start {
1022                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1023            }
1024            log::trace!("Matched subscript at pos {}", pos);
1025            emit_subscript(builder, content, config);
1026            pos += len;
1027            text_start = pos;
1028            continue;
1029        }
1030
1031        // Try mark/highlight: ==text==
1032        if byte == b'='
1033            && config.extensions.mark
1034            && let Some((len, content)) = try_parse_mark(&text[pos..])
1035        {
1036            if pos > text_start {
1037                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1038            }
1039            log::trace!("Matched mark at pos {}", pos);
1040            emit_mark(builder, content, config);
1041            pos += len;
1042            text_start = pos;
1043            continue;
1044        }
1045
1046        // Try GFM inline math: $`...`$
1047        if byte == b'$'
1048            && config.extensions.tex_math_gfm
1049            && let Some((len, content)) = try_parse_gfm_inline_math(&text[pos..])
1050        {
1051            if pos > text_start {
1052                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1053            }
1054            log::trace!("Matched GFM inline math at pos {}", pos);
1055            emit_gfm_inline_math(builder, content);
1056            pos += len;
1057            text_start = pos;
1058            continue;
1059        }
1060
1061        // Try math ($...$, $$...$$)
1062        if byte == b'$' && config.extensions.tex_math_dollars {
1063            // Try display math first ($$...$$)
1064            if let Some((len, content)) = try_parse_display_math(&text[pos..]) {
1065                // Emit accumulated text
1066                if pos > text_start {
1067                    builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1068                }
1069
1070                let dollar_count = text[pos..].chars().take_while(|&c| c == '$').count();
1071                log::trace!(
1072                    "Matched display math at pos {}: {} dollars",
1073                    pos,
1074                    dollar_count
1075                );
1076
1077                // Check for trailing attributes (Quarto cross-reference support)
1078                let after_math = &text[pos + len..];
1079                let attr_len = if config.extensions.quarto_crossrefs {
1080                    use crate::parser::utils::attributes::try_parse_trailing_attributes;
1081                    if let Some((_attr_block, _)) = try_parse_trailing_attributes(after_math) {
1082                        let trimmed_after = after_math.trim_start();
1083                        if let Some(open_brace_pos) = trimmed_after.find('{') {
1084                            let ws_before_brace = after_math.len() - trimmed_after.len();
1085                            let attr_text_len = trimmed_after[open_brace_pos..]
1086                                .find('}')
1087                                .map(|close| close + 1)
1088                                .unwrap_or(0);
1089                            ws_before_brace + open_brace_pos + attr_text_len
1090                        } else {
1091                            0
1092                        }
1093                    } else {
1094                        0
1095                    }
1096                } else {
1097                    0
1098                };
1099
1100                let total_len = len + attr_len;
1101                emit_display_math(builder, content, dollar_count);
1102
1103                // Emit attributes if present
1104                if attr_len > 0 {
1105                    use crate::parser::utils::attributes::{
1106                        emit_attributes, try_parse_trailing_attributes,
1107                    };
1108                    let attr_text = &text[pos + len..pos + total_len];
1109                    if let Some((attr_block, _text_before)) =
1110                        try_parse_trailing_attributes(attr_text)
1111                    {
1112                        let trimmed_after = attr_text.trim_start();
1113                        let ws_len = attr_text.len() - trimmed_after.len();
1114                        if ws_len > 0 {
1115                            builder.token(SyntaxKind::WHITESPACE.into(), &attr_text[..ws_len]);
1116                        }
1117                        emit_attributes(builder, &attr_block);
1118                    }
1119                }
1120
1121                pos += total_len;
1122                text_start = pos;
1123                continue;
1124            }
1125
1126            // Try inline math ($...$)
1127            if let Some((len, content)) = try_parse_inline_math(&text[pos..]) {
1128                // Emit accumulated text
1129                if pos > text_start {
1130                    builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1131                }
1132
1133                log::trace!("Matched inline math at pos {}", pos);
1134                emit_inline_math(builder, content);
1135                pos += len;
1136                text_start = pos;
1137                continue;
1138            }
1139
1140            // Neither display nor inline math matched - emit the $ as literal text
1141            // This ensures each $ gets its own TEXT token for CST compatibility
1142            if pos > text_start {
1143                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1144            }
1145            builder.token(SyntaxKind::TEXT.into(), "$");
1146            pos = advance_char_boundary(text, pos, end);
1147            text_start = pos;
1148            continue;
1149        }
1150
1151        // Try autolinks: <url> or <email>
1152        if byte == b'<'
1153            && config.extensions.autolinks
1154            && let Some((len, url)) = try_parse_autolink(
1155                &text[pos..],
1156                config.dialect == crate::options::Dialect::CommonMark,
1157            )
1158        {
1159            if pos > text_start {
1160                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1161            }
1162            log::trace!("Matched autolink at pos {}", pos);
1163            emit_autolink(builder, &text[pos..pos + len], url);
1164            pos += len;
1165            text_start = pos;
1166            continue;
1167        }
1168
1169        if !nested_in_link
1170            && config.extensions.autolink_bare_uris
1171            && let Some((len, url)) = try_parse_bare_uri(&text[pos..])
1172        {
1173            if pos > text_start {
1174                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1175            }
1176            log::trace!("Matched bare URI at pos {}", pos);
1177            emit_bare_uri_link(builder, url, config);
1178            pos += len;
1179            text_start = pos;
1180            continue;
1181        }
1182
1183        // Try native spans: <span>text</span> (after autolink since both
1184        // start with <). Under Pandoc dialect this is consumed via the
1185        // IR's `ConstructPlan` at the top of the loop; this dispatcher
1186        // branch only fires for CommonMark dialect with the extension
1187        // explicitly enabled.
1188        if byte == b'<'
1189            && config.dialect == Dialect::CommonMark
1190            && config.extensions.native_spans
1191            && let Some((len, content, attributes)) = try_parse_native_span(&text[pos..])
1192        {
1193            if pos > text_start {
1194                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1195            }
1196            log::trace!("Matched native span at pos {}", pos);
1197            emit_native_span(builder, content, &attributes, config);
1198            pos += len;
1199            text_start = pos;
1200            continue;
1201        }
1202
1203        // Try inline raw HTML (CommonMark §6.6 / Pandoc raw_html). Must run
1204        // after autolinks (more specific) and native spans (Pandoc
1205        // <span>…</span> wrapper) since all three start with `<`.
1206        if byte == b'<'
1207            && config.extensions.raw_html
1208            && let Some(len) = try_parse_inline_html(&text[pos..])
1209        {
1210            if pos > text_start {
1211                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1212            }
1213            log::trace!("Matched inline raw HTML at pos {}", pos);
1214            emit_inline_html(builder, &text[pos..pos + len]);
1215            pos += len;
1216            text_start = pos;
1217            continue;
1218        }
1219
1220        // Bracket-starting elements: inline / reference links and
1221        // images are dispatched via the IR-driven arm at the top of
1222        // the loop, gated by the IR's `BracketPlan`. Only dialect-CM-
1223        // specific Pandoc-extension constructs that share the `[...]`
1224        // shape (footnote refs, bracketed citations) need a CM-gated
1225        // dispatcher branch — under Pandoc dialect they're consumed
1226        // via the IR's `ConstructPlan` instead.
1227        if byte == b'['
1228            && config.dialect == Dialect::CommonMark
1229            && config.extensions.footnotes
1230            && let Some((len, id)) = try_parse_footnote_reference(&text[pos..])
1231        {
1232            if pos > text_start {
1233                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1234            }
1235            log::trace!("Matched footnote reference at pos {}", pos);
1236            emit_footnote_reference(builder, &id);
1237            pos += len;
1238            text_start = pos;
1239            continue;
1240        }
1241        if byte == b'['
1242            && config.dialect == Dialect::CommonMark
1243            && config.extensions.citations
1244            && let Some((len, content)) = try_parse_bracketed_citation(&text[pos..])
1245        {
1246            if pos > text_start {
1247                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1248            }
1249            log::trace!("Matched bracketed citation at pos {}", pos);
1250            emit_bracketed_citation(builder, content);
1251            pos += len;
1252            text_start = pos;
1253            continue;
1254        }
1255
1256        // Try bracketed spans: [text]{.class}. Must come after
1257        // links/citations. Under Pandoc dialect this is consumed via
1258        // the IR's `ConstructPlan` at the top of the loop; this
1259        // dispatcher branch only fires for CommonMark dialect with the
1260        // extension explicitly enabled.
1261        if config.dialect == Dialect::CommonMark
1262            && byte == b'['
1263            && config.extensions.bracketed_spans
1264            && let Some((len, text_content, attrs)) = try_parse_bracketed_span(&text[pos..])
1265        {
1266            if pos > text_start {
1267                builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1268            }
1269            log::trace!("Matched bracketed span at pos {}", pos);
1270            emit_bracketed_span(builder, &text_content, &attrs, config);
1271            pos += len;
1272            text_start = pos;
1273            continue;
1274        }
1275
1276        // Try bare citation: @cite (must come after bracketed elements).
1277        // Under Pandoc dialect this is consumed via the IR's
1278        // `ConstructPlan` at the top of the loop; this dispatcher branch
1279        // only fires for CommonMark dialect with the extension
1280        // explicitly enabled.
1281        if config.dialect == Dialect::CommonMark
1282            && byte == b'@'
1283            && (config.extensions.citations || config.extensions.quarto_crossrefs)
1284            && let Some((len, key, has_suppress)) = try_parse_bare_citation(&text[pos..])
1285        {
1286            let is_crossref =
1287                config.extensions.quarto_crossrefs && super::citations::is_quarto_crossref_key(key);
1288            if is_crossref || config.extensions.citations {
1289                if pos > text_start {
1290                    builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1291                }
1292                if is_crossref {
1293                    log::trace!("Matched Quarto crossref at pos {}: {}", pos, &key);
1294                    super::citations::emit_crossref(builder, key, has_suppress);
1295                } else {
1296                    log::trace!("Matched bare citation at pos {}: {}", pos, &key);
1297                    emit_bare_citation(builder, key, has_suppress);
1298                }
1299                pos += len;
1300                text_start = pos;
1301                continue;
1302            }
1303        }
1304
1305        // Try suppress-author citation: -@cite. Under Pandoc dialect
1306        // this is consumed via the IR's `ConstructPlan` at the top of
1307        // the loop; this dispatcher branch only fires for CommonMark
1308        // dialect with the extension explicitly enabled.
1309        if config.dialect == Dialect::CommonMark
1310            && byte == b'-'
1311            && pos + 1 < text.len()
1312            && text.as_bytes()[pos + 1] == b'@'
1313            && (config.extensions.citations || config.extensions.quarto_crossrefs)
1314            && let Some((len, key, has_suppress)) = try_parse_bare_citation(&text[pos..])
1315        {
1316            let is_crossref =
1317                config.extensions.quarto_crossrefs && super::citations::is_quarto_crossref_key(key);
1318            if is_crossref || config.extensions.citations {
1319                if pos > text_start {
1320                    builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1321                }
1322                if is_crossref {
1323                    log::trace!("Matched Quarto crossref at pos {}: {}", pos, &key);
1324                    super::citations::emit_crossref(builder, key, has_suppress);
1325                } else {
1326                    log::trace!("Matched suppress-author citation at pos {}: {}", pos, &key);
1327                    emit_bare_citation(builder, key, has_suppress);
1328                }
1329                pos += len;
1330                text_start = pos;
1331                continue;
1332            }
1333        }
1334
1335        // Emphasis emission, plan-driven. The IR's emphasis pass has
1336        // already decided every delimiter byte's disposition (open
1337        // marker, close marker, or unmatched literal); consult the
1338        // plan here instead of re-scanning.
1339        if byte == b'*' || byte == b'_' {
1340            match plan.lookup(pos) {
1341                Some(DelimChar::Open {
1342                    len,
1343                    partner,
1344                    partner_len,
1345                    kind,
1346                }) => {
1347                    if pos > text_start {
1348                        builder.token(SyntaxKind::TEXT.into(), &text[text_start..pos]);
1349                    }
1350                    let len = len as usize;
1351                    let partner_len = partner_len as usize;
1352                    let (wrapper_kind, marker_kind) = match kind {
1353                        EmphasisKind::Strong => (SyntaxKind::STRONG, SyntaxKind::STRONG_MARKER),
1354                        EmphasisKind::Emph => (SyntaxKind::EMPHASIS, SyntaxKind::EMPHASIS_MARKER),
1355                    };
1356                    builder.start_node(wrapper_kind.into());
1357                    builder.token(marker_kind.into(), &text[pos..pos + len]);
1358                    parse_inline_range_impl(
1359                        text,
1360                        pos + len,
1361                        partner,
1362                        config,
1363                        builder,
1364                        nested_in_link,
1365                        plan,
1366                        bracket_plan,
1367                        construct_plan,
1368                        suppress_inner_links,
1369                        mask,
1370                    );
1371                    builder.token(marker_kind.into(), &text[partner..partner + partner_len]);
1372                    builder.finish_node();
1373                    pos = partner + partner_len;
1374                    text_start = pos;
1375                    continue;
1376                }
1377                Some(DelimChar::Close) => {
1378                    // Defensive: a close should be jumped past by its
1379                    // matching open. If we hit one anyway (e.g. when the
1380                    // outer caller's range starts mid-pair), let it be
1381                    // emitted as part of the surrounding text by simply
1382                    // advancing. text_start stays put so the byte folds
1383                    // into the next TEXT flush.
1384                    pos += 1;
1385                    continue;
1386                }
1387                Some(DelimChar::Literal) | None => {
1388                    // Unmatched delim chars at this position behave as
1389                    // literal text. Don't emit yet — let them coalesce
1390                    // with surrounding plain bytes via the existing
1391                    // text_start flushing so the CST keeps the same TEXT
1392                    // token granularity Pandoc fixtures expect.
1393                    let bytes = text.as_bytes();
1394                    let mut end_pos = pos + 1;
1395                    while end_pos < end && bytes[end_pos] == byte {
1396                        match plan.lookup(end_pos) {
1397                            Some(DelimChar::Literal) | None => end_pos += 1,
1398                            _ => break,
1399                        }
1400                    }
1401                    pos = end_pos;
1402                    continue;
1403                }
1404            }
1405        }
1406
1407        // Check for newlines - may need to emit as hard line break
1408        if byte == b'\r' && pos + 1 < end && text.as_bytes()[pos + 1] == b'\n' {
1409            let text_before = &text[text_start..pos];
1410
1411            // Check for trailing spaces hard line break (always enabled in Pandoc)
1412            let trailing_spaces = text_before.chars().rev().take_while(|&c| c == ' ').count();
1413            if trailing_spaces >= 2 {
1414                // Emit text before the trailing spaces
1415                let text_content = &text_before[..text_before.len() - trailing_spaces];
1416                if !text_content.is_empty() {
1417                    builder.token(SyntaxKind::TEXT.into(), text_content);
1418                }
1419                let spaces = " ".repeat(trailing_spaces);
1420                builder.token(
1421                    SyntaxKind::HARD_LINE_BREAK.into(),
1422                    &format!("{}\r\n", spaces),
1423                );
1424                pos += 2;
1425                text_start = pos;
1426                continue;
1427            }
1428
1429            // hard_line_breaks: treat all single newlines as hard line breaks
1430            if config.extensions.hard_line_breaks {
1431                if !text_before.is_empty() {
1432                    builder.token(SyntaxKind::TEXT.into(), text_before);
1433                }
1434                builder.token(SyntaxKind::HARD_LINE_BREAK.into(), "\r\n");
1435                pos += 2;
1436                text_start = pos;
1437                continue;
1438            }
1439
1440            // Regular newline
1441            if !text_before.is_empty() {
1442                builder.token(SyntaxKind::TEXT.into(), text_before);
1443            }
1444            builder.token(SyntaxKind::NEWLINE.into(), "\r\n");
1445            pos += 2;
1446            text_start = pos;
1447            continue;
1448        }
1449
1450        if byte == b'\n' {
1451            let text_before = &text[text_start..pos];
1452
1453            // Check for trailing spaces hard line break (always enabled in Pandoc)
1454            let trailing_spaces = text_before.chars().rev().take_while(|&c| c == ' ').count();
1455            if trailing_spaces >= 2 {
1456                // Emit text before the trailing spaces
1457                let text_content = &text_before[..text_before.len() - trailing_spaces];
1458                if !text_content.is_empty() {
1459                    builder.token(SyntaxKind::TEXT.into(), text_content);
1460                }
1461                let spaces = " ".repeat(trailing_spaces);
1462                builder.token(SyntaxKind::HARD_LINE_BREAK.into(), &format!("{}\n", spaces));
1463                pos += 1;
1464                text_start = pos;
1465                continue;
1466            }
1467
1468            // hard_line_breaks: treat all single newlines as hard line breaks
1469            if config.extensions.hard_line_breaks {
1470                if !text_before.is_empty() {
1471                    builder.token(SyntaxKind::TEXT.into(), text_before);
1472                }
1473                builder.token(SyntaxKind::HARD_LINE_BREAK.into(), "\n");
1474                pos += 1;
1475                text_start = pos;
1476                continue;
1477            }
1478
1479            // Regular newline
1480            if !text_before.is_empty() {
1481                builder.token(SyntaxKind::TEXT.into(), text_before);
1482            }
1483            builder.token(SyntaxKind::NEWLINE.into(), "\n");
1484            pos += 1;
1485            text_start = pos;
1486            continue;
1487        }
1488
1489        // Regular character, keep accumulating
1490        pos = advance_char_boundary(text, pos, end);
1491    }
1492
1493    // Emit any remaining text
1494    if pos > text_start && text_start < end {
1495        log::trace!("Emitting remaining TEXT: {:?}", &text[text_start..end]);
1496        builder.token(SyntaxKind::TEXT.into(), &text[text_start..end]);
1497    }
1498
1499    log::trace!("parse_inline_range complete: start={}, end={}", start, end);
1500}
1501
1502#[cfg(test)]
1503mod tests {
1504    use super::*;
1505    use crate::syntax::{SyntaxKind, SyntaxNode};
1506    use rowan::GreenNode;
1507
1508    #[test]
1509    fn test_recursive_simple_emphasis() {
1510        let text = "*test*";
1511        let config = ParserOptions::default();
1512        let mut builder = GreenNodeBuilder::new();
1513
1514        parse_inline_text_recursive(&mut builder, text, &config);
1515
1516        let green: GreenNode = builder.finish();
1517        let node = SyntaxNode::new_root(green);
1518
1519        // Should be lossless
1520        assert_eq!(node.text().to_string(), text);
1521
1522        // Should have EMPHASIS node
1523        let has_emph = node.descendants().any(|n| n.kind() == SyntaxKind::EMPHASIS);
1524        assert!(has_emph, "Should have EMPHASIS node");
1525    }
1526
1527    #[test]
1528    fn test_recursive_nested() {
1529        let text = "*foo **bar** baz*";
1530        let config = ParserOptions::default();
1531        let mut builder = GreenNodeBuilder::new();
1532
1533        // Wrap in a PARAGRAPH node (inline content needs a parent)
1534        builder.start_node(SyntaxKind::PARAGRAPH.into());
1535        parse_inline_text_recursive(&mut builder, text, &config);
1536        builder.finish_node();
1537
1538        let green: GreenNode = builder.finish();
1539        let node = SyntaxNode::new_root(green);
1540
1541        // Should be lossless
1542        assert_eq!(node.text().to_string(), text);
1543
1544        // Should have both EMPHASIS and STRONG
1545        let has_emph = node.descendants().any(|n| n.kind() == SyntaxKind::EMPHASIS);
1546        let has_strong = node.descendants().any(|n| n.kind() == SyntaxKind::STRONG);
1547
1548        assert!(has_emph, "Should have EMPHASIS node");
1549        assert!(has_strong, "Should have STRONG node");
1550    }
1551
1552    /// Test Pandoc's "three" algorithm: ***foo* bar**
1553    /// Expected: Strong[Emph[foo], bar]
1554    #[test]
1555    fn test_triple_emphasis_star_then_double_star() {
1556        use crate::options::ParserOptions;
1557        use crate::syntax::SyntaxNode;
1558        use rowan::GreenNode;
1559
1560        let text = "***foo* bar**";
1561        let config = ParserOptions::default();
1562        let mut builder = GreenNodeBuilder::new();
1563
1564        builder.start_node(SyntaxKind::DOCUMENT.into());
1565        parse_inline_text_recursive(&mut builder, text, &config);
1566        builder.finish_node();
1567
1568        let green: GreenNode = builder.finish();
1569        let node = SyntaxNode::new_root(green);
1570
1571        // Verify losslessness
1572        assert_eq!(node.text().to_string(), text);
1573
1574        // Expected structure: STRONG > EMPH > "foo"
1575        // The STRONG should contain EMPH, not the other way around
1576        let structure = format!("{:#?}", node);
1577
1578        // Should have both STRONG and EMPH
1579        assert!(structure.contains("STRONG"), "Should have STRONG node");
1580        assert!(structure.contains("EMPHASIS"), "Should have EMPHASIS node");
1581
1582        // STRONG should be outer, EMPH should be inner
1583        // Check that STRONG comes before EMPH in tree traversal
1584        let mut found_strong = false;
1585        let mut found_emph_after_strong = false;
1586        for descendant in node.descendants() {
1587            if descendant.kind() == SyntaxKind::STRONG {
1588                found_strong = true;
1589            }
1590            if found_strong && descendant.kind() == SyntaxKind::EMPHASIS {
1591                found_emph_after_strong = true;
1592                break;
1593            }
1594        }
1595
1596        assert!(
1597            found_emph_after_strong,
1598            "EMPH should be inside STRONG, not before it. Current structure:\n{}",
1599            structure
1600        );
1601    }
1602
1603    /// Test Pandoc's "three" algorithm: ***foo** bar*
1604    /// Expected: Emph[Strong[foo], bar]
1605    #[test]
1606    fn test_triple_emphasis_double_star_then_star() {
1607        use crate::options::ParserOptions;
1608        use crate::syntax::SyntaxNode;
1609        use rowan::GreenNode;
1610
1611        let text = "***foo** bar*";
1612        let config = ParserOptions::default();
1613        let mut builder = GreenNodeBuilder::new();
1614
1615        builder.start_node(SyntaxKind::DOCUMENT.into());
1616        parse_inline_text_recursive(&mut builder, text, &config);
1617        builder.finish_node();
1618
1619        let green: GreenNode = builder.finish();
1620        let node = SyntaxNode::new_root(green);
1621
1622        // Verify losslessness
1623        assert_eq!(node.text().to_string(), text);
1624
1625        // Expected structure: EMPH > STRONG > "foo"
1626        let structure = format!("{:#?}", node);
1627
1628        // Should have both EMPH and STRONG
1629        assert!(structure.contains("EMPHASIS"), "Should have EMPHASIS node");
1630        assert!(structure.contains("STRONG"), "Should have STRONG node");
1631
1632        // EMPH should be outer, STRONG should be inner
1633        let mut found_emph = false;
1634        let mut found_strong_after_emph = false;
1635        for descendant in node.descendants() {
1636            if descendant.kind() == SyntaxKind::EMPHASIS {
1637                found_emph = true;
1638            }
1639            if found_emph && descendant.kind() == SyntaxKind::STRONG {
1640                found_strong_after_emph = true;
1641                break;
1642            }
1643        }
1644
1645        assert!(
1646            found_strong_after_emph,
1647            "STRONG should be inside EMPH. Current structure:\n{}",
1648            structure
1649        );
1650    }
1651
1652    /// Test that display math with attributes parses correctly
1653    /// Regression test for equation_attributes_single_line golden test
1654    #[test]
1655    fn test_display_math_with_attributes() {
1656        use crate::options::ParserOptions;
1657        use crate::syntax::SyntaxNode;
1658        use rowan::GreenNode;
1659
1660        let text = "$$ E = mc^2 $$ {#eq-einstein}";
1661        let mut config = ParserOptions::default();
1662        config.extensions.quarto_crossrefs = true; // Enable Quarto cross-references
1663
1664        let mut builder = GreenNodeBuilder::new();
1665        builder.start_node(SyntaxKind::DOCUMENT.into()); // Need a root node
1666
1667        // Parse the whole text
1668        parse_inline_text_recursive(&mut builder, text, &config);
1669
1670        builder.finish_node(); // Finish ROOT
1671        let green: GreenNode = builder.finish();
1672        let node = SyntaxNode::new_root(green);
1673
1674        // Verify losslessness
1675        assert_eq!(node.text().to_string(), text);
1676
1677        // Should have DISPLAY_MATH node
1678        let has_display_math = node
1679            .descendants()
1680            .any(|n| n.kind() == SyntaxKind::DISPLAY_MATH);
1681        assert!(has_display_math, "Should have DISPLAY_MATH node");
1682
1683        // Should have ATTRIBUTE node
1684        let has_attributes = node
1685            .descendants()
1686            .any(|n| n.kind() == SyntaxKind::ATTRIBUTE);
1687        assert!(
1688            has_attributes,
1689            "Should have ATTRIBUTE node for {{#eq-einstein}}"
1690        );
1691
1692        // Attributes should not be TEXT
1693        let math_followed_by_text = node.descendants().any(|n| {
1694            n.kind() == SyntaxKind::DISPLAY_MATH
1695                && n.next_sibling()
1696                    .map(|s| {
1697                        s.kind() == SyntaxKind::TEXT
1698                            && s.text().to_string().contains("{#eq-einstein}")
1699                    })
1700                    .unwrap_or(false)
1701        });
1702        assert!(
1703            !math_followed_by_text,
1704            "Attributes should not be parsed as TEXT"
1705        );
1706    }
1707
1708    #[test]
1709    fn test_parse_inline_text_gfm_inline_link_destination_not_autolinked() {
1710        use crate::options::{Dialect, Extensions, Flavor};
1711
1712        let config = ParserOptions {
1713            flavor: Flavor::Gfm,
1714            dialect: Dialect::for_flavor(Flavor::Gfm),
1715            extensions: Extensions::for_flavor(Flavor::Gfm),
1716            ..ParserOptions::default()
1717        };
1718
1719        let mut builder = GreenNodeBuilder::new();
1720        builder.start_node(SyntaxKind::PARAGRAPH.into());
1721        parse_inline_text_recursive(
1722            &mut builder,
1723            "Second Link [link_text](https://link.com)",
1724            &config,
1725        );
1726        builder.finish_node();
1727        let green = builder.finish();
1728        let root = SyntaxNode::new_root(green);
1729
1730        let links: Vec<_> = root
1731            .descendants()
1732            .filter(|n| n.kind() == SyntaxKind::LINK)
1733            .collect();
1734        assert_eq!(
1735            links.len(),
1736            1,
1737            "Expected exactly one LINK node for inline link, not nested bare URI autolink"
1738        );
1739
1740        let link = links[0].clone();
1741        let mut link_text = None::<String>;
1742        let mut link_dest = None::<String>;
1743
1744        for child in link.children() {
1745            match child.kind() {
1746                SyntaxKind::LINK_TEXT => link_text = Some(child.text().to_string()),
1747                SyntaxKind::LINK_DEST => link_dest = Some(child.text().to_string()),
1748                _ => {}
1749            }
1750        }
1751
1752        assert_eq!(link_text.as_deref(), Some("link_text"));
1753        assert_eq!(link_dest.as_deref(), Some("https://link.com"));
1754    }
1755
1756    #[test]
1757    fn test_autolink_bare_uri_utf8_boundary_safe() {
1758        let text = "§";
1759        let mut config = ParserOptions::default();
1760        config.extensions.autolink_bare_uris = true;
1761        let mut builder = GreenNodeBuilder::new();
1762
1763        builder.start_node(SyntaxKind::DOCUMENT.into());
1764        parse_inline_text_recursive(&mut builder, text, &config);
1765        builder.finish_node();
1766
1767        let green: GreenNode = builder.finish();
1768        let node = SyntaxNode::new_root(green);
1769        assert_eq!(node.text().to_string(), text);
1770    }
1771
1772    #[test]
1773    fn test_parse_emphasis_unicode_content_no_panic() {
1774        let text = "*§*";
1775        let config = ParserOptions::default();
1776        let mut builder = GreenNodeBuilder::new();
1777
1778        builder.start_node(SyntaxKind::PARAGRAPH.into());
1779        parse_inline_text_recursive(&mut builder, text, &config);
1780        builder.finish_node();
1781
1782        let green: GreenNode = builder.finish();
1783        let node = SyntaxNode::new_root(green);
1784        let has_emph = node.descendants().any(|n| n.kind() == SyntaxKind::EMPHASIS);
1785        assert!(has_emph, "Should have EMPHASIS node");
1786        assert_eq!(node.text().to_string(), text);
1787    }
1788}
1789
1790#[test]
1791fn test_two_with_nested_one_and_triple_closer() {
1792    // **bold with *italic***
1793    // Should parse as: Strong["bold with ", Emph["italic"]]
1794    // The *** at end is parsed as * (closes Emph) + ** (closes Strong)
1795
1796    use crate::options::ParserOptions;
1797    use crate::syntax::SyntaxNode;
1798    use rowan::GreenNode;
1799
1800    let text = "**bold with *italic***";
1801    let config = ParserOptions::default();
1802    let mut builder = GreenNodeBuilder::new();
1803
1804    builder.start_node(SyntaxKind::PARAGRAPH.into());
1805    parse_inline_text_recursive(&mut builder, text, &config);
1806    builder.finish_node();
1807
1808    let green: GreenNode = builder.finish();
1809    let node = SyntaxNode::new_root(green);
1810
1811    assert_eq!(node.text().to_string(), text, "Should be lossless");
1812
1813    let strong_nodes: Vec<_> = node
1814        .descendants()
1815        .filter(|n| n.kind() == SyntaxKind::STRONG)
1816        .collect();
1817    assert_eq!(strong_nodes.len(), 1, "Should have exactly one STRONG node");
1818    let has_emphasis_in_strong = strong_nodes[0]
1819        .descendants()
1820        .any(|n| n.kind() == SyntaxKind::EMPHASIS);
1821    assert!(
1822        has_emphasis_in_strong,
1823        "STRONG should contain EMPHASIS node"
1824    );
1825}
1826
1827#[test]
1828fn test_emphasis_with_trailing_space_before_closer() {
1829    // *foo * should parse as emphasis (Pandoc behavior)
1830    // For asterisks, Pandoc doesn't require right-flanking for closers
1831
1832    use crate::options::ParserOptions;
1833    use crate::syntax::SyntaxNode;
1834    use rowan::GreenNode;
1835
1836    let text = "*foo *";
1837    let config = ParserOptions::default();
1838    let mut builder = GreenNodeBuilder::new();
1839
1840    builder.start_node(SyntaxKind::PARAGRAPH.into());
1841    parse_inline_text_recursive(&mut builder, text, &config);
1842    builder.finish_node();
1843
1844    let green: GreenNode = builder.finish();
1845    let node = SyntaxNode::new_root(green);
1846
1847    let has_emph = node.descendants().any(|n| n.kind() == SyntaxKind::EMPHASIS);
1848    assert!(has_emph, "Should have EMPHASIS node");
1849    assert_eq!(node.text().to_string(), text);
1850}
1851
1852#[test]
1853fn test_triple_emphasis_all_strong_nested() {
1854    // ***foo** bar **baz*** should parse as Emph[Strong[foo], " bar ", Strong[baz]]
1855    // Pandoc output confirms this
1856
1857    use crate::options::ParserOptions;
1858    use crate::syntax::SyntaxNode;
1859    use rowan::GreenNode;
1860
1861    let text = "***foo** bar **baz***";
1862    let config = ParserOptions::default();
1863    let mut builder = GreenNodeBuilder::new();
1864
1865    builder.start_node(SyntaxKind::DOCUMENT.into());
1866    parse_inline_text_recursive(&mut builder, text, &config);
1867    builder.finish_node();
1868
1869    let green: GreenNode = builder.finish();
1870    let node = SyntaxNode::new_root(green);
1871
1872    // Should have one EMPHASIS node at root
1873    let emphasis_nodes: Vec<_> = node
1874        .descendants()
1875        .filter(|n| n.kind() == SyntaxKind::EMPHASIS)
1876        .collect();
1877    assert_eq!(
1878        emphasis_nodes.len(),
1879        1,
1880        "Should have exactly one EMPHASIS node, found: {}",
1881        emphasis_nodes.len()
1882    );
1883
1884    // EMPHASIS should contain two STRONG nodes
1885    let emphasis_node = emphasis_nodes[0].clone();
1886    let strong_in_emphasis: Vec<_> = emphasis_node
1887        .children()
1888        .filter(|n| n.kind() == SyntaxKind::STRONG)
1889        .collect();
1890    assert_eq!(
1891        strong_in_emphasis.len(),
1892        2,
1893        "EMPHASIS should contain two STRONG nodes, found: {}",
1894        strong_in_emphasis.len()
1895    );
1896
1897    // Verify losslessness
1898    assert_eq!(node.text().to_string(), text);
1899}
1900
1901#[test]
1902fn test_triple_emphasis_all_emph_nested() {
1903    // ***foo* bar *baz*** should parse as Strong[Emph[foo], " bar ", Emph[baz]]
1904    // Pandoc output confirms this
1905
1906    use crate::options::ParserOptions;
1907    use crate::syntax::SyntaxNode;
1908    use rowan::GreenNode;
1909
1910    let text = "***foo* bar *baz***";
1911    let config = ParserOptions::default();
1912    let mut builder = GreenNodeBuilder::new();
1913
1914    builder.start_node(SyntaxKind::DOCUMENT.into());
1915    parse_inline_text_recursive(&mut builder, text, &config);
1916    builder.finish_node();
1917
1918    let green: GreenNode = builder.finish();
1919    let node = SyntaxNode::new_root(green);
1920
1921    // Should have one STRONG node at root
1922    let strong_nodes: Vec<_> = node
1923        .descendants()
1924        .filter(|n| n.kind() == SyntaxKind::STRONG)
1925        .collect();
1926    assert_eq!(
1927        strong_nodes.len(),
1928        1,
1929        "Should have exactly one STRONG node, found: {}",
1930        strong_nodes.len()
1931    );
1932
1933    // STRONG should contain two EMPHASIS nodes
1934    let strong_node = strong_nodes[0].clone();
1935    let emph_in_strong: Vec<_> = strong_node
1936        .children()
1937        .filter(|n| n.kind() == SyntaxKind::EMPHASIS)
1938        .collect();
1939    assert_eq!(
1940        emph_in_strong.len(),
1941        2,
1942        "STRONG should contain two EMPHASIS nodes, found: {}",
1943        emph_in_strong.len()
1944    );
1945
1946    // Verify losslessness
1947    assert_eq!(node.text().to_string(), text);
1948}
1949
1950// Multiline emphasis tests
1951#[test]
1952fn test_parse_emphasis_multiline() {
1953    // Per Pandoc spec, emphasis CAN contain newlines (soft breaks)
1954    use crate::options::ParserOptions;
1955    use crate::syntax::SyntaxNode;
1956    use rowan::GreenNode;
1957
1958    let text = "*text on\nline two*";
1959    let config = ParserOptions::default();
1960    let mut builder = GreenNodeBuilder::new();
1961
1962    builder.start_node(SyntaxKind::PARAGRAPH.into());
1963    parse_inline_text_recursive(&mut builder, text, &config);
1964    builder.finish_node();
1965
1966    let green: GreenNode = builder.finish();
1967    let node = SyntaxNode::new_root(green);
1968
1969    let has_emph = node.descendants().any(|n| n.kind() == SyntaxKind::EMPHASIS);
1970    assert!(has_emph, "Should have EMPHASIS node");
1971
1972    assert_eq!(node.text().to_string(), text);
1973    assert!(
1974        node.text().to_string().contains('\n'),
1975        "Should preserve newline in emphasis content"
1976    );
1977}
1978
1979#[test]
1980fn test_parse_strong_multiline() {
1981    // Per Pandoc spec, strong emphasis CAN contain newlines
1982    use crate::options::ParserOptions;
1983    use crate::syntax::SyntaxNode;
1984    use rowan::GreenNode;
1985
1986    let text = "**strong on\nline two**";
1987    let config = ParserOptions::default();
1988    let mut builder = GreenNodeBuilder::new();
1989
1990    builder.start_node(SyntaxKind::PARAGRAPH.into());
1991    parse_inline_text_recursive(&mut builder, text, &config);
1992    builder.finish_node();
1993
1994    let green: GreenNode = builder.finish();
1995    let node = SyntaxNode::new_root(green);
1996
1997    let has_strong = node.descendants().any(|n| n.kind() == SyntaxKind::STRONG);
1998    assert!(has_strong, "Should have STRONG node");
1999
2000    assert_eq!(node.text().to_string(), text);
2001    assert!(
2002        node.text().to_string().contains('\n'),
2003        "Should preserve newline in strong content"
2004    );
2005}
2006
2007#[test]
2008fn test_parse_triple_emphasis_multiline() {
2009    // Triple emphasis with newlines
2010    use crate::options::ParserOptions;
2011    use crate::syntax::SyntaxNode;
2012    use rowan::GreenNode;
2013
2014    let text = "***both on\nline two***";
2015    let config = ParserOptions::default();
2016    let mut builder = GreenNodeBuilder::new();
2017
2018    builder.start_node(SyntaxKind::PARAGRAPH.into());
2019    parse_inline_text_recursive(&mut builder, text, &config);
2020    builder.finish_node();
2021
2022    let green: GreenNode = builder.finish();
2023    let node = SyntaxNode::new_root(green);
2024
2025    // Should have STRONG node (triple = strong + emph)
2026    let has_strong = node.descendants().any(|n| n.kind() == SyntaxKind::STRONG);
2027    assert!(has_strong, "Should have STRONG node");
2028
2029    assert_eq!(node.text().to_string(), text);
2030    assert!(
2031        node.text().to_string().contains('\n'),
2032        "Should preserve newline in triple emphasis content"
2033    );
2034}