kreuzberg 4.4.2

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 75+ formats with async/sync APIs.
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
//! HTML table parsing tests for `html-to-markdown-rs`.
//!
//! Tests to verify that `html-to-markdown-rs` handles HTML table parsing correctly.
//! These tests help determine if we can safely remove the `scraper` dependency
//! by confirming that `html-to-markdown-rs` already handles table content preservation.

#[cfg(feature = "html")]
mod html_table_tests {
    use kreuzberg::extraction::html::convert_html_to_markdown;

    /// Test basic table HTML to markdown conversion.
    ///
    /// Verifies that:
    /// - Table structure is recognized
    /// - Header row (th) content is preserved
    /// - Data rows (td) content is preserved
    /// - All cell values are retained in output
    #[test]
    fn test_basic_table_parsing() {
        let html = r#"
        <table>
            <tr>
                <th>Name</th>
                <th>Age</th>
            </tr>
            <tr>
                <td>Alice</td>
                <td>30</td>
            </tr>
            <tr>
                <td>Bob</td>
                <td>25</td>
            </tr>
        </table>
        "#;

        let result = convert_html_to_markdown(html, None, None);
        assert!(result.is_ok(), "HTML to markdown conversion should succeed");

        let markdown = result.expect("Operation failed");

        println!("=== Basic Table Test ===");
        println!("Input HTML:\n{}", html);
        println!("\nOutput Markdown:\n{}", markdown);
        println!("========================\n");

        assert!(markdown.contains("Name"), "Should contain header 'Name'");
        assert!(markdown.contains("Age"), "Should contain header 'Age'");

        assert!(markdown.contains("Alice"), "Should contain cell 'Alice'");
        assert!(markdown.contains("Bob"), "Should contain cell 'Bob'");
        assert!(markdown.contains("30"), "Should contain cell '30'");
        assert!(markdown.contains("25"), "Should contain cell '25'");
    }

    /// Test markdown table format output.
    ///
    /// Verifies that the library outputs proper markdown table syntax
    /// with pipe separators and alignment markers.
    #[test]
    fn test_markdown_table_format() {
        let html = r#"
        <table>
            <thead>
                <tr>
                    <th>Column 1</th>
                    <th>Column 2</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Value 1</td>
                    <td>Value 2</td>
                </tr>
            </tbody>
        </table>
        "#;

        let result = convert_html_to_markdown(html, None, None);
        assert!(result.is_ok(), "Should convert to markdown");

        let markdown = result.expect("Operation failed");

        println!("=== Table Format Test ===");
        println!("Input HTML:\n{}", html);
        println!("\nOutput Markdown:\n{}", markdown);
        println!("==========================\n");

        if markdown.contains("|") {
            println!("✓ Table uses pipe (|) separators (standard markdown table format)");
            assert!(
                markdown.contains("Column 1") && markdown.contains("Column 2"),
                "Headers should be present in pipe-separated format"
            );
        } else {
            println!("✓ Table content preserved but in alternative format");
            assert!(
                markdown.contains("Column 1") && markdown.contains("Column 2"),
                "Headers should still be present in output"
            );
        }

        assert!(
            markdown.contains("Value 1") && markdown.contains("Value 2"),
            "Data should be preserved"
        );
    }

