mordant 0.10.0

A 100% CommonMark-compatible GitHub Flavored Markdown parser and renderer
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
//! YAML frontmatter parser extension for mordant.
//!
//! This module provides a parser extension that extracts YAML frontmatter
//! from markdown documents and stores it as metadata on the Document node.
//!
//! Based on the mordant-meta crate by Yusuke Inuzuka.

use std::boxed::Box;
use std::format;
use std::rc::Rc;
use std::string::String;
use std::string::ToString;
use std::vec::Vec;
use std::cell::RefCell;
use std::result::Result as CoreResult;

use crate::ast::{Arena, Meta, NodeRef};
use crate::context::{ContextKey, ContextKeyRegistry, NodeRefValue};
use crate::parser::{
    self, AnyAstTransformer, AnyBlockParser, AstTransformer, BlockParser, NoParserOptions,
    Parser, ParserExtension, ParserExtensionFn, PRIORITY_SETTEXT_HEADING,
};
use crate::text::Reader;
use crate::util::StringMap;

const META_NODE: &str = "mordant-meta-n";

/// Options for the meta parser.
#[derive(Debug, Clone, Default)]
pub struct MetaParserOptions {
    /// Convert the meta data to a table node in the AST.
    pub table: bool,
}

impl parser::ParserOptions for MetaParserOptions {}

// --- Meta value helpers ---

fn format_meta_value(meta: &Meta) -> String {
    match meta {
        Meta::Null => "null".to_string(),
        Meta::Bool(b) => b.to_string(),
        Meta::Int(i) => i.to_string(),
        Meta::Float(f) => f.to_string(),
        Meta::String(s) => s.clone(),
        Meta::Sequence(seq) => {
            let items: Vec<String> = seq.iter().map(format_meta_value).collect();
            format!("[{}]", items.join(", "))
        }
        Meta::Mapping(map) => {
            let items: Vec<String> = map
                .iter()
                .map(|(k, v)| format!("{}: {}", k, format_meta_value(v)))
                .collect();
            format!("{{{}}}", items.join(", "))
        }
    }
}

// --- YAML parsing via yaml-peg ---

fn to_meta<R: yaml_peg::repr::Repr>(node: &yaml_peg::Node<R>) -> Meta {
    match node.yaml() {
        yaml_peg::Yaml::Null => Meta::Null,
        yaml_peg::Yaml::Bool(b) => Meta::Bool(*b),
        yaml_peg::Yaml::Int(s) => Meta::Int(s.parse().unwrap_or(0)),
        yaml_peg::Yaml::Float(s) => Meta::Float(s.parse().unwrap_or(0.0)),
        yaml_peg::Yaml::Str(s) => Meta::String(s.clone()),
        yaml_peg::Yaml::Seq(seq) => Meta::Sequence(seq.iter().map(|n| to_meta(n)).collect()),
        yaml_peg::Yaml::Map(map) => {
            let mut result = StringMap::with_capacity(map.len());
            for (k, v) in map.iter() {
                if let yaml_peg::Yaml::Str(key) = k.yaml() {
                    result.insert(key.clone(), to_meta(v));
                }
            }
            Meta::Mapping(result)
        }
        yaml_peg::Yaml::Alias(_) => Meta::Null, // Aliases not supported
    }
}

fn parse_yaml(input: &str) -> CoreResult<Meta, String> {
    let doc = yaml_peg::parser::parse::<yaml_peg::repr::RcRepr>(input)
        .map_err(|e| format!("YAML parsing error: {:?}", e))?;
    if !doc.is_empty() {
        Ok(to_meta(&doc[0]))
    } else {
        Err("YAML document is empty".to_string())
    }
}

// --- Block parser ---

#[derive(Debug)]
struct MetaParser {
    meta_node: ContextKey<NodeRefValue>,
}

impl MetaParser {
    pub fn new(reg: Rc<RefCell<ContextKeyRegistry>>) -> Self {
        let meta_node = reg.borrow_mut().get_or_create::<NodeRefValue>(META_NODE);
        Self { meta_node }
    }
}

