panache-parser 0.5.1

Lossless CST parser and syntax wrappers for Pandoc markdown, Quarto, and RMarkdown
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! Unit tests for emphasis parser.
//!
//! These tests verify CST structure directly, focusing on parser correctness
//! rather than formatter output. Each test parses input and inspects the
//! resulting syntax tree to ensure emphasis is parsed correctly.

use panache_parser::{parse, syntax::SyntaxKind};

/// Helper to get all nodes/tokens of a specific kind from a syntax tree.
fn find_nodes(
    tree: &panache_parser::SyntaxNode,
    kind: SyntaxKind,
) -> Vec<panache_parser::SyntaxNode> {
    tree.descendants_with_tokens()
        .filter_map(|element| {
            if element.kind() == kind {
                // Try to convert to node; if it's a token, we skip it for Vec<SyntaxNode>
                match element {
                    rowan::NodeOrToken::Node(n) => Some(n),
                    rowan::NodeOrToken::Token(_) => None,
                }
            } else {
                None
            }
        })
        .collect()
}

/// Helper to count nodes OR tokens of a specific kind.
fn count_elements(tree: &panache_parser::SyntaxNode, kind: SyntaxKind) -> usize {
    tree.descendants_with_tokens()
        .filter(|element| element.kind() == kind)
        .count()
}

/// Helper to check if a node contains a specific child kind (node or token).
fn has_child(node: &panache_parser::SyntaxNode, kind: SyntaxKind) -> bool {
    node.children_with_tokens()
        .any(|child| child.kind() == kind)
}

/// Helper to count nodes of a specific kind.
fn count_nodes(tree: &panache_parser::SyntaxNode, kind: SyntaxKind) -> usize {
    find_nodes(tree, kind).len()
}

// =============================================================================
// Critical Cases: Nested Inline Elements
// =============================================================================
// These are the "killer test cases" that require proper position tracking
// to avoid matching delimiters inside code spans, math, etc.

#[test]
fn code_span_in_emphasis() {
    let input = "*text `code here` end*\n";
    let tree = parse(input, None);

    // Should have: EMPHASIS containing CODE_SPAN
    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(
        emphasis_nodes.len(),
        1,
        "Should parse exactly one emphasis node"
    );

    let emphasis = &emphasis_nodes[0];
    assert!(
        has_child(emphasis, SyntaxKind::INLINE_CODE),
        "Emphasis should contain code span as child"
    );
}

#[test]
fn code_span_with_asterisk_in_emphasis() {
    // The asterisk inside the code span should NOT close the emphasis
    let input = "*text `code * here` end*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(
        emphasis_nodes.len(),
        1,
        "Should parse exactly one emphasis node"
    );

    let emphasis = &emphasis_nodes[0];
    assert!(
        has_child(emphasis, SyntaxKind::INLINE_CODE),
        "Emphasis should contain code span"
    );

    // Verify the code span content includes the asterisk
    let code_spans = find_nodes(&tree, SyntaxKind::INLINE_CODE);
    assert_eq!(code_spans.len(), 1, "Should have exactly one code span");
}

#[test]
fn math_in_emphasis() {
    let input = "*text $math$ end*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(
        emphasis_nodes.len(),
        1,
        "Should parse exactly one emphasis node"
    );

    let emphasis = &emphasis_nodes[0];
    assert!(
        has_child(emphasis, SyntaxKind::INLINE_MATH),
        "Emphasis should contain inline math"
    );
}

#[test]
fn math_with_asterisk_in_emphasis() {
    // The asterisk inside math should NOT close the emphasis
    let input = "*text $a * b$ end*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(
        emphasis_nodes.len(),
        1,
        "Should parse exactly one emphasis node"
    );

    let emphasis = &emphasis_nodes[0];
    assert!(
        has_child(emphasis, SyntaxKind::INLINE_MATH),
        "Emphasis should contain inline math"
    );
}