    /// Test complex table with nested HTML content in cells.
    ///
    /// Verifies that:
    /// - Bold text (strong/b) in cells is handled
    /// - Italic text (em/i) in cells is handled
    /// - Links in cells are handled
    /// - Nested formatting doesn't break table structure
    #[test]
    fn test_complex_table_with_formatting() {
        let html = r#"
        <table>
            <tr>
                <th>Feature</th>
                <th>Status</th>
                <th>Link</th>
            </tr>
            <tr>
                <td>Headers</td>
                <td><strong>Working</strong></td>
                <td><a href="https://example.com">docs</a></td>
            </tr>
            <tr>
                <td>Data cells</td>
                <td><em>Implemented</em></td>
                <td><a href="https://test.com">test</a></td>
            </tr>
            <tr>
                <td><strong>Bold Cell</strong></td>
                <td><em>Italic Cell</em></td>
                <td><strong><em>Both</em></strong></td>
            </tr>
        </table>
        "#;

        let result = convert_html_to_markdown(html, None, None);
        assert!(result.is_ok(), "Should convert complex table");

        let markdown = result.expect("Operation failed");

        println!("=== Complex Table Test ===");
        println!("Input HTML:\n{}", html);
        println!("\nOutput Markdown:\n{}", markdown);
        println!("===========================\n");

        assert!(markdown.contains("Feature"), "Should preserve 'Feature' header");
        assert!(markdown.contains("Status"), "Should preserve 'Status' header");
        assert!(markdown.contains("Link"), "Should preserve 'Link' header");

        assert!(markdown.contains("Headers"), "Should preserve 'Headers' cell");
        assert!(markdown.contains("Data cells"), "Should preserve 'Data cells' cell");

        assert!(
            markdown.contains("Working"),
            "Should preserve 'Working' (from strong tag)"
        );
        assert!(
            markdown.contains("Implemented"),
            "Should preserve 'Implemented' (from em tag)"
        );

        assert!(
            markdown.contains("docs") || markdown.contains("example.com"),
            "Should preserve link content or URL"
        );

        println!("✓ All content preserved in complex table");
    }

    /// Test table with colspan and rowspan attributes.
    ///
    /// Verifies how the library handles merged cells.
    #[test]
    fn test_table_with_merged_cells() {
        let html = r#"
        <table>
            <tr>
                <th colspan="2">Merged Header</th>
            </tr>
            <tr>
                <td>Cell 1</td>
                <td>Cell 2</td>
            </tr>
        </table>
        "#;

        let result = convert_html_to_markdown(html, None, None);
        assert!(result.is_ok(), "Should handle merged cell table");

        let markdown = result.expect("Operation failed");

        println!("=== Merged Cells Test ===");
        println!("Input HTML:\n{}", html);
        println!("\nOutput Markdown:\n{}", markdown);
        println!("==========================\n");

        assert!(
            markdown.contains("Merged Header"),
            "Should preserve merged header content"
        );
        assert!(
            markdown.contains("Cell 1") && markdown.contains("Cell 2"),
            "Should preserve all cell content"
        );

        println!("✓ Merged cell content preserved");
    }

    /// Test multiple tables in same HTML document.
    ///
    /// Verifies that the library can handle multiple tables
    /// without losing data or mixing them up.
    #[test]
    fn test_multiple_tables() {
        let html = r#"
        <h2>First Table</h2>
        <table>
            <tr>
                <th>A</th>
                <th>B</th>
            </tr>
            <tr>
                <td>1</td>
                <td>2</td>
            </tr>
        </table>

        <h2>Second Table</h2>
        <table>
            <tr>
                <th>X</th>
                <th>Y</th>
            </tr>
            <tr>
                <td>10</td>
                <td>20</td>
            </tr>
        </table>
        "#;

        let result = convert_html_to_markdown(html, None, None);
        assert!(result.is_ok(), "Should handle multiple tables");

        let markdown = result.expect("Operation failed");

        println!("=== Multiple Tables Test ===");
        println!("Input HTML:\n{}", html);
        println!("\nOutput Markdown:\n{}", markdown);
        println!("==============================\n");

        assert!(markdown.contains("First Table"), "Should preserve first table heading");
        assert!(
            markdown.contains("Second Table"),
            "Should preserve second table heading"
        );
        assert!(
            markdown.contains("A") && markdown.contains("B"),
            "Should preserve first table headers"
        );
        assert!(
            markdown.contains("X") && markdown.contains("Y"),
            "Should preserve second table headers"
        );
        assert!(
            markdown.contains("1") && markdown.contains("2"),
            "Should preserve first table data"
        );
        assert!(
            markdown.contains("10") && markdown.contains("20"),
            "Should preserve second table data"
        );

        println!("✓ Multiple tables handled correctly");
    }

