panache-parser 0.22.0

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
use super::helpers::{find_first, parse_blocks};
use crate::options::{Dialect, Extensions, Flavor, ParserOptions};
use crate::parser::Parser;
use crate::syntax::{SyntaxKind, SyntaxNode};

fn get_heading_content(node: &SyntaxNode) -> Option<String> {
    find_first(node, SyntaxKind::HEADING_CONTENT).map(|n| n.text().to_string())
}

#[test]
fn parses_simple_atx_heading() {
    let node = parse_blocks("# Heading\n");
    let content = get_heading_content(&node).unwrap();
    assert_eq!(content, "Heading");
}

#[test]
fn empty_atx_heading() {
    let node = parse_blocks("# \n");
    let content = get_heading_content(&node).unwrap();
    assert_eq!(content, "");
}

#[test]
fn parses_atx_heading_with_leading_spaces() {
    let node = parse_blocks("  # Leading spaces\n");
    let content = get_heading_content(&node).unwrap();
    assert_eq!(content, "Leading spaces");
}

#[test]
fn parses_atx_heading_with_multiple_hashes() {
    let node = parse_blocks("### Subheading\n");
    let content = get_heading_content(&node).unwrap();
    assert_eq!(content, "Subheading");
}

#[test]
fn parses_atx_heading_with_trailing_hashes() {
    let node = parse_blocks("### Foo Bar ###\n");
    let content = get_heading_content(&node).unwrap();
    assert_eq!(content, "Foo Bar");
}

#[test]
fn does_not_parse_with_four_leading_spaces() {
    let node = parse_blocks("    # Not a heading\n");
    assert!(find_first(&node, SyntaxKind::HEADING).is_none());
}

#[test]
fn requires_blank_line_before_heading() {
    let node = parse_blocks("text\n# Heading\n");
    assert!(find_first(&node, SyntaxKind::HEADING).is_none());
}

#[test]
fn parses_heading_after_horizontal_rule_without_blank_line() {
    let node = parse_blocks("---\n# Heading\n");
    let headings: Vec<_> = node
        .descendants()
        .filter(|n| n.kind() == SyntaxKind::HEADING)
        .collect();
    assert_eq!(headings.len(), 1);
}

#[test]
fn parses_heading_after_code_block_without_blank_line() {
    let node = parse_blocks("```r\nx\n```\n# Heading\n");
    let headings: Vec<_> = node
        .descendants()
        .filter(|n| n.kind() == SyntaxKind::HEADING)
        .collect();
    assert_eq!(headings.len(), 1);
}

#[test]
fn parses_empty_atx_heading_before_table_caption_table() {
    let input = "##\n\n: Example After One Iteration\n\n| Experiment | Heads | $\\gamma$ |\n|------------|-------|----------|\n| 1          |     7 |     0.78 |\n";
    let node = parse_blocks(input);
    let blocks: Vec<_> = node
        .children()
        .filter(|n| n.kind() != SyntaxKind::BLANK_LINE)
        .collect();
    assert_eq!(blocks.first().map(|n| n.kind()), Some(SyntaxKind::HEADING));
    assert_eq!(
        blocks.get(1).map(|n| n.kind()),
        Some(SyntaxKind::PIPE_TABLE)
    );
    assert!(
        find_first(&node, SyntaxKind::DEFINITION_LIST).is_none(),
        "empty ATX heading should not be parsed as a definition-list term"
    );
}

#[test]
fn parses_heading_without_blank_line_when_extension_disabled() {
    let mut config = ParserOptions::default();
    config.extensions.blank_before_header = false;
    let input = "Text\n# Heading\nMore\n";
    let node = Parser::new(input, &config).parse();
    let blocks: Vec<_> = node.children().map(|node| node.kind()).collect();

    assert_eq!(
        node.text().to_string(),
        input,
        "parser must remain lossless"
    );
    assert_eq!(
        blocks,
        vec![
            SyntaxKind::PARAGRAPH,
            SyntaxKind::HEADING,
            SyntaxKind::PARAGRAPH
        ]
    );
}

