hwp2md 0.2.0

HWP/HWPX ↔ Markdown bidirectional converter
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
use super::*;
use crate::ir;

// -----------------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------------

fn first_section_blocks(doc: &ir::Document) -> &[ir::Block] {
    doc.sections
        .first()
        .map(|s| s.blocks.as_slice())
        .unwrap_or(&[])
}

// -----------------------------------------------------------------------
// extract_frontmatter
// -----------------------------------------------------------------------

#[test]
fn extract_frontmatter_valid() {
    let md = "---\ntitle: \"Hello\"\nauthor: \"Alice\"\ndate: \"2026-01-01\"\n---\n\nBody";
    let meta = extract_frontmatter(md);
    assert_eq!(meta.title.as_deref(), Some("Hello"));
    assert_eq!(meta.author.as_deref(), Some("Alice"));
    assert_eq!(meta.created.as_deref(), Some("2026-01-01"));
}

#[test]
fn extract_frontmatter_no_frontmatter() {
    let meta = extract_frontmatter("# Just a heading\n\nSome text.");
    assert!(meta.title.is_none());
    assert!(meta.author.is_none());
}

#[test]
fn extract_frontmatter_incomplete_no_closing_delimiter() {
    // Opening --- exists but no closing --- → treated as no frontmatter.
    let meta = extract_frontmatter("---\ntitle: \"Oops\"\nno closing fence");
    assert!(meta.title.is_none());
}

// -----------------------------------------------------------------------
// parse_markdown — headings
// -----------------------------------------------------------------------

#[test]
fn parse_markdown_heading_level_1() {
    let doc = parse_markdown("# Hello World\n");
    let blocks = first_section_blocks(&doc);
    assert_eq!(blocks.len(), 1);
    if let ir::Block::Heading { level, inlines } = &blocks[0] {
        assert_eq!(*level, 1);
        assert!(!inlines.is_empty());
        assert_eq!(inlines[0].text, "Hello World");
    } else {
        panic!("expected Heading, got {:?}", blocks[0]);
    }
}

#[test]
fn parse_markdown_heading_level_3() {
    let doc = parse_markdown("### Deep\n");
    let blocks = first_section_blocks(&doc);
    if let ir::Block::Heading { level, .. } = &blocks[0] {
        assert_eq!(*level, 3);
    } else {
        panic!("expected Heading");
    }
}

// -----------------------------------------------------------------------
// parse_markdown — paragraph
// -----------------------------------------------------------------------

#[test]
fn parse_markdown_paragraph() {
    let doc = parse_markdown("Hello, parser!\n");
    let blocks = first_section_blocks(&doc);
    if let ir::Block::Paragraph { inlines } = &blocks[0] {
        let text: String = inlines.iter().map(|i| i.text.as_str()).collect();
        assert!(text.contains("Hello, parser!"));
    } else {
        panic!("expected Paragraph, got {:?}", blocks[0]);
    }
}

// -----------------------------------------------------------------------
// parse_markdown — inline styles
// -----------------------------------------------------------------------

#[test]
fn parse_markdown_bold() {
    let doc = parse_markdown("**bold text**\n");
    let blocks = first_section_blocks(&doc);
    if let ir::Block::Paragraph { inlines } = &blocks[0] {
        let bold_inline = inlines.iter().find(|i| i.bold);
        assert!(
            bold_inline.is_some(),
            "no bold inline found; inlines: {inlines:?}"
        );
    } else {
        panic!("expected Paragraph");
    }
}

#[test]
fn parse_markdown_italic() {
    let doc = parse_markdown("*italic text*\n");
    let blocks = first_section_blocks(&doc);
    if let ir::Block::Paragraph { inlines } = &blocks[0] {
        let italic = inlines.iter().find(|i| i.italic);
        assert!(
            italic.is_some(),
            "no italic inline found; inlines: {inlines:?}"
        );
    } else {
        panic!("expected Paragraph");
    }
}

// -----------------------------------------------------------------------
// parse_markdown — code block
// -----------------------------------------------------------------------

#[test]
fn parse_markdown_code_block() {
    let doc = parse_markdown("```rust\nfn main() {}\n```\n");
    let blocks = first_section_blocks(&doc);
    if let ir::Block::CodeBlock { language, code } = &blocks[0] {
        assert_eq!(language.as_deref(), Some("rust"));
        assert!(code.contains("fn main()"), "code: {code}");
    } else {
        panic!("expected CodeBlock, got {:?}", blocks[0]);
    }
}