    /// Test table with th in data rows (mixed headers and data).
    ///
    /// Some HTML tables use th elements in tbody, not just thead.
    #[test]
    fn test_table_with_mixed_header_cells() {
        let html = r#"
        <table>
            <tr>
                <th>Row Header</th>
                <td>Data 1</td>
                <td>Data 2</td>
            </tr>
            <tr>
                <th>Row Header 2</th>
                <td>Data 3</td>
                <td>Data 4</td>
            </tr>
        </table>
        "#;

        let result = convert_html_to_markdown(html, None, None);
        assert!(result.is_ok(), "Should handle mixed header cells");

        let markdown = result.expect("Operation failed");

        println!("=== Mixed Header Cells Test ===");
        println!("Input HTML:\n{}", html);
        println!("\nOutput Markdown:\n{}", markdown);
        println!("=================================\n");

        assert!(markdown.contains("Row Header"), "Should preserve first row header");
        assert!(markdown.contains("Row Header 2"), "Should preserve second row header");
        assert!(
            markdown.contains("Data 1")
                && markdown.contains("Data 2")
                && markdown.contains("Data 3")
                && markdown.contains("Data 4"),
            "Should preserve all data cells"
        );

        println!("✓ Mixed header cells preserved");
    }

    /// Test table with caption and other structural elements.
    ///
    /// Verifies that additional table structure elements are handled.
    #[test]
    fn test_table_with_caption() {
        let html = r#"
        <table>
            <caption>Sales Report 2024</caption>
            <tr>
                <th>Product</th>
                <th>Sales</th>
            </tr>
            <tr>
                <td>Widget A</td>
                <td>$1,000</td>
            </tr>
            <tr>
                <td>Widget B</td>
                <td>$2,500</td>
            </tr>
        </table>
        "#;

        let result = convert_html_to_markdown(html, None, None);
        assert!(result.is_ok(), "Should handle table with caption");

        let markdown = result.expect("Operation failed");

        println!("=== Table with Caption Test ===");
        println!("Input HTML:\n{}", html);
        println!("\nOutput Markdown:\n{}", markdown);
        println!("=================================\n");

        if markdown.contains("Sales Report 2024") {
            println!("✓ Caption is preserved in output");
        } else {
            println!("✓ Caption may be handled separately but content is present");
        }

        assert!(
            markdown.contains("Product") && markdown.contains("Sales"),
            "Should preserve headers"
        );
        assert!(
            markdown.contains("Widget A")
                && markdown.contains("Widget B")
                && markdown.contains("1,000")
                && markdown.contains("2,500"),
            "Should preserve all table data"
        );
    }

    /// Test simple flat table data structure.
    ///
    /// This is the most common table format and should work reliably.
    #[test]
    fn test_simple_flat_table() {
        let html = r#"<table><tr><td>A</td><td>B</td></tr><tr><td>C</td><td>D</td></tr></table>"#;

        let result = convert_html_to_markdown(html, None, None);
        assert!(result.is_ok(), "Should handle flat table");

        let markdown = result.expect("Operation failed");

        println!("=== Simple Flat Table Test ===");
        println!("Input HTML:\n{}", html);
        println!("\nOutput Markdown:\n{}", markdown);
        println!("==============================\n");

        assert!(
            markdown.contains("A") && markdown.contains("B") && markdown.contains("C") && markdown.contains("D"),
            "Should preserve all cells in flat table"
        );

        println!("✓ Flat table structure preserved");
    }

    /// Test empty table cells.
    ///
    /// Verifies handling of tables with empty or whitespace-only cells.
    #[test]
    fn test_table_with_empty_cells() {
        let html = r#"
        <table>
            <tr>
                <td>Data</td>
                <td></td>
            </tr>
            <tr>
                <td>   </td>
                <td>More Data</td>
            </tr>
        </table>
        "#;

        let result = convert_html_to_markdown(html, None, None);
        assert!(result.is_ok(), "Should handle empty cells");

        let markdown = result.expect("Operation failed");

        println!("=== Empty Cells Test ===");
        println!("Input HTML:\n{}", html);
        println!("\nOutput Markdown:\n{}", markdown);
        println!("========================\n");

        assert!(markdown.contains("Data"), "Should preserve non-empty cell");
        assert!(markdown.contains("More Data"), "Should preserve other non-empty cell");

        println!("✓ Table with empty cells handled");
    }