#[test]
fn link_in_emphasis() {
    let input = "*text [link](url) end*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(
        emphasis_nodes.len(),
        1,
        "Should parse exactly one emphasis node"
    );

    let emphasis = &emphasis_nodes[0];
    assert!(
        has_child(emphasis, SyntaxKind::LINK),
        "Emphasis should contain link"
    );
}

#[test]
fn link_with_asterisk_in_emphasis() {
    // The asterisk in link text should NOT close the emphasis
    let input = "*text [link * here](url) end*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(
        emphasis_nodes.len(),
        1,
        "Should parse exactly one emphasis node"
    );

    let emphasis = &emphasis_nodes[0];
    assert!(
        has_child(emphasis, SyntaxKind::LINK),
        "Emphasis should contain link"
    );
}

#[test]
fn image_in_emphasis() {
    let input = "*text ![alt](img.png) end*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(
        emphasis_nodes.len(),
        1,
        "Should parse exactly one emphasis node"
    );

    let emphasis = &emphasis_nodes[0];
    assert!(
        has_child(emphasis, SyntaxKind::IMAGE_LINK),
        "Emphasis should contain image"
    );
}

#[test]
fn strong_with_code_span() {
    let input = "**text `code ** here` end**\n";
    let tree = parse(input, None);

    let strong_nodes = find_nodes(&tree, SyntaxKind::STRONG);
    assert_eq!(
        strong_nodes.len(),
        1,
        "Should parse exactly one strong node"
    );

    let strong = &strong_nodes[0];
    assert!(
        has_child(strong, SyntaxKind::INLINE_CODE),
        "Strong should contain code span"
    );
}

#[test]
fn complex_nesting() {
    // Multiple inline elements in emphasis
    let input = "*em with `code`, [link](url), and text*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(
        emphasis_nodes.len(),
        1,
        "Should parse exactly one emphasis node"
    );

    let emphasis = &emphasis_nodes[0];
    assert!(
        has_child(emphasis, SyntaxKind::INLINE_CODE),
        "Should contain code span"
    );
    assert!(has_child(emphasis, SyntaxKind::LINK), "Should contain link");
}

// =============================================================================
// Rule of 3s (Pandoc's delimiter consumption strategy)
// =============================================================================

#[test]
fn three_opener_two_closer() {
    // ***foo** should produce: literal * + Strong("foo")
    let input = "***foo**\n";
    let tree = parse(input, None);

    // Should have one STRONG node
    let strong_nodes = find_nodes(&tree, SyntaxKind::STRONG);
    assert_eq!(
        strong_nodes.len(),
        1,
        "Should parse exactly one strong node"
    );

    // The first * should be a literal (TEXT or escaped)
    // This is a known current bug - documenting expected behavior
}

#[test]
fn triple_matched() {
    // ***foo*** should produce: StrongEmph("foo")
    let input = "***foo***\n";
    let tree = parse(input, None);

    // Should have nested STRONG and EMPHASIS
    let strong_nodes = find_nodes(&tree, SyntaxKind::STRONG);
    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);

    assert!(
        !strong_nodes.is_empty() || !emphasis_nodes.is_empty(),
        "Should parse emphasis/strong from triple delimiters"
    );
}

#[test]
fn four_or_more_delimiters_literal() {
    // ****foo**** should be literal (Pandoc doesn't recognize 4+)
    let input = "****foo****\n";
    let tree = parse(input, None);

    // Should NOT create emphasis or strong nodes
    let _emphasis_count = count_nodes(&tree, SyntaxKind::EMPHASIS);
    let _strong_count = count_nodes(&tree, SyntaxKind::STRONG);

    // Expecting this to remain literal - current parser may differ
    // This documents the Pandoc-compliant behavior we want
}

// =============================================================================
// Overlapping Delimiters
// =============================================================================