// -----------------------------------------------------------------------
// parse_markdown — table
// -----------------------------------------------------------------------

#[test]
fn parse_markdown_table() {
    let md = "| A | B |\n| --- | --- |\n| 1 | 2 |\n";
    let doc = parse_markdown(md);
    let blocks = first_section_blocks(&doc);
    if let ir::Block::Table { rows, col_count } = &blocks[0] {
        assert_eq!(*col_count, 2, "col_count: {col_count}");
        assert!(!rows.is_empty());
    } else {
        panic!("expected Table, got {:?}", blocks[0]);
    }
}

// -----------------------------------------------------------------------
// parse_markdown — list
// -----------------------------------------------------------------------

#[test]
fn parse_markdown_unordered_list() {
    let doc = parse_markdown("- item one\n- item two\n");
    let blocks = first_section_blocks(&doc);
    if let ir::Block::List { ordered, items, .. } = &blocks[0] {
        assert!(!ordered);
        assert_eq!(items.len(), 2);
    } else {
        panic!("expected List, got {:?}", blocks[0]);
    }
}

#[test]
fn parse_markdown_ordered_list() {
    let doc = parse_markdown("1. first\n2. second\n");
    let blocks = first_section_blocks(&doc);
    if let ir::Block::List {
        ordered,
        start,
        items,
        ..
    } = &blocks[0]
    {
        assert!(ordered);
        assert_eq!(*start, 1);
        assert_eq!(items.len(), 2);
    } else {
        panic!("expected ordered List, got {:?}", blocks[0]);
    }
}

// -----------------------------------------------------------------------
// parse_markdown — image
// -----------------------------------------------------------------------

#[test]
fn parse_markdown_image_with_alt_text() {
    let doc = parse_markdown("![a cat](cat.png)\n");
    let blocks = first_section_blocks(&doc);
    // comrak turns a standalone image in a paragraph into a Paragraph containing
    // an Image inline; the image block variant may also appear at top level.
    // We accept either representation.
    let found = blocks.iter().any(|b| match b {
        ir::Block::Image { src, alt } => src == "cat.png" && alt == "a cat",
        ir::Block::Paragraph { inlines } => inlines
            .iter()
            .any(|i| i.text.contains("cat.png") && i.text.contains("a cat")),
        _ => false,
    });
    assert!(found, "image not found in blocks: {blocks:?}");
}

// -----------------------------------------------------------------------
// parse_markdown — footnote
// -----------------------------------------------------------------------

#[test]
fn parse_markdown_footnote() {
    let md = "Text[^note].\n\n[^note]: The footnote.\n";
    let doc = parse_markdown(md);
    let blocks = first_section_blocks(&doc);
    let has_fn = blocks
        .iter()
        .any(|b| matches!(b, ir::Block::Footnote { id, .. } if id == "note"));
    assert!(has_fn, "footnote not found; blocks: {blocks:?}");
}

// -----------------------------------------------------------------------
// parse_markdown — math
// -----------------------------------------------------------------------

#[test]
fn parse_markdown_display_math() {
    // comrak math_dollars extension: $$formula$$ on a single line = display math.
    let md = "$$E=mc^2$$\n";
    let doc = parse_markdown(md);
    let blocks = first_section_blocks(&doc);
    // Display math may appear either as ir::Block::Math{display:true} or,
    // when comrak wraps it in a paragraph, as an inline whose text contains "$$".
    let has_display_math = blocks.iter().any(|b| match b {
        ir::Block::Math { display: true, .. } => true,
        ir::Block::Paragraph { inlines } => inlines.iter().any(|i| i.text.contains("$$")),
        _ => false,
    });
    assert!(
        has_display_math,
        "display math not found; blocks: {blocks:?}"
    );
}

// -----------------------------------------------------------------------
// parse_markdown — blockquote
// -----------------------------------------------------------------------

#[test]
fn parse_markdown_blockquote() {
    let doc = parse_markdown("> quoted text\n");
    let blocks = first_section_blocks(&doc);
    let has_bq = blocks
        .iter()
        .any(|b| matches!(b, ir::Block::BlockQuote { .. }));
    assert!(has_bq, "blockquote not found; blocks: {blocks:?}");
}

// -----------------------------------------------------------------------
// parse_markdown — frontmatter extraction via parse_markdown
// -----------------------------------------------------------------------

