html-to-markdown-rs 3.3.3

High-performance HTML to Markdown converter using the astral-tl parser. Part of the Kreuzberg ecosystem.
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
#![allow(missing_docs)]

use html_to_markdown_rs::ConversionOptions;

#[test]
fn test_basic_paragraph() {
    let html = "<p>Hello world</p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "Hello world\n");
}

#[test]
fn test_multiple_paragraphs() {
    let html = "<p>First paragraph</p><p>Second paragraph</p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "First paragraph\n\nSecond paragraph\n");
}

#[test]
fn test_atx_headings() {
    let html = "<h1>H1</h1><h2>H2</h2><h3>H3</h3><h4>H4</h4><h5>H5</h5><h6>H6</h6>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "# H1\n\n## H2\n\n### H3\n\n#### H4\n\n##### H5\n\n###### H6\n");
}

#[test]
fn test_bold() {
    let html = "<p>Text with <strong>bold</strong> word</p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "Text with **bold** word\n");
}

#[test]
fn test_italic() {
    let html = "<p>Text with <em>italic</em> word</p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "Text with *italic* word\n");
}

#[test]
fn test_bold_and_italic() {
    let html = "<p><strong><em>Bold and italic</em></strong></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "***Bold and italic***\n");
}

#[test]
fn test_inline_code() {
    let html = "<p>Use <code>code</code> here</p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "Use `code` here\n");
}

#[test]
fn test_code_block() {
    let html = "<pre><code>fn main() {\n    println!(\"Hello\");\n}</code></pre>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "```\nfn main() {\n    println!(\"Hello\");\n}\n```\n");
}

#[test]
fn test_simple_link() {
    let html = "<p><a href=\"https://example.com\">Link text</a></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "[Link text](https://example.com)\n");
}

#[test]
fn test_link_with_title() {
    let html = "<p><a href=\"/url\" title=\"title\">Link</a></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "[Link](/url \"title\")\n");
}

#[test]
fn test_simple_image() {
    let html = "<p><img src=\"image.jpg\" alt=\"Alt text\" /></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "![Alt text](image.jpg)\n");
}

#[test]
fn test_graphic_with_url() {
    let html = "<p><graphic url=\"diagram.svg\" alt=\"Diagram\" /></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "![Diagram](diagram.svg)\n");
}

#[test]
fn test_graphic_with_href() {
    let html = "<p><graphic href=\"image.png\" alt=\"Image\" /></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "![Image](image.png)\n");
}

#[test]
fn test_graphic_with_xlink_href() {
    let html = "<p><graphic xlink:href=\"chart.svg\" alt=\"Chart\" /></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "![Chart](chart.svg)\n");
}

#[test]
fn test_graphic_with_src() {
    let html = "<p><graphic src=\"fallback.jpg\" alt=\"Fallback\" /></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "![Fallback](fallback.jpg)\n");
}

#[test]
fn test_graphic_with_filename_fallback() {
    let html = "<p><graphic url=\"image.png\" filename=\"my-image.png\" /></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "![my-image.png](image.png)\n");
}

#[test]
fn test_graphic_attribute_priority() {
    // url should take priority over href, xlink:href, src
    let html = "<p><graphic url=\"priority.svg\" href=\"second.svg\" xlink:href=\"third.svg\" src=\"fourth.svg\" alt=\"Priority\" /></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "![Priority](priority.svg)\n");
}

#[test]
fn test_unordered_list() {
    let html = "<ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "- Item 1\n- Item 2\n- Item 3\n");
}

#[test]
fn test_ordered_list() {
    let html = "<ol><li>First</li><li>Second</li><li>Third</li></ol>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "1. First\n2. Second\n3. Third\n");
}

#[test]
fn test_nested_lists() {
    let html = "<ul><li>Item 1<ul><li>Nested 1</li><li>Nested 2</li></ul></li><li>Item 2</li></ul>";
    let result = convert(html, None).unwrap();
    assert!(result.contains("- Item 1"));
    assert!(result.contains("Nested 1"), "Expected 'Nested 1' in result: {result:?}");
    assert!(result.contains("Nested 2"), "Expected 'Nested 2' in result: {result:?}");
}

#[test]
fn test_blockquote() {
    let html = "<blockquote><p>Quoted text</p></blockquote>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "> Quoted text\n");
}

#[test]
fn test_nested_blockquote() {
    let html = "<blockquote><blockquote><p>Nested quote</p></blockquote></blockquote>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "> > Nested quote\n");
}

#[test]
fn test_horizontal_rule() {
    let html = "<hr>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "---\n");
}

#[test]
fn test_hr_after_paragraph_keeps_blank_line() {
    let html = "<p>paragraph</p><hr>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "paragraph\n\n---\n");
}

#[test]
fn test_comment_between_paragraphs() {
    let html = "<p>yes</p><!----><p>but no</p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "yes\n\nbut no\n");
}

#[test]
fn test_line_break() {
    let html = "<p>Line 1<br>Line 2</p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "Line 1  \nLine 2\n");
}

#[test]
fn test_strikethrough() {
    let html = "<p><del>Deleted text</del></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "~~Deleted text~~\n");
}

#[test]
fn test_simple_table() {
    let html = "<table><tr><th>Header</th></tr><tr><td>Cell</td></tr></table>";
    let result = convert(html, None).unwrap();
    assert!(result.contains("| Header |"));
    assert!(result.contains("| --- |"));
    assert!(result.contains("| Cell |"));
}

#[test]
fn test_table_rowspan() {
    let html = r#"<table>
<tr><th>Header 1</th><th>Header 2</th></tr>
<tr><td rowspan="2">Spanning cell</td><td>
    <div>First row content</div>
    <div>Second line</div>
</td></tr>
<tr><td>
    <div>Next row</div>
    <div>More content</div>
</td></tr>
</table>"#;
    let options = ConversionOptions {
        br_in_tables: true,
        ..Default::default()
    };
    let result = convert(html, Some(options)).unwrap();
    let expected = "\n\n| Header 1 | Header 2 |\n| --- | --- |\n| Spanning cell | First row content<br>Second line |\n|  | Next row<br>More content |\n";
    assert_eq!(result, expected);
}

#[test]
fn test_empty_element() {
    let html = "<p></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "");
}

#[test]
fn test_whitespace_normalization() {
    let html = "<p>Multiple    spaces    here</p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "Multiple spaces here\n");
}

#[test]
fn test_unicode_content() {
    let html = "<p>Hello 世界 🌍</p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "Hello 世界 🌍\n");
}

#[test]
fn test_html_entities() {
    let html = "<p>&lt;div&gt; &amp; &quot;quotes&quot;</p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "<div> & \"quotes\"\n");
}