#[test]
fn overlapping_emphasis_strong() {
    // *foo **bar* baz** should produce: literal "*foo " + Strong("bar* baz")
    // The first * can't close because of wrong nesting, so ** opens strong
    let input = "*foo **bar* baz**\n";
    let tree = parse(input, None);

    // Should have one STRONG node for "bar* baz"
    let strong_nodes = find_nodes(&tree, SyntaxKind::STRONG);
    assert!(
        !strong_nodes.is_empty(),
        "Should parse at least one strong node"
    );
}

#[test]
fn overlapping_strong_emphasis() {
    // **foo *bar** baz* - Pandoc produces ALL literal text (no emphasis or strong)
    // The ** at start can't find a valid closer because:
    // - The * at pos 6 starts emphasis that consumes the trailing *
    // - No ** closer remains for the outer strong
    let input = "**foo *bar** baz*\n";
    let tree = parse(input, None);

    // Pandoc: no strong or emphasis - all literal
    // We accept either Pandoc-exact (no strong) or a reasonable interpretation
    let strong_nodes = find_nodes(&tree, SyntaxKind::STRONG);
    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);

    // Verify the text is preserved (lossless)
    assert_eq!(tree.text().to_string(), input, "Should be lossless");

    // Note: Pandoc produces all literal. If we produce emphasis, that's acceptable
    // as long as we're lossless. The key is we don't crash or produce invalid trees.
    let _ = (strong_nodes, emphasis_nodes);
}

// =============================================================================
// Adjacent Patterns
// =============================================================================

#[test]
fn adjacent_emphasis() {
    // *foo**bar* - In Pandoc, when parsing `*`, encountering `**` tries to parse
    // as strong. When strong fails (no ** closer), the entire `*` parse fails.
    // Result: all literal text, no emphasis nodes.
    let input = "*foo**bar*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    // Per Pandoc: should parse as literal text, no emphasis
    assert!(
        emphasis_nodes.is_empty(),
        "Adjacent ** should prevent * from matching"
    );
}

#[test]
fn adjacent_strong() {
    // **foo****bar** should produce: Strong("foo") + Strong("bar")
    // (merged in AST but separate in CST)
    let input = "**foo****bar**\n";
    let tree = parse(input, None);

    let strong_nodes = find_nodes(&tree, SyntaxKind::STRONG);
    assert!(!strong_nodes.is_empty(), "Should parse strong emphasis");
}

// =============================================================================
// Flanking Rules
// =============================================================================

#[test]
fn intraword_asterisk() {
    // un*frigging*believable - asterisks CAN work intraword
    let input = "un*frigging*believable\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(
        emphasis_nodes.len(),
        1,
        "Should parse intraword emphasis with asterisks"
    );
}

#[test]
fn intraword_underscore_disabled() {
    // feas_ible - underscores should NOT work intraword (default config)
    let input = "feas_ible\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(
        emphasis_nodes.len(),
        0,
        "Should not parse intraword emphasis with underscores"
    );
}

#[test]
fn whitespace_flanking_opener() {
    // "* foo*" - opener has trailing space, should not match
    let input = "* foo*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    // Should NOT parse as emphasis (opener not left-flanking)
    assert_eq!(
        emphasis_nodes.len(),
        0,
        "Should not parse emphasis with space after opener"
    );
}

#[test]
fn whitespace_flanking_closer() {
    // "*foo *" - Pandoc DOES parse this as emphasis (asterisk closers don't require right-flanking)
    // See Markdown.hs ender function: for asterisks, only underscore closers need right-flanking
    let input = "*foo *\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    // Should parse as emphasis (Pandoc behavior)
    assert_eq!(
        emphasis_nodes.len(),
        1,
        "Asterisk emphasis should allow space before closer (Pandoc behavior)"
    );
}

// =============================================================================
// Escapes
// =============================================================================