#[test]
fn atx_interrupts_lazy_blockquote_line_when_extension_disabled() {
    // pandoc -f markdown-blank_before_header: BlockQuote [Para "para"],
    // Header 1 "head" — the heading ends the quote instead of being
    // swallowed as lazy continuation text (issue #428).
    let mut config = ParserOptions::default();
    config.extensions.blank_before_header = false;
    let input = "> para\n# head\n";
    let node = Parser::new(input, &config).parse();

    assert_eq!(
        node.text().to_string(),
        input,
        "parser must remain lossless"
    );
    assert_eq!(
        node.children().map(|node| node.kind()).collect::<Vec<_>>(),
        vec![SyntaxKind::BLOCK_QUOTE, SyntaxKind::HEADING]
    );
}

#[test]
fn atx_interrupts_lazy_blockquote_line_commonmark() {
    // pandoc -f commonmark agrees: an ATX heading is never paragraph
    // continuation text, so the lazy line ends the quote (CommonMark §5.1).
    let config = ParserOptions {
        flavor: Flavor::CommonMark,
        dialect: Dialect::for_flavor(Flavor::CommonMark),
        extensions: Extensions::for_flavor(Flavor::CommonMark),
        ..Default::default()
    };
    let input = "> para\n# head\n";
    let node = Parser::new(input, &config).parse();

    assert_eq!(
        node.text().to_string(),
        input,
        "parser must remain lossless"
    );
    assert_eq!(
        node.children().map(|node| node.kind()).collect::<Vec<_>>(),
        vec![SyntaxKind::BLOCK_QUOTE, SyntaxKind::HEADING]
    );
}

#[test]
fn atx_on_lazy_blockquote_line_stays_text_by_default() {
    // Default pandoc (blank_before_header on) keeps the heading-shaped line
    // as lazy paragraph text: BlockQuote [Para "para SoftBreak # head"].
    let config = ParserOptions::default();
    let input = "> para\n# head\n";
    let node = Parser::new(input, &config).parse();

    assert_eq!(
        node.text().to_string(),
        input,
        "parser must remain lossless"
    );
    assert_eq!(
        node.children().map(|node| node.kind()).collect::<Vec<_>>(),
        vec![SyntaxKind::BLOCK_QUOTE]
    );
    assert!(
        node.descendants().all(|n| n.kind() != SyntaxKind::HEADING),
        "heading-shaped lazy line must stay paragraph text by default"
    );
}

#[test]
fn atx_interrupts_lazy_blockquote_list_when_extension_disabled() {
    // pandoc -f markdown-blank_before_header: BlockQuote [BulletList],
    // Header 1 "head" — same interruption for a list item's lazy line.
    let mut config = ParserOptions::default();
    config.extensions.blank_before_header = false;
    let input = "> - item\n# head\n";
    let node = Parser::new(input, &config).parse();

    assert_eq!(
        node.text().to_string(),
        input,
        "parser must remain lossless"
    );
    assert_eq!(
        node.children().map(|node| node.kind()).collect::<Vec<_>>(),
        vec![SyntaxKind::BLOCK_QUOTE, SyntaxKind::HEADING]
    );
}

#[test]
fn atx_interrupts_reduced_marker_lazy_line_commonmark() {
    // pandoc -f commonmark: BlockQuote [BlockQuote [Para "para"], Header 1] —
    // a `> # head` line under an open depth-2 quote is not lazy continuation;
    // the inner quote closes and the heading forms in the outer quote
    // (issue #429).
    let config = ParserOptions {
        flavor: Flavor::CommonMark,
        dialect: Dialect::for_flavor(Flavor::CommonMark),
        extensions: Extensions::for_flavor(Flavor::CommonMark),
        ..Default::default()
    };
    let input = ">> para\n> # head\n";
    let node = Parser::new(input, &config).parse();

    assert_eq!(
        node.text().to_string(),
        input,
        "parser must remain lossless"
    );
    assert_eq!(
        node.children().map(|node| node.kind()).collect::<Vec<_>>(),
        vec![SyntaxKind::BLOCK_QUOTE]
    );
    let outer = node.children().next().unwrap();
    assert_eq!(
        outer.children().map(|node| node.kind()).collect::<Vec<_>>(),
        vec![SyntaxKind::BLOCK_QUOTE, SyntaxKind::HEADING]
    );
}