#[test]
fn parse_markdown_with_frontmatter_metadata() {
    let md = "---\ntitle: \"Parsed Title\"\nauthor: \"Bob\"\n---\n\n# Heading\n";
    let doc = parse_markdown(md);
    assert_eq!(doc.metadata.title.as_deref(), Some("Parsed Title"));
    assert_eq!(doc.metadata.author.as_deref(), Some("Bob"));
}

// -----------------------------------------------------------------------
// extract_frontmatter — additional cases
// -----------------------------------------------------------------------

#[test]
fn extract_frontmatter_keywords_array_format() {
    let md = "---\nkeywords: [rust, wasm, cli]\n---\n";
    let meta = extract_frontmatter(md);
    assert_eq!(meta.keywords, vec!["rust", "wasm", "cli"]);
}

#[test]
fn extract_frontmatter_keywords_comma_format() {
    let md = "---\nkeywords: alpha, beta, gamma\n---\n";
    let meta = extract_frontmatter(md);
    assert_eq!(meta.keywords, vec!["alpha", "beta", "gamma"]);
}

#[test]
fn extract_frontmatter_empty_input() {
    let meta = extract_frontmatter("");
    assert!(meta.title.is_none());
    assert!(meta.keywords.is_empty());
}

#[test]
fn extract_frontmatter_subject_and_description() {
    let md = "---\nsubject: My Subject\ndescription: A longer desc\n---\n";
    let meta = extract_frontmatter(md);
    assert_eq!(meta.subject.as_deref(), Some("My Subject"));
    assert_eq!(meta.description.as_deref(), Some("A longer desc"));
}

// -----------------------------------------------------------------------
// node_to_block — additional cases
// -----------------------------------------------------------------------

#[test]
fn node_to_block_footnote_definition() {
    let md = "[^fn]: Footnote body.\n\nText[^fn].\n";
    let doc = parse_markdown(md);
    let blocks = first_section_blocks(&doc);
    let found = blocks
        .iter()
        .any(|b| matches!(b, ir::Block::Footnote { id, .. } if id == "fn"));
    assert!(
        found,
        "FootnoteDefinition block not found; blocks: {blocks:?}"
    );
}

#[test]
fn node_to_block_display_math() {
    let md = "$$x^2 + y^2 = z^2$$\n";
    let doc = parse_markdown(md);
    let blocks = first_section_blocks(&doc);
    let found = blocks.iter().any(|b| match b {
        ir::Block::Math { display: true, tex } => tex.contains("x^2"),
        ir::Block::Paragraph { inlines } => inlines.iter().any(|i| i.text.contains("$$")),
        _ => false,
    });
    assert!(found, "display Math block not found; blocks: {blocks:?}");
}

#[test]
fn node_to_block_inline_math_in_paragraph() {
    let md = "Area is $\\pi r^2$ units.\n";
    let doc = parse_markdown(md);
    let blocks = first_section_blocks(&doc);
    if let ir::Block::Paragraph { inlines } = &blocks[0] {
        let has_math = inlines.iter().any(|i| i.text.contains('$'));
        assert!(has_math, "inline math not found in inlines: {inlines:?}");
    } else {
        panic!("expected Paragraph, got {:?}", blocks[0]);
    }
}

#[test]
fn node_to_block_image_alt_preserved() {
    let md = "![description here](img/photo.jpg)\n";
    let doc = parse_markdown(md);
    let blocks = first_section_blocks(&doc);
    let found = blocks.iter().any(|b| match b {
        ir::Block::Image { alt, src } => alt == "description here" && src == "img/photo.jpg",
        ir::Block::Paragraph { inlines } => inlines
            .iter()
            .any(|i| i.text.contains("description here") && i.text.contains("img/photo.jpg")),
        _ => false,
    });
    assert!(found, "image with alt text not found; blocks: {blocks:?}");
}

// -----------------------------------------------------------------------
// collect_inlines — additional cases
// -----------------------------------------------------------------------

#[test]
fn collect_inlines_bold_and_italic_combined() {
    let doc = parse_markdown("***both***\n");
    let blocks = first_section_blocks(&doc);
    if let ir::Block::Paragraph { inlines } = &blocks[0] {
        let found = inlines.iter().any(|i| i.bold && i.italic);
        assert!(found, "no bold+italic inline; inlines: {inlines:?}");
    } else {
        panic!("expected Paragraph");
    }
}

