marco-core 1.1.0

nom-based Markdown parser, HTML renderer, and intelligence features (highlights, diagnostics, completions) for the Marco editor.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Inline parser modules - convert grammar output to AST nodes
//!
//! This module contains specialized parsers that convert inline grammar elements
//! (from grammar/inlines) into AST nodes with proper position tracking.
//!
//! Phase 5: Inline parser module extraction

// Shared utilities for all inline parsers
pub mod shared;

// Individual inline parser modules
pub mod cm_autolink_parser;
pub mod cm_backslash_escape_parser;
pub mod cm_code_span_parser;
pub mod cm_emphasis_parser;
pub mod cm_entity_reference_parser;
pub mod cm_image_parser;
pub mod cm_inline_html_parser;
pub mod cm_line_breaks_parser;
pub mod cm_link_parser;
pub mod cm_reference_link_parser;
pub mod cm_strong_emphasis_parser;
pub mod cm_strong_parser;
pub mod gfm_autolink_literal_parser;
pub mod gfm_footnote_reference_parser;
pub mod gfm_strikethrough_parser;
pub mod marco_dash_strikethrough_parser;
pub mod marco_emoji_shortcode_parser;
pub mod marco_inline_footnote_parser;
pub mod marco_mark_parser;
pub mod marco_platform_mentions_parser;
pub mod marco_subscript_arrow_parser;
pub mod marco_subscript_parser;
pub mod marco_superscript_parser;
pub mod marco_task_checkbox_inline_parser;
pub mod math_display_parser;
pub mod math_inline_parser;
pub mod text_parser;

// Re-export parser functions for convenience
pub use cm_autolink_parser::parse_autolink;
pub use cm_backslash_escape_parser::parse_backslash_escape;
pub use cm_code_span_parser::parse_code_span;
pub use cm_emphasis_parser::parse_emphasis;
pub use cm_entity_reference_parser::parse_entity_reference;
pub use cm_image_parser::parse_image;
pub use cm_inline_html_parser::parse_inline_html;
pub use cm_line_breaks_parser::{parse_hard_line_break, parse_soft_line_break};
pub use cm_link_parser::parse_link;
pub use cm_reference_link_parser::parse_reference_link;
pub use cm_strong_emphasis_parser::parse_strong_emphasis;
pub use cm_strong_parser::parse_strong;
pub use gfm_autolink_literal_parser::parse_gfm_autolink_literal;
pub use gfm_footnote_reference_parser::parse_footnote_reference;
pub use gfm_strikethrough_parser::parse_strikethrough;
pub use marco_dash_strikethrough_parser::parse_dash_strikethrough;
pub use marco_emoji_shortcode_parser::parse_emoji_shortcode;
pub use marco_inline_footnote_parser::parse_inline_footnote;
pub use marco_mark_parser::parse_mark;
pub use marco_platform_mentions_parser::parse_platform_mention;
pub use marco_subscript_arrow_parser::parse_subscript_arrow;
pub use marco_subscript_parser::parse_subscript;
pub use marco_superscript_parser::parse_superscript;
pub use marco_task_checkbox_inline_parser::parse_task_checkbox_inline;
pub use math_display_parser::parse_display_math;
pub use math_inline_parser::parse_inline_math;
pub use text_parser::{parse_special_as_text, parse_text};

use super::ast::{Node, NodeKind};
use nom::bytes::complete::take;
use shared::{opt_span, GrammarSpan};