#[test]
fn atx_interrupts_reduced_marker_lazy_line_when_extension_disabled() {
    // pandoc -f markdown-blank_before_header agrees with commonmark on the
    // reduced-marker shape: BlockQuote [BlockQuote [Para], Header 1].
    let mut config = ParserOptions::default();
    config.extensions.blank_before_header = false;
    let input = ">> para\n> # head\n";
    let node = Parser::new(input, &config).parse();

    assert_eq!(
        node.text().to_string(),
        input,
        "parser must remain lossless"
    );
    assert_eq!(
        node.children().map(|node| node.kind()).collect::<Vec<_>>(),
        vec![SyntaxKind::BLOCK_QUOTE]
    );
    let outer = node.children().next().unwrap();
    assert_eq!(
        outer.children().map(|node| node.kind()).collect::<Vec<_>>(),
        vec![SyntaxKind::BLOCK_QUOTE, SyntaxKind::HEADING]
    );
}

#[test]
fn atx_on_reduced_marker_lazy_line_stays_text_by_default() {
    // Default pandoc (blank_before_header on) keeps the reduced-marker
    // heading-shaped line as lazy paragraph text, same as the zero-marker
    // form: BlockQuote [BlockQuote [Para "para SoftBreak # head"]].
    let config = ParserOptions::default();
    let input = ">> para\n> # head\n";
    let node = Parser::new(input, &config).parse();

    assert_eq!(
        node.text().to_string(),
        input,
        "parser must remain lossless"
    );
    assert!(
        node.descendants().all(|n| n.kind() != SyntaxKind::HEADING),
        "heading-shaped reduced-marker lazy line must stay paragraph text by default"
    );
}

#[test]
fn atx_interrupts_reduced_marker_lazy_line_depth3() {
    // pandoc -f commonmark on `>>> para\n>> # head`: the heading forms at
    // the two-marker level — BlockQuote [BlockQuote [BlockQuote [Para],
    // Header 1]].
    let config = ParserOptions {
        flavor: Flavor::CommonMark,
        dialect: Dialect::for_flavor(Flavor::CommonMark),
        extensions: Extensions::for_flavor(Flavor::CommonMark),
        ..Default::default()
    };
    let input = ">>> para\n>> # head\n";
    let node = Parser::new(input, &config).parse();

    assert_eq!(
        node.text().to_string(),
        input,
        "parser must remain lossless"
    );
    let outer = node.children().next().unwrap();
    assert_eq!(outer.kind(), SyntaxKind::BLOCK_QUOTE);
    let middle = outer.children().next().unwrap();
    assert_eq!(
        middle
            .children()
            .map(|node| node.kind())
            .collect::<Vec<_>>(),
        vec![SyntaxKind::BLOCK_QUOTE, SyntaxKind::HEADING]
    );
}

#[test]
fn atx_interrupts_reduced_marker_lazy_list_when_extension_disabled() {
    // pandoc -f markdown-blank_before_header: BlockQuote [BlockQuote
    // [BulletList], Header 1] — the reduced-marker heading line also ends a
    // list item's lazy continuation (issue #429).
    let mut config = ParserOptions::default();
    config.extensions.blank_before_header = false;
    let input = ">> - item\n> # head\n";
    let node = Parser::new(input, &config).parse();

    assert_eq!(
        node.text().to_string(),
        input,
        "parser must remain lossless"
    );
    assert_eq!(
        node.children().map(|node| node.kind()).collect::<Vec<_>>(),
        vec![SyntaxKind::BLOCK_QUOTE]
    );
    let outer = node.children().next().unwrap();
    assert_eq!(
        outer.children().map(|node| node.kind()).collect::<Vec<_>>(),
        vec![SyntaxKind::BLOCK_QUOTE, SyntaxKind::HEADING]
    );
}

#[test]
fn setext_underline_mid_paragraph_stays_text_when_extension_disabled() {
    // Pandoc never forms a setext heading mid-paragraph, even with
    // `blank_before_header` disabled: `markdown-blank_before_header` keeps
    // `Text\nTitle\n-----\nMore` a single Para. Only ATX interrupts.
    let mut config = ParserOptions::default();
    config.extensions.blank_before_header = false;
    let input = "Text\nTitle\n-----\nMore\n";
    let node = Parser::new(input, &config).parse();

    assert_eq!(
        node.text().to_string(),
        input,
        "parser must remain lossless"
    );
    assert_eq!(
        node.children().map(|node| node.kind()).collect::<Vec<_>>(),
        vec![SyntaxKind::PARAGRAPH]
    );
    assert!(
        node.descendants().all(|n| n.kind() != SyntaxKind::HEADING),
        "setext underline after paragraph text must not form a heading"
    );
}