#[test]
fn collect_inlines_link_with_bold_text() {
    let doc = parse_markdown("[**click**](https://example.com)\n");
    let blocks = first_section_blocks(&doc);
    if let ir::Block::Paragraph { inlines } = &blocks[0] {
        let found = inlines
            .iter()
            .any(|i| i.bold && i.link.as_deref() == Some("https://example.com"));
        assert!(found, "bold+link inline not found; inlines: {inlines:?}");
    } else {
        panic!("expected Paragraph");
    }
}

#[test]
fn collect_inlines_footnote_reference() {
    let md = "Text[^ref].\n\n[^ref]: Note.\n";
    let doc = parse_markdown(md);
    let blocks = first_section_blocks(&doc);
    if let ir::Block::Paragraph { inlines } = &blocks[0] {
        let has_ref = inlines
            .iter()
            .any(|i| i.footnote_ref.as_deref() == Some("ref"));
        assert!(
            has_ref,
            "footnote reference not found; inlines: {inlines:?}"
        );
    } else {
        panic!("expected Paragraph, got {:?}", blocks[0]);
    }
}

#[test]
fn collect_inlines_inline_image() {
    let doc = parse_markdown("See ![icon](icons/star.svg) here.\n");
    let blocks = first_section_blocks(&doc);
    if let ir::Block::Paragraph { inlines } = &blocks[0] {
        let has_img = inlines.iter().any(|i| i.text.contains("icons/star.svg"));
        assert!(has_img, "inline image not found; inlines: {inlines:?}");
    } else {
        panic!("expected Paragraph");
    }
}

#[test]
fn collect_inlines_superscript() {
    // comrak superscript extension: ^text^
    let doc = parse_markdown("E=mc^2^\n");
    let blocks = first_section_blocks(&doc);
    if let ir::Block::Paragraph { inlines } = &blocks[0] {
        let has_sup = inlines.iter().any(|i| i.superscript);
        assert!(
            has_sup,
            "superscript inline not found; inlines: {inlines:?}"
        );
    } else {
        panic!("expected Paragraph");
    }
}

#[test]
fn collect_inlines_underline_via_html_u_tag() {
    // HTML inline tags <u>…</u> are passed through as raw HTML by comrak when
    // unsafe HTML is NOT enabled (the default). In that case comrak emits the
    // raw tag string as HtmlInline nodes which our handler intercepts.
    let mut options = comrak::Options::default();
    options.render.unsafe_ = true; // allow raw HTML so comrak parses <u>
    let arena = comrak::Arena::new();
    let root = comrak::parse_document(&arena, "Hello <u>world</u>!\n", &options);

    let para = root
        .children()
        .find(|c| matches!(c.data.borrow().value, NodeValue::Paragraph));
    let para = para.expect("paragraph node not found");

    let inlines = collect_inlines(para);
    let has_underline = inlines.iter().any(|i| i.underline && i.text == "world");
    assert!(
        has_underline,
        "underline inline not found; inlines: {inlines:?}"
    );
}

#[test]
fn collect_inlines_subscript_via_html_sub_tag() {
    let mut options = comrak::Options::default();
    options.render.unsafe_ = true;
    let arena = comrak::Arena::new();
    let root = comrak::parse_document(&arena, "H<sub>2</sub>O\n", &options);

    let para = root
        .children()
        .find(|c| matches!(c.data.borrow().value, NodeValue::Paragraph));
    let para = para.expect("paragraph node not found");

    let inlines = collect_inlines(para);
    let has_subscript = inlines.iter().any(|i| i.subscript && i.text == "2");
    assert!(
        has_subscript,
        "subscript inline not found; inlines: {inlines:?}"
    );
}

// -----------------------------------------------------------------------
// collect_inlines — nested <u><sub> combinations
// -----------------------------------------------------------------------

fn parse_with_unsafe_html(input: &str) -> Vec<ir::Inline> {
    let mut options = comrak::Options::default();
    options.render.unsafe_ = true;
    let arena = comrak::Arena::new();
    let root = comrak::parse_document(&arena, input, &options);
    let para = root
        .children()
        .find(|c| matches!(c.data.borrow().value, NodeValue::Paragraph))
        .expect("paragraph not found");
    collect_inlines(para)
}

#[test]
fn collect_inlines_u_wrapping_sub() {
    let inlines = parse_with_unsafe_html("<u><sub>text</sub></u>\n");
    let found = inlines
        .iter()
        .any(|i| i.underline && i.subscript && i.text == "text");
    assert!(
        found,
        "<u><sub>text</sub></u>: expected underline+subscript; got {inlines:?}"
    );
}