#[test]
fn test_nested_formatting() {
    let html = "<p><strong>Bold <em>and italic</em> text</strong></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "**Bold *and italic* text**\n");
}

#[test]
fn test_link_inside_paragraph() {
    let html = "<p>Check out <a href=\"https://example.com\">this link</a> for more info.</p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "Check out [this link](https://example.com) for more info.\n");
}

#[test]
fn test_code_with_special_chars() {
    let html = "<code>&lt;html&gt;</code>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "`<html>`\n");
}

#[test]
fn test_empty_link() {
    let html = "<p><a href=\"\">Empty</a></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "[Empty](<>)\n");
}

#[test]
fn test_div_as_block() {
    let html = "<div>Block content</div>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "Block content\n");
}

#[test]
fn test_multiple_divs() {
    let html = "<div>First</div><div>Second</div>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "First\n\nSecond\n");
}

#[test]
fn test_span_inline() {
    let html = "<p>Text with <span>span</span> element</p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "Text with span element\n");
}

#[test]
fn test_subscript() {
    let html = "<p>H<sub>2</sub>O</p>";
    let opts = ConversionOptions {
        sub_symbol: "~".to_string(),
        ..Default::default()
    };
    let result = convert(html, Some(opts)).unwrap();
    assert_eq!(result, "H~2~O\n");
}

#[test]
fn test_subscript_trailing_whitespace() {
    let html = "<p><sub>hello </sub>world</p>";
    let opts = ConversionOptions {
        sub_symbol: "~".to_string(),
        ..Default::default()
    };
    let result = convert(html, Some(opts)).unwrap();
    assert_eq!(result, "~hello~ world\n");
}

#[test]
fn test_subscript_leading_whitespace() {
    let html = "<p>hello<sub> world</sub></p>";
    let opts = ConversionOptions {
        sub_symbol: "~".to_string(),
        ..Default::default()
    };
    let result = convert(html, Some(opts)).unwrap();
    assert_eq!(result, "hello ~world~\n");
}

#[test]
fn test_superscript() {
    let html = "<p>x<sup>2</sup></p>";
    let opts = ConversionOptions {
        sup_symbol: "^".to_string(),
        ..Default::default()
    };
    let result = convert(html, Some(opts)).unwrap();
    assert_eq!(result, "x^2^\n");
}

#[test]
fn test_superscript_trailing_whitespace() {
    let html = "<p><sup>hello </sup>world</p>";
    let opts = ConversionOptions {
        sup_symbol: "^".to_string(),
        ..Default::default()
    };
    let result = convert(html, Some(opts)).unwrap();
    assert_eq!(result, "^hello^ world\n");
}

#[test]
fn test_superscript_leading_whitespace() {
    let html = "<p>hello<sup> world</sup></p>";
    let opts = ConversionOptions {
        sup_symbol: "^".to_string(),
        ..Default::default()
    };
    let result = convert(html, Some(opts)).unwrap();
    assert_eq!(result, "hello ^world^\n");
}

#[test]
fn test_subscript_default_passthrough() {
    let html = "<p>H<sub>2</sub>O</p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "H2O\n");
}

#[test]
fn test_superscript_default_passthrough() {
    let html = "<p>x<sup>2</sup> + y<sup>3</sup></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "x2 + y3\n");
}

#[test]
fn test_subscript_superscript_combined_default() {
    let html = "<p>CO<sub>2</sub><sup>*</sup></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "CO2*\n");
}

#[test]
fn test_subscript_html_tag_symbol() {
    let html = "<p>H<sub>2</sub>O</p>";
    let opts = ConversionOptions {
        sub_symbol: "<sub>".to_string(),
        ..Default::default()
    };
    let result = convert(html, Some(opts)).unwrap();
    assert_eq!(result, "H<sub>2</sub>O\n");
}

#[test]
fn test_adjacent_links_with_newline_separator() {
    let html = "<p>\n<a href=\"/page1\">Link 1</a>\n<a href=\"/page2\">Link 2</a>\n</p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "[Link 1](/page1) [Link 2](/page2)\n");
}

#[test]
fn test_adjacent_links_no_whitespace() {
    let html = "<p><a href=\"/page1\">Link 1</a><a href=\"/page2\">Link 2</a></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "[Link 1](/page1)[Link 2](/page2)\n");
}

#[test]
fn test_adjacent_links_with_space() {
    let html = "<p><a href=\"/page1\">Link 1</a> <a href=\"/page2\">Link 2</a></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "[Link 1](/page1) [Link 2](/page2)\n");
}

#[test]
fn test_adjacent_inline_elements_with_newline() {
    let html = "<p><strong>bold</strong>\n<em>italic</em></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "**bold** *italic*\n");
}

#[test]
fn test_autolink() {
    let html = "<p><a href=\"https://example.com\">https://example.com</a></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "<https://example.com>\n");
}

#[test]
fn test_email_autolink() {
    let html = "<p><a href=\"mailto:test@example.com\">test@example.com</a></p>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "<test@example.com>\n");
}

#[test]
fn test_metadata_extraction() {
    let html = "<html><head><title>Page Title</title></head><body><p>Content</p></body></html>";
    let opts = ConversionOptions {
        extract_metadata: true,
        ..Default::default()
    };
    let result = convert(html, Some(opts)).unwrap();
    assert!(result.contains("Page Title"));
    assert!(result.contains("Content"));
}

#[test]
fn test_metadata_disabled() {
    let html = "<html><head><title>Page Title</title></head><body><p>Content</p></body></html>";
    let opts = ConversionOptions {
        extract_metadata: false,
        ..Default::default()
    };
    let result = convert(html, Some(opts)).unwrap();
    assert!(!result.contains("<!--"));
    assert!(result.contains("Content"));
}

#[test]
fn test_task_list() {
    let html = "<ul><li><input type=\"checkbox\" checked> Done</li><li><input type=\"checkbox\"> Todo</li></ul>";
    let result = convert(html, None).unwrap();
    assert!(result.contains("- [x] Done"));
    assert!(result.contains("- [ ] Todo"));
}

#[test]
fn test_definition_list() {
    let html = "<dl><dt>Term</dt><dd>Definition</dd></dl>";
    let result = convert(html, None).unwrap();
    assert!(result.contains("Term"));
    assert!(result.contains("Definition"));
}

#[test]
fn test_malformed_html() {
    let html = "<p>Unclosed paragraph<p>Another";
    let result = convert(html, None);
    assert!(result.is_ok());
}

#[test]
fn test_deeply_nested_structure() {
    let html = "<div><div><div><div><p>Deeply nested</p></div></div></div></div>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "Deeply nested\n");
}

#[test]
fn test_mixed_content() {
    let html = r#"
        <h1>Title</h1>
        <p>Paragraph with <strong>bold</strong> and <em>italic</em>.</p>
        <ul>
            <li>List item 1</li>
            <li>List item 2</li>
        </ul>
        <p>Link: <a href="https://example.com">Example</a></p>
    "#;
    let result = convert(html, None).unwrap();
    assert!(result.contains("# Title"));
    assert!(result.contains("**bold**"));
    assert!(result.contains("*italic*"));
    assert!(result.contains("- List item 1"));
    assert!(result.contains("[Example](https://example.com)"));
}

#[test]
fn test_ordered_list_with_heading_and_table() {
    let html = r"
<ol>
  <li>
    <h3>h3</h3>
  </li>
  <li>
    <table>
      <caption>table</caption>
      <tr>
        <td>blah</td>
      </tr>
    </table>
  </li>
</ol>
";

    let result = convert(html, None).unwrap();
    let expected = "1. ### h3\n2. *table*\n\n    | blah |\n    | --- |\n";
    assert_eq!(result, expected);
}

#[test]
fn test_heading_wrapped_in_link_issue_115() {
    let html = r#"<a href="https://domain.local"><h2>Heading A</h2></a>"#;
    let result = convert(html, None).unwrap();
    assert_eq!(result, "## [Heading A](https://domain.local)\n");
}

#[test]
fn test_link_text_escaping_issue_114() {
    let html = r#"<a href="https://domain.local">Hi :]</a><br><a href="https://domain.local">1<2</a>"#;
    let result = convert(html, None).unwrap();
    assert_eq!(
        result,
        "[Hi :\\]](https://domain.local)  \n[1<2](https://domain.local)\n"
    );
}

#[test]
fn test_uppercase_tags_issue_113() {
    let html = r"<B>Foo<Br />Bar</B>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "**Foo  \nBar**\n");
}

#[test]
fn test_breaks_and_newlines_issue_112() {
    let html = "<br>\n1\n2\n<b>3</b>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "\n1\n2\n**3**\n");
}

#[test]
fn test_nested_bold_issue_111() {
    let html = "<b>bold<b>er</b></b>";
    let result = convert(html, None).unwrap();
    assert_eq!(result, "**bolder**\n");
}

#[test]
fn hidden_elements_stripped() {
    let html = "<p>visible</p><div hidden>secret</div><p>also visible</p>";
    let result = convert(html, None).unwrap();
    assert!(!result.contains("secret"));
    assert!(result.contains("visible"));
}

#[test]
fn q_element_produces_quotes() {
    let html = "<p>He said <q>hello</q> to me</p>";
    let result = convert(html, None).unwrap();
    assert!(result.contains(r#""hello""#), "q element should add quotes: {result}");
}

#[test]
fn test_wikipedia_back_reference_caret_normalized() {
    // Wikipedia back-references use <a href="#cite_ref-N">^</a>
    // The caret should be normalized to ↑ to avoid confusion with markdown footnote syntax
    let html = r##"<p>Some text<sup><a href="#cite_ref-1">^</a></sup> more text</p>"##;
    let result = convert(html, None).unwrap();
    assert!(
        result.contains("[↑](#cite_ref-1)"),
        "Back-reference caret should be normalized to ↑: {result}"
    );
    assert!(
        !result.contains("[^]"),
        "Should not produce [^] which looks like footnote syntax: {result}"
    );
}

#[test]
fn test_regular_caret_link_not_affected() {
    // Regular links with ^ text but no # href should keep the ^
    let html = r#"<a href="https://example.com">^</a>"#;
    let result = convert(html, None).unwrap();
    assert!(result.contains("[^]"), "Non-anchor caret links should keep ^: {result}");
}

fn convert(
    html: &str,
    opts: Option<html_to_markdown_rs::ConversionOptions>,
) -> html_to_markdown_rs::error::Result<String> {
    html_to_markdown_rs::convert(html, opts, None).map(|r| r.content.unwrap_or_default())
}