impl BlockParser for MetaParser {
    fn trigger(&self) -> &[u8] {
        b"-"
    }

    fn open(
        &self,
        arena: &mut Arena,
        _parent_ref: NodeRef,
        reader: &mut crate::text::BasicReader,
        ctx: &mut parser::Context,
    ) -> Option<(NodeRef, parser::State)> {
        let (line, _) = reader.position();
        if line != 0 {
            return None;
        }
        let (line_bytes, seg) = reader.peek_line_bytes()?;
        if !line_bytes.starts_with(b"---") {
            return None;
        }

        // Valid frontmatter must be exactly `---` followed by a newline.
        // `-----` (5 dashes) is a thematic break, not frontmatter.
        // `---` without a newline is also not frontmatter.
        let has_newline = line_bytes.iter().any(|&b| b == b'\n');
        if !has_newline {
            return None; // No newline after `---` - not frontmatter
        }

        // Check what comes after the newline. The line after `---` must have
        // actual content (not empty/whitespace-only, not another `---`).
        // Additionally, the content should look like YAML (contain colons,
        // list markers, or block scalars) to avoid consuming setext headings
        // like `---\nFoo\n---` which should be thematic break + heading.
        let source = reader.source();
        let after_line_pos = seg.stop();
        if after_line_pos >= source.len() {
            return None; // `---\n` at EOF - thematic break
        }
        let remaining = &source[after_line_pos..];

        // Check the first line of remaining content
        if let Some(first_nl) = remaining.find('\n') {
            let first_line = &remaining[..first_nl];
            // If the first line is empty/whitespace-only or is just `---`, it's a thematic break
            if first_line.trim().is_empty() || first_line.trim() == "---" {
                return None;
            }
            // Only claim as frontmatter if the content looks like YAML:
            // - Contains a colon (key: value)
            // - Starts with "- " (list item)
            // - Starts with "|" or ">" (block scalar)
            let trimmed = first_line.trim();
            let looks_like_yaml = trimmed.contains(':') 
                || trimmed.starts_with("- ") 
                || trimmed.starts_with("|") 
                || trimmed.starts_with(">")
                || trimmed.starts_with("---")
                || trimmed.starts_with("...");
            if !looks_like_yaml {
                return None; // Looks like plain text, not YAML - let it be a setext heading
            }
        } else {
            // No more newlines - remaining is a single line
            if remaining.trim().is_empty() || remaining.trim() == "---" {
                return None;
            }
            // Single line without newline - check if it looks like YAML
            let trimmed = remaining.trim();
            let looks_like_yaml = trimmed.contains(':') 
                || trimmed.starts_with("- ") 
                || trimmed.starts_with("|") 
                || trimmed.starts_with(">")
                || trimmed.starts_with("---")
                || trimmed.starts_with("...");
            if !looks_like_yaml {
                return None;
            }
        }

        reader.advance_to_eol();
        let node_ref = arena.new_node(crate::ast::CodeBlock::new(
            crate::ast::CodeBlockKind::Fenced,
            None,
        ));
        ctx.insert(self.meta_node, node_ref);
        Some((node_ref, parser::State::NO_CHILDREN))
    }

    fn cont(
        &self,
        arena: &mut Arena,
        node_ref: NodeRef,
        reader: &mut crate::text::BasicReader,
        _ctx: &mut parser::Context,
    ) -> Option<parser::State> {
        let (line_bytes, seg) = reader.peek_line_bytes()?;
        if line_bytes.starts_with(b"---") {
            reader.advance_to_eol();
            return None;
        }
        crate::as_type_data_mut!(arena, node_ref, Block).append_source_line(seg);
        Some(parser::State::NO_CHILDREN)
    }

    fn close(
        &self,
        _arena: &mut Arena,
        _node_ref: NodeRef,
        _reader: &mut crate::text::BasicReader,
        _ctx: &mut parser::Context,
    ) {
    }

    fn can_interrupt_paragraph(&self) -> bool {
        true
    }
}