#[test]
fn collect_inlines_sub_wrapping_u() {
    let inlines = parse_with_unsafe_html("<sub><u>text</u></sub>\n");
    let found = inlines
        .iter()
        .any(|i| i.underline && i.subscript && i.text == "text");
    assert!(
        found,
        "<sub><u>text</u></sub>: expected underline+subscript; got {inlines:?}"
    );
}

#[test]
fn collect_inlines_unclosed_u_applies_underline_to_remaining() {
    let inlines = parse_with_unsafe_html("<u>text\n");
    let has_underline = inlines
        .iter()
        .any(|i| i.underline && i.text.contains("text"));
    assert!(
        has_underline,
        "unclosed <u>: underline should apply to remaining text; got {inlines:?}"
    );
}

#[test]
fn collect_inlines_u_then_sub_are_separate() {
    let inlines = parse_with_unsafe_html("<u>a</u><sub>b</sub>\n");
    let has_underline_a = inlines
        .iter()
        .any(|i| i.underline && !i.subscript && i.text == "a");
    let has_subscript_b = inlines
        .iter()
        .any(|i| i.subscript && !i.underline && i.text == "b");
    assert!(
        has_underline_a,
        "<u>a</u>: expected underline-only for 'a'; got {inlines:?}"
    );
    assert!(
        has_subscript_b,
        "<sub>b</sub>: expected subscript-only for 'b'; got {inlines:?}"
    );
}

// -----------------------------------------------------------------------
// parse_markdown — complex document
// -----------------------------------------------------------------------

#[test]
fn parse_markdown_complex_document() {
    let md = r#"---
title: "Complex Doc"
author: "Tester"
keywords: [a, b, c]
---

# Heading One

A paragraph with **bold**, *italic*, and `code`.

> A blockquote with text.

| Col1 | Col2 |
| ---- | ---- |
| A    | B    |

1. First
2. Second

$$math$$

[^note]: Footnote text.

See[^note].
"#;
    let doc = parse_markdown(md);
    assert_eq!(doc.metadata.title.as_deref(), Some("Complex Doc"));
    assert_eq!(doc.metadata.author.as_deref(), Some("Tester"));
    assert_eq!(doc.metadata.keywords, vec!["a", "b", "c"]);

    let blocks = first_section_blocks(&doc);
    assert!(
        blocks
            .iter()
            .any(|b| matches!(b, ir::Block::Heading { level: 1, .. })),
        "h1 missing"
    );
    assert!(
        blocks
            .iter()
            .any(|b| matches!(b, ir::Block::Paragraph { .. })),
        "paragraph missing"
    );
    assert!(
        blocks
            .iter()
            .any(|b| matches!(b, ir::Block::BlockQuote { .. })),
        "blockquote missing"
    );
    assert!(
        blocks.iter().any(|b| matches!(b, ir::Block::Table { .. })),
        "table missing"
    );
    assert!(
        blocks
            .iter()
            .any(|b| matches!(b, ir::Block::List { ordered: true, .. })),
        "ordered list missing"
    );
    assert!(
        blocks.iter().any(|b| match b {
            ir::Block::Math { .. } => true,
            ir::Block::Paragraph { inlines } => inlines.iter().any(|i| i.text.contains("$$")),
            _ => false,
        }),
        "math missing"
    );
    assert!(
        blocks
            .iter()
            .any(|b| matches!(b, ir::Block::Footnote { id, .. } if id == "note")),
        "footnote definition missing"
    );
}

// -----------------------------------------------------------------------
// SoftBreak / LineBreak → "\n" inline
// -----------------------------------------------------------------------

#[test]
fn collect_inlines_soft_break_emits_newline() {
    let doc = parse_markdown("first\nsecond\n");
    let blocks = first_section_blocks(&doc);
    assert_eq!(blocks.len(), 1, "expected exactly one paragraph block");
    match &blocks[0] {
        ir::Block::Paragraph { inlines } => {
            let combined: String = inlines.iter().map(|i| i.text.as_str()).collect();
            assert!(
                combined.contains("first"),
                "text 'first' missing; combined: {combined:?}"
            );
            assert!(
                combined.contains("second"),
                "text 'second' missing; combined: {combined:?}"
            );
            assert!(
                inlines.iter().any(|i| i.text == "\n"),
                "SoftBreak must produce a newline inline; inlines: {inlines:?}"
            );
        }
        other => panic!("expected Paragraph, got {other:?}"),
    }
}