#[test]
fn escaped_opener() {
    // \*foo* should not create emphasis
    let input = "\\*foo*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(
        emphasis_nodes.len(),
        0,
        "Should not parse emphasis when opener is escaped"
    );

    // Should have an ESCAPED_CHAR token
    let escape_count = count_elements(&tree, SyntaxKind::ESCAPED_CHAR);
    assert!(escape_count >= 1, "Should have escape node");
}

#[test]
fn escaped_closer() {
    // *foo\* should not create emphasis
    let input = "*foo\\*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(
        emphasis_nodes.len(),
        0,
        "Should not parse emphasis when closer is escaped"
    );
}

#[test]
fn escaped_within_emphasis() {
    // *foo \* bar* should create emphasis with escaped asterisk inside
    let input = "*foo \\* bar*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(
        emphasis_nodes.len(),
        1,
        "Should parse emphasis with escape inside"
    );

    let emphasis = &emphasis_nodes[0];
    assert!(
        has_child(emphasis, SyntaxKind::ESCAPED_CHAR),
        "Emphasis should contain escape node"
    );
}

// =============================================================================
// Unclosed Constructs
// =============================================================================

#[test]
fn unclosed_code_in_emphasis() {
    // *text `unclosed code end*
    // When code span fails to close, backtick becomes literal,
    // and * could be a valid closer candidate
    let input = "*text `unclosed code end*\n";
    let _tree = parse(input, None);

    // Current behavior may vary - documenting that this is an edge case
    // Pandoc would parse this as emphasis with literal backtick inside
}

#[test]
fn unclosed_emphasis() {
    // *foo - no closing delimiter
    let input = "*foo\n";
    let tree = parse(input, None);

    // Should NOT create emphasis node (no closer)
    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(
        emphasis_nodes.len(),
        0,
        "Should not parse unclosed emphasis"
    );
}

#[test]
fn unclosed_strong() {
    // **foo - no closing delimiter
    let input = "**foo\n";
    let tree = parse(input, None);

    // Should NOT create strong node (no closer)
    let strong_nodes = find_nodes(&tree, SyntaxKind::STRONG);
    assert_eq!(strong_nodes.len(), 0, "Should not parse unclosed strong");
}

// =============================================================================
// Cross-delimiter Interaction
// =============================================================================

#[test]
fn emphasis_in_strikeout() {
    let input = "~~strike *em* text~~\n";
    let tree = parse(input, None);

    let strikeout_nodes = find_nodes(&tree, SyntaxKind::STRIKEOUT);
    assert_eq!(strikeout_nodes.len(), 1, "Should parse strikeout");

    let strikeout = &strikeout_nodes[0];
    assert!(
        has_child(strikeout, SyntaxKind::EMPHASIS),
        "Strikeout should contain emphasis"
    );
}

#[test]
fn strikeout_in_emphasis() {
    let input = "*em ~~strike~~ text*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(emphasis_nodes.len(), 1, "Should parse emphasis");

    let emphasis = &emphasis_nodes[0];
    assert!(
        has_child(emphasis, SyntaxKind::STRIKEOUT),
        "Emphasis should contain strikeout"
    );
}

#[test]
fn subscript_in_emphasis() {
    let input = "*em ~sub~ text*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(emphasis_nodes.len(), 1, "Should parse emphasis");

    let emphasis = &emphasis_nodes[0];
    assert!(
        has_child(emphasis, SyntaxKind::SUBSCRIPT),
        "Emphasis should contain subscript"
    );
}

#[test]
fn superscript_in_emphasis() {
    let input = "*em ^super^ text*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(emphasis_nodes.len(), 1, "Should parse emphasis");

    let emphasis = &emphasis_nodes[0];
    assert!(
        has_child(emphasis, SyntaxKind::SUPERSCRIPT),
        "Emphasis should contain superscript"
    );
}

// =============================================================================
// Empty Emphasis
// =============================================================================