impl From<MetaParser> for AnyBlockParser {
    fn from(p: MetaParser) -> Self {
        AnyBlockParser::Extension(Box::new(p))
    }
}

// --- AST transformer ---

#[derive(Debug)]
struct MetaAstTransformer {
    meta_node: ContextKey<NodeRefValue>,
    options: MetaParserOptions,
}

impl MetaAstTransformer {
    pub fn new(reg: Rc<RefCell<ContextKeyRegistry>>, options: MetaParserOptions) -> Self {
        let meta_node = reg.borrow_mut().get_or_create::<NodeRefValue>(META_NODE);
        Self { meta_node, options }
    }
}

impl AstTransformer for MetaAstTransformer {
    fn transform(
        &self,
        arena: &mut Arena,
        doc_ref: NodeRef,
        reader: &mut crate::text::BasicReader,
        ctx: &mut parser::Context,
    ) {
        let Some(meta_ref) = ctx.get(self.meta_node) else {
            return;
        };
        let mut yaml_data = String::new();
        let source = reader.source();

        for line in
            crate::as_type_data!(arena, *meta_ref, Block).source()
        {
            yaml_data.push_str(&line.str(source));
        }
        meta_ref.delete(arena);

        // If no YAML content was captured (e.g., thematic break `---` was
        // incorrectly consumed), silently skip without inserting an error.
        if yaml_data.trim().is_empty() {
            return;
        }

        match parse_yaml(&yaml_data) {
            Ok(Meta::Mapping(map)) => {
                let m = map.clone();
                for (key, value) in map {
                    crate::as_kind_data_mut!(arena, doc_ref, Document)
                        .metadata_mut()
                        .insert(key, value);
                }
                if self.options.table {
                    render_meta_as_table(arena, doc_ref, m);
                }
            }
            Ok(_other) => {
                // YAML parsed but wasn't a mapping (e.g., a bare list)
                let mut error_data =
                    crate::ast::HtmlBlock::new(crate::ast::HtmlBlockKind::Kind2);
                error_data.set_value(
                    "<!-- YAML metadata must be a mapping -->\n".to_string(),
                );
                let error_ref = arena.new_node(error_data);
                if let Some(first) = arena[doc_ref].first_child() {
                    doc_ref.insert_before(arena, first, error_ref);
                } else {
                    doc_ref.append_child(arena, error_ref);
                }
            }
            Err(e) => {
                let mut error_data =
                    crate::ast::HtmlBlock::new(crate::ast::HtmlBlockKind::Kind2);
                error_data.set_value(
                    format!("<!-- Error parsing YAML metadata: {} -->\n", e).to_string(),
                );
                let error_ref = arena.new_node(error_data);
                if let Some(first) = arena[doc_ref].first_child() {
                    doc_ref.insert_before(arena, first, error_ref);
                } else {
                    doc_ref.append_child(arena, error_ref);
                }
            }
        }
    }
}

impl From<MetaAstTransformer> for AnyAstTransformer {
    fn from(t: MetaAstTransformer) -> Self {
        AnyAstTransformer::Extension(Box::new(t))
    }
}

/// Render metadata as an HTML table node in the AST.
fn render_meta_as_table(arena: &mut Arena, doc_ref: NodeRef, map: StringMap<Meta>) {
    use crate::ast::{Table, TableBody, TableCell, TableHeader, TableRow};

    let table_ref = arena.new_node(Table::new());
    let header_ref = arena.new_node(TableHeader::new());
    let header_row_ref = arena.new_node(TableRow::new());

    for (key, _) in map.iter() {
        let cell_ref = arena.new_node(TableCell::default());
        let text_ref = arena.new_node(crate::ast::Text::new(key.clone()));
        cell_ref.append_child(arena, text_ref);
        header_row_ref.append_child(arena, cell_ref);
    }
    header_ref.append_child(arena, header_row_ref);
    table_ref.append_child(arena, header_ref);

    let body_ref = arena.new_node(TableBody::new());
    table_ref.append_child(arena, body_ref);
    let body_row_ref = arena.new_node(TableRow::new());

    for (_, value) in map {
        let cell_ref = arena.new_node(TableCell::default());
        let text_ref = arena.new_node(crate::ast::Text::new(format_meta_value(&value)));
        cell_ref.append_child(arena, text_ref);
        body_row_ref.append_child(arena, cell_ref);
    }
    body_ref.append_child(arena, body_row_ref);

    if let Some(first) = arena[doc_ref].first_child() {
        doc_ref.insert_before(arena, first, table_ref);
    } else {
        doc_ref.append_child(arena, table_ref);
    }
}