#[test]
fn collect_inlines_hard_line_break_emits_newline() {
    // Two trailing spaces force a hard line break (LineBreak node in comrak).
    let doc = parse_markdown("line one  \nline two\n");
    let blocks = first_section_blocks(&doc);
    assert!(
        !blocks.is_empty(),
        "expected at least one block; got nothing"
    );
    if let ir::Block::Paragraph { inlines } = &blocks[0] {
        // Should contain text from both lines and possibly a newline inline.
        let combined: String = inlines.iter().map(|i| i.text.as_str()).collect();
        assert!(
            combined.contains("line one") || combined.contains("line two"),
            "line content missing; got: {combined:?}"
        );
    }
}

// -----------------------------------------------------------------------
// HtmlInline — unknown tags emitted as plain inline text
// -----------------------------------------------------------------------

#[test]
fn collect_inlines_unknown_html_inline_becomes_plain_text() {
    let mut options = comrak::Options::default();
    options.render.unsafe_ = true;
    let arena = comrak::Arena::new();
    // <span> is not handled — must be emitted verbatim as a plain inline.
    let root = comrak::parse_document(&arena, "Hello <span>world</span>!\n", &options);

    let para = root
        .children()
        .find(|c| matches!(c.data.borrow().value, NodeValue::Paragraph))
        .expect("paragraph not found");

    let inlines = collect_inlines(para);
    // The text "world" should appear either as a standalone plain inline or
    // inside the span tags. We just require no panic and some content.
    let combined: String = inlines.iter().map(|i| i.text.as_str()).collect();
    assert!(
        combined.contains("Hello"),
        "plain text lost; combined: {combined:?}"
    );
    // <span> and </span> should appear as plain text since they are unknown.
    assert!(
        combined.contains("span") || combined.contains("world"),
        "unknown html inline not rendered; combined: {combined:?}"
    );
}

// -----------------------------------------------------------------------
// collect_alt_text — text inside an image node
// -----------------------------------------------------------------------

#[test]
fn node_to_block_image_no_alt_returns_empty_alt() {
    // An image with no alt text should have an empty alt string.
    let doc = parse_markdown("![](photo.jpg)\n");
    let blocks = first_section_blocks(&doc);
    let found = blocks.iter().any(|b| match b {
        ir::Block::Image { alt, .. } => alt.is_empty(),
        ir::Block::Paragraph { inlines } => inlines.iter().any(|i| i.text.contains("photo.jpg")),
        _ => false,
    });
    assert!(found, "image not found; blocks: {blocks:?}");
}

// -----------------------------------------------------------------------
// </sub> close tag restores outer subscript state
// -----------------------------------------------------------------------

#[test]
fn collect_inlines_sub_close_tag_restores_state() {
    // After </sub>, subsequent text should NOT be subscript.
    let inlines = parse_with_unsafe_html("<sub>x</sub>y\n");
    let x_is_subscript = inlines.iter().any(|i| i.subscript && i.text == "x");
    let y_not_subscript = inlines.iter().any(|i| !i.subscript && i.text == "y");
    assert!(
        x_is_subscript,
        "x inside <sub> must be subscript; got {inlines:?}"
    );
    assert!(
        y_not_subscript,
        "y after </sub> must NOT be subscript; got {inlines:?}"
    );
}

// -----------------------------------------------------------------------
// Horizontal rule parsed correctly
// -----------------------------------------------------------------------

#[test]
fn parse_markdown_horizontal_rule() {
    let doc = parse_markdown("---\n\ntext\n");
    let blocks = first_section_blocks(&doc);
    assert!(
        blocks
            .iter()
            .any(|b| matches!(b, ir::Block::HorizontalRule)),
        "expected HorizontalRule block from '---'; blocks: {blocks:?}"
    );
    assert!(
        blocks
            .iter()
            .any(|b| matches!(b, ir::Block::Paragraph { .. })),
        "expected Paragraph block with 'text'; blocks: {blocks:?}"
    );
}

// -----------------------------------------------------------------------
// parse_markdown — strikethrough via ~~…~~
// -----------------------------------------------------------------------

#[test]
fn parse_markdown_strikethrough_inline() {
    let doc = parse_markdown("~~struck~~\n");
    let blocks = first_section_blocks(&doc);
    if let ir::Block::Paragraph { inlines } = &blocks[0] {
        let has_strike = inlines.iter().any(|i| i.strikethrough);
        assert!(has_strike, "no strikethrough inline; inlines: {inlines:?}");
    } else {
        panic!("expected Paragraph, got {:?}", blocks[0]);
    }
}