#[test]
fn empty_emphasis() {
    // ** alone should be literal
    let input = "**\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    let strong_nodes = find_nodes(&tree, SyntaxKind::STRONG);

    assert_eq!(emphasis_nodes.len(), 0, "Should not parse empty emphasis");
    assert_eq!(strong_nodes.len(), 0, "Should not parse empty strong");
}

#[test]
fn emphasis_only_code() {
    // *`code`* - emphasis containing only code span
    let input = "*`code`*\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);
    assert_eq!(
        emphasis_nodes.len(),
        1,
        "Should parse emphasis with only code"
    );

    let emphasis = &emphasis_nodes[0];
    assert!(
        has_child(emphasis, SyntaxKind::INLINE_CODE),
        "Emphasis should contain code span"
    );
}

// =============================================================================
// Losslessness Tests
// =============================================================================

#[test]
fn lossless_simple_emphasis() {
    let input = "*foo*\n";
    let tree = parse(input, None);
    let output = tree.to_string();

    assert_eq!(
        input, output,
        "Parser should be lossless for simple emphasis"
    );
}

#[test]
fn lossless_triple_delimiters() {
    let input = "***foo**\n";
    let tree = parse(input, None);
    let output = tree.to_string();

    assert_eq!(
        input, output,
        "Parser should preserve all bytes for mismatched delimiters"
    );
}

#[test]
fn lossless_with_nested_code() {
    let input = "*text `code * here` end*\n";
    let tree = parse(input, None);
    let output = tree.to_string();

    assert_eq!(input, output, "Parser should be lossless with nested code");
}

#[test]
fn lossless_unclosed() {
    let input = "*foo\n";
    let tree = parse(input, None);
    let output = tree.to_string();

    assert_eq!(
        input, output,
        "Parser should be lossless for unclosed emphasis"
    );
}

// =============================================================================
// Pandoc-Exact Tests (Strict Compliance)
// =============================================================================
// These tests require exact Pandoc behavior, not just losslessness.

#[test]
fn pandoc_overlapping_strong_emphasis_all_literal() {
    // **foo *bar** baz* - Pandoc produces ALL literal text (no emphasis or strong)
    // The ** opener can't find a valid ** closer because the * at position 6
    // would create improper nesting. Unlike our lenient test above, this one
    // requires Pandoc-exact behavior.
    //
    // Pandoc output: [ Str "**foo" , Space , Str "*bar**" , Space , Str "baz*" ]
    let input = "**foo *bar** baz*\n";
    let tree = parse(input, None);

    let strong_nodes = find_nodes(&tree, SyntaxKind::STRONG);
    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);

    assert_eq!(
        strong_nodes.len(),
        0,
        "Pandoc: **foo *bar** baz* should have NO strong nodes (all literal)"
    );
    assert_eq!(
        emphasis_nodes.len(),
        0,
        "Pandoc: **foo *bar** baz* should have NO emphasis nodes (all literal)"
    );
}

#[test]
fn pandoc_escaped_star_then_emphasis() {
    // \**not bold\** - Pandoc produces: * (literal) + Emph["not bold*"]
    // The first \* is an escaped asterisk (literal *), leaving *not bold\**
    // The second * opens emphasis, \* inside is escaped, final * closes emphasis.
    //
    // Pandoc output: [ Str "*" , Emph [ Str "not" , Space , Str "bold*" ] ]
    let input = "\\**not bold\\**\n";
    let tree = parse(input, None);

    let emphasis_nodes = find_nodes(&tree, SyntaxKind::EMPHASIS);

    assert_eq!(
        emphasis_nodes.len(),
        1,
        "Pandoc: \\**not bold\\** should produce exactly ONE emphasis node"
    );

    // The emphasis content should include "not bold" and the escaped *
    let emphasis = &emphasis_nodes[0];
    let emph_text = emphasis.text().to_string();
    assert!(
        emph_text.contains("not bold"),
        "Emphasis should contain 'not bold', got: {}",
        emph_text
    );
}