#[test]
fn setext_heading_at_document_start_when_extension_disabled() {
    let mut config = ParserOptions::default();
    config.extensions.blank_before_header = false;
    let input = "Title\n=====\n";
    let node = Parser::new(input, &config).parse();

    assert_eq!(node.text().to_string(), input);
    assert_eq!(
        node.children().map(|node| node.kind()).collect::<Vec<_>>(),
        vec![SyntaxKind::HEADING]
    );
}

#[test]
fn parses_heading_at_start_of_document() {
    let node = parse_blocks("# Start\n");
    let content = get_heading_content(&node).unwrap();
    assert_eq!(content, "Start");
}

#[test]
fn parses_multiple_headings() {
    let node = parse_blocks("# First\n\n## Second\n");
    let mut headings = node
        .descendants()
        .filter(|n| n.kind() == SyntaxKind::HEADING_CONTENT);
    assert_eq!(headings.next().unwrap().text(), "First");
    assert_eq!(headings.next().unwrap().text(), "Second");
}

#[test]
fn parses_mmd_header_identifier_in_atx_when_enabled() {
    let mut config = ParserOptions::default();
    config.extensions.mmd_header_identifiers = true;
    let node = Parser::new("# Heading [my id]\n", &config).parse();

    let heading = find_first(&node, SyntaxKind::HEADING).expect("heading");
    let attr = heading
        .children()
        .find(|n| n.kind() == SyntaxKind::ATTRIBUTE)
        .expect("attribute");
    assert_eq!(attr.text().to_string(), "[my id]");
}

#[test]
fn does_not_parse_mmd_header_identifier_in_atx_when_disabled() {
    let mut config = ParserOptions::default();
    config.extensions.mmd_header_identifiers = false;
    let node = Parser::new("# Heading [my id]\n", &config).parse();

    let heading = find_first(&node, SyntaxKind::HEADING).expect("heading");
    assert!(
        heading
            .children()
            .all(|n| n.kind() != SyntaxKind::ATTRIBUTE),
        "mmd_header_identifiers disabled should keep [my id] in heading content"
    );
}

#[test]
fn parses_mmd_header_identifier_in_setext_when_enabled() {
    let mut config = ParserOptions::default();
    config.extensions.mmd_header_identifiers = true;
    let node = Parser::new("Heading [setext id]\n---\n", &config).parse();

    let heading = find_first(&node, SyntaxKind::HEADING).expect("heading");
    let attr = heading
        .children()
        .find(|n| n.kind() == SyntaxKind::ATTRIBUTE)
        .expect("attribute");
    assert_eq!(attr.text().to_string(), "[setext id]");
}

#[test]
fn atx_heading_immediately_after_yaml_frontmatter() {
    // Pandoc allows a heading directly after YAML frontmatter without a blank line.
    let input = "---\ntitle: Test\n---\n# Heading\n";
    let node = Parser::new(input, &ParserOptions::default()).parse();
    assert!(
        find_first(&node, SyntaxKind::HEADING).is_some(),
        "heading directly after YAML frontmatter should be parsed as a heading"
    );
}

#[test]
fn atx_heading_with_id_immediately_after_yaml_frontmatter() {
    // Heading IDs must be extractable when heading follows YAML directly.
    let input = "---\ntitle: Test\n---\n# One {#one}\n";
    let node = Parser::new(input, &ParserOptions::default()).parse();
    assert!(
        find_first(&node, SyntaxKind::HEADING).is_some(),
        "heading with ID directly after YAML frontmatter should be parsed as a heading"
    );
}

#[test]
fn parses_mmd_header_identifier_before_atx_closing_hashes() {
    let mut config = ParserOptions::default();
    config.extensions.mmd_header_identifiers = true;
    let input = "## Title [my id] ###\n";
    let node = Parser::new(input, &config).parse();

    let heading = find_first(&node, SyntaxKind::HEADING).expect("heading");
    let attr = heading
        .children()
        .find(|n| n.kind() == SyntaxKind::ATTRIBUTE)
        .expect("attribute");
    assert_eq!(attr.text().to_string(), "[my id]");
    assert_eq!(node.text().to_string(), input);
}