// --- Extension factory ---

/// Returns a parser extension that parses YAML frontmatter.
pub fn meta_parser_extension(options: impl Into<MetaParserOptions>) -> impl ParserExtension {
    ParserExtensionFn::new(|p: &mut Parser| {
        p.add_block_parser(
            MetaParser::new,
            NoParserOptions,
            PRIORITY_SETTEXT_HEADING - 100,
        );
        p.add_ast_transformer(
            MetaAstTransformer::new,
            options.into(),
            0,
        );
    })
}

// --- Unit Tests ---

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

    fn parse_with_meta(source: &str) -> (Arena, NodeRef) {
        let ext = meta_parser_extension(MetaParserOptions::default());
        let parser = Parser::with_extensions(
            parser::Options::default(),
            ext,
        );
        let mut reader = crate::text::BasicReader::new(source);
        parser.parse(&mut reader)
    }

    #[test]
    fn test_simple_frontmatter() {
        let (arena, doc_ref) = parse_with_meta("---\ntitle: Test\n---\n\nBody");
        let kd = &arena[doc_ref].kind_data();
        if let crate::ast::KindData::Document(doc) = kd {
            let meta = doc.metadata();
            assert!(!meta.is_empty(), "Metadata should not be empty");
            assert!(meta.contains_key("title"), "Should have 'title' key");
        } else {
            panic!("Expected Document node");
        }
    }

    #[test]
    fn test_no_frontmatter() {
        let (arena, doc_ref) = parse_with_meta("No frontmatter here");
        let kd = &arena[doc_ref].kind_data();
        if let crate::ast::KindData::Document(doc) = kd {
            let meta = doc.metadata();
            assert!(meta.is_empty(), "Metadata should be empty");
        } else {
            panic!("Expected Document node");
        }
    }

    #[test]
    fn test_thematic_break_not_consumed() {
        // A bare `---` should be parsed as a thematic break, not frontmatter
        let (arena, doc_ref) = parse_with_meta("---");
        let kd = &arena[doc_ref].kind_data();
        if let crate::ast::KindData::Document(doc) = kd {
            let meta = doc.metadata();
            assert!(meta.is_empty(), "Metadata should be empty for thematic break");
        } else {
            panic!("Expected Document node");
        }
        // Verify thematic break exists in children
        let mut child = arena[doc_ref].first_child();
        let mut found_hr = false;
        while let Some(nref) = child {
            if matches!(arena[nref].kind_data(), crate::ast::KindData::ThematicBreak(_)) {
                found_hr = true;
            }
            child = arena[nref].next_sibling();
        }
        assert!(found_hr, "Should have a ThematicBreak child");
    }

    #[test]
    fn test_five_dashes_not_consumed() {
        // Five dashes is a thematic break, not frontmatter
        let (arena, doc_ref) = parse_with_meta("-----");
        let kd = &arena[doc_ref].kind_data();
        if let crate::ast::KindData::Document(doc) = kd {
            let meta = doc.metadata();
            assert!(meta.is_empty(), "Metadata should be empty for five dashes");
        } else {
            panic!("Expected Document node");
        }
        // Verify thematic break exists
        let mut child = arena[doc_ref].first_child();
        let mut found_hr = false;
        while let Some(nref) = child {
            if matches!(arena[nref].kind_data(), crate::ast::KindData::ThematicBreak(_)) {
                found_hr = true;
            }
            child = arena[nref].next_sibling();
        }
        assert!(found_hr, "Should have a ThematicBreak child for five dashes");
    }

    #[test]
    fn test_nested_mapping() {
        let source = "---\nauthor:\n  name: Jane\n  age: 30\n---\n\nBody";
        let (arena, doc_ref) = parse_with_meta(source);
        let kd = &arena[doc_ref].kind_data();
        if let crate::ast::KindData::Document(doc) = kd {
            let meta = doc.metadata();
            assert!(meta.contains_key("author"), "Should have 'author' key");
            if let Some(Meta::Mapping(inner)) = meta.get("author") {
                assert_eq!(inner.get("name"), Some(&Meta::String("Jane".to_string())));
                assert_eq!(inner.get("age"), Some(&Meta::Int(30)));
            } else {
                panic!("Author should be a Mapping");
            }
        } else {
            panic!("Expected Document node");
        }
    }

    #[test]
    fn test_sequence() {
        let source = "---\ntags:\n  - rust\n  - markdown\n---\n\nBody";
        let (arena, doc_ref) = parse_with_meta(source);
        let kd = &arena[doc_ref].kind_data();
        if let crate::ast::KindData::Document(doc) = kd {
            let meta = doc.metadata();
            assert!(meta.contains_key("tags"), "Should have 'tags' key");
            if let Some(Meta::Sequence(items)) = meta.get("tags") {
                assert_eq!(items.len(), 2);
                assert_eq!(items[0], Meta::String("rust".to_string()));
                assert_eq!(items[1], Meta::String("markdown".to_string()));
            } else {
                panic!("Tags should be a Sequence");
            }
        } else {
            panic!("Expected Document node");
        }
    }

    #[test]
    fn test_all_scalar_types() {
        let source = "---\nstr_val: hello\nint_val: 42\nfloat_val: 3.14\nbool_val: true\nnull_val: null\n---\n\nBody";
        let (arena, doc_ref) = parse_with_meta(source);
        let kd = &arena[doc_ref].kind_data();
        if let crate::ast::KindData::Document(doc) = kd {
            let meta = doc.metadata();
            assert_eq!(meta.get("str_val"), Some(&Meta::String("hello".to_string())));
            assert_eq!(meta.get("int_val"), Some(&Meta::Int(42)));
            assert_eq!(meta.get("float_val"), Some(&Meta::Float(3.14)));
            assert_eq!(meta.get("bool_val"), Some(&Meta::Bool(true)));
            assert_eq!(meta.get("null_val"), Some(&Meta::Null));
        } else {
            panic!("Expected Document node");
        }
    }

    #[test]
    fn test_empty_frontmatter() {
        // Empty frontmatter should not crash
        let (arena, doc_ref) = parse_with_meta("---\n---\n\nBody");
        let kd = &arena[doc_ref].kind_data();
        if let crate::ast::KindData::Document(doc) = kd {
            let meta = doc.metadata();
            assert!(meta.is_empty(), "Empty frontmatter should produce empty metadata");
        } else {
            panic!("Expected Document node");
        }
    }

    #[test]
    fn test_frontmatter_with_dash_in_string() {
        // YAML string containing --- should not confuse the parser
        let source = "---\nbody: |\n  text with --- inside\n---\n\nBody";
        let (arena, doc_ref) = parse_with_meta(source);
        let kd = &arena[doc_ref].kind_data();
        if let crate::ast::KindData::Document(doc) = kd {
            let meta = doc.metadata();
            assert!(meta.contains_key("body"), "Should have 'body' key");
        } else {
            panic!("Expected Document node");
        }
    }

    #[test]
    fn test_thematic_break_with_blank_line() {
        // ---\n\nHello should be thematic break, not frontmatter
        let (arena, doc_ref) = parse_with_meta("---\n\nHello");
        let kd = &arena[doc_ref].kind_data();
        if let crate::ast::KindData::Document(doc) = kd {
            let meta = doc.metadata();
            assert!(meta.is_empty(), "Should not parse as frontmatter");
        } else {
            panic!("Expected Document node");
        }
        // Verify thematic break exists
        let mut child = arena[doc_ref].first_child();
        let mut found_hr = false;
        while let Some(nref) = child {
            if matches!(arena[nref].kind_data(), crate::ast::KindData::ThematicBreak(_)) {
                found_hr = true;
            }
            child = arena[nref].next_sibling();
        }
        assert!(found_hr, "Should have a ThematicBreak child");
    }

    #[test]
    fn test_multiple_frontmatter_keys() {
        let source = "---\ntitle: Doc\nauthor: Jane\ndate: 2024-01-15\ntags:\n  - a\n  - b\n---\n\nBody";
        let (arena, doc_ref) = parse_with_meta(source);
        let kd = &arena[doc_ref].kind_data();
        if let crate::ast::KindData::Document(doc) = kd {
            let meta = doc.metadata();
            assert!(meta.contains_key("title"));
            assert!(meta.contains_key("author"));
            assert!(meta.contains_key("date"));
            assert!(meta.contains_key("tags"));
        } else {
            panic!("Expected Document node");
        }
    }

    #[test]
    fn test_original_test_meta_full_frontmatter() {
        // Original mordant-meta test_meta: full frontmatter with nested structures
        let source = "---\ntitle: YAML Frontmatter\ndate: 2026-03-11\ntags: [Rust, Markdown<>]\nauthor:\n  name: yuin\n---\naaa\n";
        let (arena, doc_ref) = parse_with_meta(source);
        let kd = &arena[doc_ref].kind_data();
        if let crate::ast::KindData::Document(doc) = kd {
            let meta = doc.metadata();
            assert_eq!(meta.get("title"), Some(&Meta::String("YAML Frontmatter".to_string())));
            assert_eq!(meta.get("date"), Some(&Meta::String("2026-03-11".to_string())));
            if let Some(Meta::Sequence(tags)) = meta.get("tags") {
                assert_eq!(tags.len(), 2);
                assert_eq!(tags[0], Meta::String("Rust".to_string()));
                assert_eq!(tags[1], Meta::String("Markdown<>".to_string()));
            } else {
                panic!("Tags should be a Sequence");
            }
            if let Some(Meta::Mapping(author)) = meta.get("author") {
                assert_eq!(author.get("name"), Some(&Meta::String("yuin".to_string())));
            } else {
                panic!("Author should be a Mapping");
            }
        } else {
            panic!("Expected Document node");
        }
    }

    #[test]
    fn test_original_test_ok_simple() {
        // Original mordant-meta test_ok: simple frontmatter
        let source = "---\ntitle: YAML Frontmatter\n---\naaa\n";
        let (arena, doc_ref) = parse_with_meta(source);
        let kd = &arena[doc_ref].kind_data();
        if let crate::ast::KindData::Document(doc) = kd {
            let meta = doc.metadata();
            assert_eq!(meta.get("title"), Some(&Meta::String("YAML Frontmatter".to_string())));
        } else {
            panic!("Expected Document node");
        }
    }

    #[test]
    fn test_table_option() {
        // Verify the table option renders metadata as an HTML table
        let ext = meta_parser_extension(MetaParserOptions { table: true });
        let parser = Parser::with_extensions(
            parser::Options::default(),
            ext,
        );
        let source = "---\ntitle: Test\n---\nBody\n";
        let mut reader = crate::text::BasicReader::new(source);
        let (arena, doc_ref) = parser.parse(&mut reader);
        let kd = &arena[doc_ref].kind_data();
        if let crate::ast::KindData::Document(doc) = kd {
            let meta = doc.metadata();
            assert_eq!(meta.get("title"), Some(&Meta::String("Test".to_string())));
            // With table option, a Table node should be inserted as first child
            let first_child = arena[doc_ref].first_child();
            assert!(first_child.is_some(), "Should have a first child (table)");
            if let Some(table_ref) = first_child {
                assert!(matches!(
                    arena[table_ref].kind_data(),
                    crate::ast::KindData::Table(_)
                ), "First child should be a Table node");
            }
        } else {
            panic!("Expected Document node");
        }
    }
}