/// Parse inline elements within text content
/// Takes a GrammarSpan to preserve position information
/// Returns a vector of inline nodes (Text, Emphasis, Strong, Link, CodeSpan)
pub fn parse_inlines_from_span(span: GrammarSpan) -> Result<Vec<Node>, Box<dyn std::error::Error>> {
    log::debug!(
        "Parsing inline elements in span at line {}: {:?}",
        span.location_line(),
        span.fragment()
    );

    let mut nodes = Vec::with_capacity(8);
    let mut remaining = span;

    // Safety: prevent infinite loops
    const MAX_ITERATIONS: usize = 1000;
    let mut iteration_count = 0;
    let mut last_offset = 0;

    while !remaining.fragment().is_empty() {
        iteration_count += 1;
        if iteration_count > MAX_ITERATIONS {
            log::error!("Inline parser exceeded MAX_ITERATIONS ({})", MAX_ITERATIONS);
            break;
        }

        let start_pos = remaining.location_offset();

        // Safety: ensure we're making progress
        if start_pos == last_offset && iteration_count > 1 {
            log::error!(
                "Inline parser not making progress at offset {}, forcing skip",
                start_pos
            );
            // Force skip one character
            let skip = remaining
                .fragment()
                .chars()
                .next()
                .map(|c| c.len_utf8())
                .unwrap_or(1);
            if let Ok((rest, _)) = take::<_, _, nom::error::Error<_>>(skip)(remaining) {
                remaining = rest;
                last_offset = remaining.location_offset();
                continue;
            } else {
                break;
            }
        }
        last_offset = start_pos;

        // ---------------------------------------------------------------
        // Fast path: skip all ~25 parser attempts for bytes that cannot
        // possibly start any special inline sequence.
        //
        // Special ASCII bytes (can open a parser):
        //   * _ ` [ < ! & \n \\ $ ^ ~ = -
        // Space (0x20) is usually plain text but "  \n" (2+ spaces + newline)
        // forms a hard line break — let full dispatch handle that case.
        // Any byte >= 0x80 may start a multi-byte character (e.g. ˅ U+02C5)
        // so we leave those for the full dispatch.
        // ---------------------------------------------------------------
        // SAFETY: the loop condition guarantees remaining is non-empty.
        let first_byte = remaining.fragment().as_bytes()[0];
        let is_non_special_ascii = first_byte < 0x80
            && !matches!(
                first_byte,
                b'*' | b'_'
                    | b'`'
                    | b'['
                    | b'<'
                    | b'!'
                    | b'&'
                    | b'\n'
                    | b'\\'
                    | b'$'
                    | b'^'
                    | b'~'
                    | b'='
                    | b'-'
            );
        // Guard spaces: "  \n" is a hard line break — let the full loop handle it.
        let safe_to_fast_path = is_non_special_ascii
            && if first_byte == b' ' {
                let frag = remaining.fragment().as_bytes();
                let sp = frag.iter().take_while(|&&b| b == b' ').count();
                !(sp >= 2 && frag.get(sp) == Some(&b'\n'))
            } else {
                true
            };
        if safe_to_fast_path {
            if let Ok((rest, node)) = parse_text(remaining) {
                nodes.push(node);
                remaining = rest;
                continue;
            }
            // parse_text failed: the position may start a GFM autolink literal,
            // an emoji shortcode, a platform mention, or trailing hard-break spaces.
            // Fall through to the full dispatch so those parsers get a chance.
        }

        // Try parsing code span first (highest priority to avoid conflicts)
        if let Ok((rest, node)) = parse_code_span(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Try parsing display math before inline math (avoid $$ being parsed as two $)
        if crate::parser::shared::parse_math_enabled() {
            if let Ok((rest, node)) = parse_display_math(remaining) {
                nodes.push(node);
                remaining = rest;
                continue;
            }

            // Try parsing inline math
            if let Ok((rest, node)) = parse_inline_math(remaining) {
                nodes.push(node);
                remaining = rest;
                continue;
            }
        }

        // Try parsing backslash escape (before other inline elements)
        if let Ok((rest, node)) = parse_backslash_escape(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Extension inlines (non-CommonMark): try these early so their delimiter
        // sequences aren't consumed as plain text.
        if let Ok((rest, node)) = parse_strikethrough(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        if let Ok((rest, node)) = parse_dash_strikethrough(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        if let Ok((rest, node)) = parse_mark(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // CommonMark underscore emphasis has special delimiter rules. In
        // particular, intraword underscores (alnum _ alnum) should not open
        // or close emphasis. Because our parser advances left-to-right, the
        // underscore parsers may not be able to see the previous character.
        //
        // Workaround: when we're at an underscore run and the previous emitted
        // character is alphanumeric and the next character after the run is
        // alphanumeric, consume the underscore run as literal text.
        if let Some(run_len) = intraword_underscore_run_len(&nodes, remaining.fragment()) {
            if let Ok((rest, consumed)) = take::<_, _, nom::error::Error<_>>(run_len)(remaining) {
                nodes.push(Node {
                    kind: NodeKind::Text("_".repeat(run_len)),
                    span: opt_span(consumed),
                    children: Vec::new(),
                });
                remaining = rest;
                continue;
            }
        }

        // Try parsing strong+emphasis (***text*** / ___text___) before strong
        // so we don't consume the first two delimiters as strong and leave a
        // dangling delimiter behind.
        if let Ok((rest, node)) = parse_strong_emphasis(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Try parsing strong (must come before emphasis to match ** before *)
        if let Ok((rest, node)) = parse_strong(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Try parsing emphasis
        if let Ok((rest, node)) = parse_emphasis(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Extended syntax: inline footnotes `^[...]`.
        // Try before superscript since both start with '^'.
        if let Ok((rest, (ref_node, def_node))) = parse_inline_footnote(remaining) {
            nodes.push(ref_node);
            nodes.push(def_node);
            remaining = rest;
            continue;
        }

        if let Ok((rest, node)) = parse_superscript(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        if let Ok((rest, node)) = parse_subscript_arrow(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        if let Ok((rest, node)) = parse_subscript(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Try parsing GFM autolink literals (www/http(s)/email/protocol forms)
        if let Ok((rest, node)) = parse_gfm_autolink_literal(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Try parsing autolink (must come before link and inline HTML since syntax starts with <)
        if let Ok((rest, node)) = parse_autolink(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Try parsing GFM-style footnote references `[^label]`.
        // Must come before link parsing since it also starts with '['.
        if let Ok((rest, node)) = parse_footnote_reference(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Extended syntax: inline task checkbox markers mid-paragraph.
        // This must come before link parsing since it starts with '['.
        if is_task_checkbox_inline_start_boundary_ok(&nodes, remaining.fragment()) {
            if let Ok((rest, node)) = parse_task_checkbox_inline(remaining) {
                nodes.push(node);
                remaining = rest;
                continue;
            }
        }

        // Try parsing image (must come before link since syntax is similar but starts with !)
        if let Ok((rest, node)) = parse_image(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Try parsing link
        if let Ok((rest, node)) = parse_link(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Try parsing reference-style links (CommonMark)
        if let Ok((rest, node)) = parse_reference_link(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Try parsing inline HTML
        if let Ok((rest, node)) = parse_inline_html(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Try parsing hard line break (two spaces + newline, or backslash + newline)
        if let Ok((rest, node)) = parse_hard_line_break(remaining) {
            log::debug!(
                "Parsed hard line break at offset {}",
                remaining.location_offset()
            );
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Try parsing soft line break (regular newline)
        if let Ok((rest, node)) = parse_soft_line_break(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Try parsing entity references (e.g. &copy;, &#169;)
        if let Ok((rest, node)) = parse_entity_reference(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Try parsing emoji shortcodes (extended syntax), e.g. :joy:
        if let Ok((rest, node)) = parse_emoji_shortcode(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Try parsing platform mentions (extended syntax), e.g. @user[github](Name)
        if let Ok((rest, node)) = parse_platform_mention(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // No inline element matched - try parsing plain text
        if let Ok((rest, node)) = parse_text(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Special character that didn't parse as any inline element - consume as text
        if let Ok((rest, node)) = parse_special_as_text(remaining) {
            nodes.push(node);
            remaining = rest;
            continue;
        }

        // Safety check: if we reach here, we failed to parse anything
        // This should not happen if all parsers are working correctly
        log::error!(
            "Inline parser unable to make progress at offset {}",
            start_pos
        );
        break;
    }

    log::debug!("Parsed {} inline nodes", nodes.len());
    Ok(nodes)
}

fn intraword_underscore_run_len(nodes: &[Node], fragment: &str) -> Option<usize> {
    if !fragment.starts_with('_') {
        return None;
    }

    let prev = last_emitted_char(nodes)?;
    if !prev.is_alphanumeric() {
        return None;
    }

    let run_len = fragment.chars().take_while(|&c| c == '_').count();
    let after = fragment.chars().nth(run_len)?;
    if !after.is_alphanumeric() {
        return None;
    }

    Some(run_len)
}

fn is_task_checkbox_inline_start_boundary_ok(nodes: &[Node], fragment: &str) -> bool {
    if !fragment.starts_with('[') {
        return false;
    }

    // If the previous emitted character is alphanumeric/underscore, we do not
    // treat `[x]` / `[ ]` as a task marker (avoid matching `word[x]`).
    match last_emitted_char(nodes) {
        None => true,
        Some(prev) => !(prev.is_alphanumeric() || prev == '_'),
    }
}

fn last_emitted_char(nodes: &[Node]) -> Option<char> {
    nodes.iter().rev().find_map(last_char_in_node)
}

fn last_char_in_node(node: &Node) -> Option<char> {
    match &node.kind {
        NodeKind::Text(t) => t.chars().last(),
        // Formatting/container nodes: use their last child.
        _ => node.children.iter().rev().find_map(last_char_in_node),
    }
}

/// Parse inline elements within text content (backward compatibility wrapper)
/// Creates a new span at position 0:0 - USE parse_inlines_from_span() for position-aware parsing
/// Returns a vector of inline nodes (Text, Emphasis, Strong, Link, CodeSpan)
pub fn parse_inlines(text: &str) -> Result<Vec<Node>, Box<dyn std::error::Error>> {
    parse_inlines_from_span(GrammarSpan::new(text))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn smoke_test_triple_delimiter_parses_as_single_node() {
        let nodes = parse_inlines("***hi***").expect("inline parse failed");
        assert_eq!(nodes.len(), 1);
        assert!(matches!(
            nodes[0].kind,
            crate::parser::ast::NodeKind::StrongEmphasis
        ));
    }

    #[test]
    fn smoke_test_extension_inlines_parse_mid_line() {
        let nodes = parse_inlines(
            "This is ^sup^ and ~sub~ and ˅sub2˅ and ==mark== and ~~del~~ and --del2--.",
        )
        .expect("inline parse failed");

        use crate::parser::ast::NodeKind;

        assert!(nodes
            .iter()
            .any(|n| matches!(n.kind, NodeKind::Superscript)));
        assert!(nodes.iter().any(|n| matches!(n.kind, NodeKind::Subscript)));
        assert!(nodes.iter().any(|n| matches!(n.kind, NodeKind::Mark)));
        assert!(nodes
            .iter()
            .any(|n| matches!(n.kind, NodeKind::Strikethrough)));
    }
}