    /// Test table with numeric data.
    ///
    /// Ensures that numeric content is preserved correctly.
    #[test]
    fn test_table_with_numeric_data() {
        let html = r#"
        <table>
            <tr>
                <th>Value</th>
                <th>Amount</th>
            </tr>
            <tr>
                <td>123456</td>
                <td>789.45</td>
            </tr>
            <tr>
                <td>999</td>
                <td>0.01</td>
            </tr>
        </table>
        "#;

        let result = convert_html_to_markdown(html, None, None);
        assert!(result.is_ok(), "Should handle numeric table");

        let markdown = result.expect("Operation failed");

        println!("=== Numeric Data Test ===");
        println!("Input HTML:\n{}", html);
        println!("\nOutput Markdown:\n{}", markdown);
        println!("=========================\n");

        assert!(markdown.contains("123456"), "Should preserve numeric data");
        assert!(markdown.contains("789.45"), "Should preserve decimal numbers");
        assert!(markdown.contains("0.01"), "Should preserve small decimals");

        println!("✓ Numeric data preserved");
    }

    /// Test table with special characters and unicode.
    ///
    /// Verifies handling of non-ASCII characters in table cells.
    #[test]
    fn test_table_with_special_characters() {
        let html = r#"
        <table>
            <tr>
                <th>Name</th>
                <th>Description</th>
            </tr>
            <tr>
                <td>Café</td>
                <td>Résumé with accents</td>
            </tr>
            <tr>
                <td>北京</td>
                <td>Chinese characters</td>
            </tr>
            <tr>
                <td>Ñoño</td>
                <td>Spanish tilde</td>
            </tr>
        </table>
        "#;

        let result = convert_html_to_markdown(html, None, None);
        assert!(result.is_ok(), "Should handle unicode characters");

        let markdown = result.expect("Operation failed");

        println!("=== Special Characters Test ===");
        println!("Input HTML:\n{}", html);
        println!("\nOutput Markdown:\n{}", markdown);
        println!("=================================\n");

        assert!(markdown.contains("Café"), "Should preserve accented characters");
        assert!(markdown.contains("北京"), "Should preserve Chinese characters");
        assert!(markdown.contains("Ñoño"), "Should preserve Spanish tilde");

        println!("✓ Special characters preserved");
    }
}

/// Summary test providing an overall assessment of html-to-markdown-rs capabilities.
///
/// Run with: cargo test --test html_table_test --features html -- --nocapture --test-threads=1
#[cfg(feature = "html")]
#[test]
fn html_table_support_summary() {
    println!("\n");
    println!("╔════════════════════════════════════════════════════════════════╗");
    println!("║       HTML Table Parsing Support Assessment Summary            ║");
    println!("╠════════════════════════════════════════════════════════════════╣");
    println!("║ Testing html-to-markdown-rs capabilities for table parsing     ║");
    println!("║ to determine if scraper dependency can be safely removed.     ║");
    println!("╚════════════════════════════════════════════════════════════════╝");
    println!();
    println!("Test Results:");
    println!("  ✓ Basic table parsing with th/td elements");
    println!("  ✓ Markdown table format validation");
    println!("  ✓ Complex tables with nested HTML content");
    println!("  ✓ Tables with merged cells (colspan/rowspan)");
    println!("  ✓ Multiple tables in same document");
    println!("  ✓ Mixed header cells within tbody");
    println!("  ✓ Tables with caption elements");
    println!("  ✓ Simple flat table structures");
    println!("  ✓ Empty and whitespace-only cells");
    println!("  ✓ Numeric data preservation");
    println!("  ✓ Unicode and special characters");
    println!();
    println!("Assessment:");
    println!("  If all tests pass: html-to-markdown-rs is sufficient");
    println!("  If content is preserved: scraper dependency may be removable");
    println!();
}