kreuzberg 4.8.2

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 91+ formats and 248 programming languages via tree-sitter code intelligence 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
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
791
792
793
794
795
796
797
//! RTF (Rich Text Format) extractor.
//!
//! Supports: Rich Text Format (.rtf)
//!
//! This native Rust extractor provides text extraction from RTF documents with:
//! - Character encoding support (Windows-1252 for 0x80-0x9F range)
//! - Common RTF control words (paragraph breaks, tabs, bullets, quotes, dashes)
//! - Unicode escape sequences
//! - Image metadata extraction
//! - Whitespace normalization

mod encoding;
mod formatting;
mod images;
mod metadata;
mod parser;
mod tables;

// Re-export public functions for backward compatibility
pub use encoding::{hex_digit_to_u8, parse_hex_byte, parse_rtf_control_word};
pub use formatting::normalize_whitespace;
pub use images::{extract_image_metadata, extract_pict_image};
pub use metadata::{extract_rtf_metadata, parse_rtf_datetime};
pub use parser::{ParagraphMeta, extract_rtf_formatting, extract_text_from_rtf, spans_to_annotations};

use crate::Result;
use crate::core::config::ExtractionConfig;
use crate::plugins::{DocumentExtractor, Plugin};
use crate::types::ExtractedImage;
use crate::types::internal::InternalDocument;
use crate::types::internal_builder::InternalDocumentBuilder;
use crate::types::metadata::Metadata;
use crate::types::uri::Uri;
use async_trait::async_trait;
use bytes::Bytes;
use std::borrow::Cow;

/// Native Rust RTF extractor.
///
/// Extracts text content, metadata, and structure from RTF documents
pub struct RtfExtractor;

impl RtfExtractor {
    /// Create a new RTF extractor.
    pub fn new() -> Self {
        Self
    }
}

impl Default for RtfExtractor {
    fn default() -> Self {
        Self::new()
    }
}

impl RtfExtractor {
    /// Build an `InternalDocument` from RTF content.
    ///
    /// Extracts paragraphs (split on double-newlines), tables, formatting
    /// annotations, and header/footer content with appropriate content layers.
    fn build_internal_document(rtf_content: &str, plain: bool) -> InternalDocument {
        use crate::types::document_structure::ContentLayer;

        let (extracted_text, tables, rtf_images, para_metas, mut formatting) =
            extract_text_from_rtf(rtf_content, plain);
        // Headers/footers are still extracted by the separate formatting pass since
        // the text pass skips those destinations. Merge them in.
        let legacy_formatting = extract_rtf_formatting(rtf_content);
        formatting.header_text = legacy_formatting.header_text;
        formatting.footer_text = legacy_formatting.footer_text;

        let mut builder = InternalDocumentBuilder::new("rtf");

        // Extract URIs from hyperlinks found during RTF parsing
        for (_start, _end, url) in &formatting.hyperlinks {
            if !url.is_empty() {
                // Try to find link text from the extracted text
                let label = extracted_text.get(*_start..*_end).map(|s| s.to_string());
                builder.push_uri(Uri::hyperlink(url, label));
            }
        }

        let mut table_idx = 0;
        let mut meta_idx = 0;
        let mut byte_offset = 0usize;
        let mut in_table_rows = false; // tracking consecutive is_table paragraphs

        // Track list state for grouping list items
        let mut in_list = false;
        let mut list_id: Option<u16> = None;
        let mut list_depth: u8 = 0;

        for paragraph in extracted_text.split("\n\n") {
            let trimmed = paragraph.trim();
            if trimmed.is_empty() {
                byte_offset += paragraph.len() + 2;
                meta_idx += 1;
                continue;
            }

            let meta = para_metas.get(meta_idx).cloned().unwrap_or_default();
            meta_idx += 1;

            // Check if this paragraph is a table row
            if meta.is_table {
                // Close any open list (all nested levels)
                if in_list {
                    for _ in 0..=list_depth {
                        builder.end_list();
                    }
                    in_list = false;
                    list_id = None;
                    list_depth = 0;
                }
                in_table_rows = true;
                // Table rows are handled as a block — skip the text, use structured table data
                byte_offset += paragraph.len() + 2;
                continue;
            }

            // If we were in table rows and now we're not, push the table
            if in_table_rows {
                in_table_rows = false;
                if table_idx < tables.len() {
                    builder.push_table_from_cells(&tables[table_idx].cells, None, None);
                    table_idx += 1;
                }
            }

            // Check if this paragraph looks like a table (fallback for pipe-delimited text)
            let lines: Vec<&str> = trimmed.lines().collect();
            let is_table_like = lines.len() >= 2 && lines.iter().all(|l| l.contains('|'));
            if is_table_like && table_idx < tables.len() {
                // Close any open list (all nested levels)
                if in_list {
                    for _ in 0..=list_depth {
                        builder.end_list();
                    }
                    in_list = false;
                    list_id = None;
                    list_depth = 0;
                }
                builder.push_table_from_cells(&tables[table_idx].cells, None, None);
                table_idx += 1;
                byte_offset += paragraph.len() + 2;
                continue;
            }

            let trim_offset = byte_offset + (paragraph.len() - paragraph.trim_start().len());
            let annotations = spans_to_annotations(trim_offset, trim_offset + trimmed.len(), &formatting);

            // Handle headings
            if meta.heading_level > 0 && meta.heading_level <= 6 {
                // Close any open list (all nested levels)
                if in_list {
                    for _ in 0..=list_depth {
                        builder.end_list();
                    }
                    in_list = false;
                    list_id = None;
                    list_depth = 0;
                }
                builder.push_heading(meta.heading_level, trimmed, None, None);
            }
            // Handle list items
            else if let Some(level) = meta.list_level {
                let new_list_id = meta.list_id;
                let ordered = meta.ordered;

                // Check if we need to start a new list or adjust nesting
                if !in_list || list_id != new_list_id {
                    if in_list {
                        // Close all nested levels plus the root list
                        for _ in 0..=list_depth {
                            builder.end_list();
                        }
                    }
                    builder.push_list(ordered);
                    // If starting at a level > 0, nest immediately
                    for _ in 0..level {
                        builder.push_list(ordered);
                    }
                    in_list = true;
                    list_id = new_list_id;
                    list_depth = level;
                } else if level > list_depth {
                    // Nest deeper
                    for _ in list_depth..level {
                        builder.push_list(ordered);
                    }
                    list_depth = level;
                } else if level < list_depth {
                    // Un-nest
                    for _ in level..list_depth {
                        builder.end_list();
                    }
                    list_depth = level;
                }

                builder.push_list_item(trimmed, ordered, annotations, None, None);
            }
            // Regular paragraph
            else {
                // Close any open list (all nested levels)
                if in_list {
                    for _ in 0..=list_depth {
                        builder.end_list();
                    }
                    in_list = false;
                    list_id = None;
                    list_depth = 0;
                }
                builder.push_paragraph(trimmed, annotations, None, None);
            }
            byte_offset += paragraph.len() + 2;
        }

        // Close any open list (all nested levels)
        if in_list {
            for _ in 0..=list_depth {
                builder.end_list();
            }
        }

        // If we ended while in table rows, push the last table
        if in_table_rows && table_idx < tables.len() {
            builder.push_table_from_cells(&tables[table_idx].cells, None, None);
            table_idx += 1;
        }

        // Push tables that weren't matched to text paragraphs
        while table_idx < tables.len() {
            builder.push_table_from_cells(&tables[table_idx].cells, None, None);
            table_idx += 1;
        }

        // Push extracted images
        for (i, rtf_img) in rtf_images.into_iter().enumerate() {
            let image = ExtractedImage {
                data: Bytes::from(rtf_img.data),
                format: Cow::Borrowed(rtf_img.format),
                image_index: i,
                page_number: None,
                width: None,
                height: None,
                colorspace: None,
                bits_per_component: None,
                is_mask: false,
                description: Some("image".to_string()),
                ocr_result: None,
                bounding_box: None,
                source_path: None,
            };
            builder.push_image(None, image, None, None);
        }

        // Push header/footer with content layers
        if let Some(ref header) = formatting.header_text {
            let idx = builder.push_paragraph(header, vec![], None, None);
            builder.set_layer(idx, ContentLayer::Header);
        }
        if let Some(ref footer) = formatting.footer_text {
            let idx = builder.push_paragraph(footer, vec![], None, None);
            builder.set_layer(idx, ContentLayer::Footer);
        }

        builder.build()
    }
}

impl Plugin for RtfExtractor {
    fn name(&self) -> &str {
        "rtf-extractor"
    }

    fn version(&self) -> String {
        env!("CARGO_PKG_VERSION").to_string()
    }

    fn initialize(&self) -> Result<()> {
        Ok(())
    }

    fn shutdown(&self) -> Result<()> {
        Ok(())
    }

    fn description(&self) -> &str {
        "Extracts content from RTF (Rich Text Format) files with native Rust parsing"
    }

    fn author(&self) -> &str {
        "Kreuzberg Team"
    }
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl DocumentExtractor for RtfExtractor {
    #[cfg_attr(feature = "otel", tracing::instrument(
        skip(self, content, config),
        fields(
            extractor.name = self.name(),
            content.size_bytes = content.len(),
        )
    ))]
    async fn extract_bytes(
        &self,
        content: &[u8],
        mime_type: &str,
        config: &ExtractionConfig,
    ) -> Result<InternalDocument> {
        tracing::debug!(format = "rtf", size_bytes = content.len(), "extraction starting");
        let rtf_content = String::from_utf8_lossy(content);
        let plain = true; // InternalDocument doesn't need markdown formatting

        // extract_rtf_metadata needs the extracted text; get it from the same pass
        let (extracted_text, _tables, _images, _metas, _formatting_data) = extract_text_from_rtf(&rtf_content, plain);
        let mut metadata_map = extract_rtf_metadata(&rtf_content, &extracted_text);

        // Map standard fields from metadata_map to typed Metadata fields
        let title = metadata_map
            .remove(&Cow::Borrowed("title"))
            .and_then(|v| v.as_str().map(|s| s.to_string()));
        let subject = metadata_map
            .remove(&Cow::Borrowed("subject"))
            .and_then(|v| v.as_str().map(|s| s.to_string()));
        let authors = metadata_map.remove(&Cow::Borrowed("authors")).and_then(|v| {
            v.as_array()
                .map(|arr| arr.iter().filter_map(|v| v.as_str().map(|s| s.to_string())).collect())
        });
        let created_by = metadata_map
            .remove(&Cow::Borrowed("created_by"))
            .and_then(|v| v.as_str().map(|s| s.to_string()));
        let modified_by = metadata_map
            .remove(&Cow::Borrowed("modified_by"))
            .and_then(|v| v.as_str().map(|s| s.to_string()));
        let created_at = metadata_map
            .remove(&Cow::Borrowed("created_at"))
            .and_then(|v| v.as_str().map(|s| s.to_string()));
        let modified_at = metadata_map
            .remove(&Cow::Borrowed("modified_at"))
            .and_then(|v| v.as_str().map(|s| s.to_string()));

        let mut doc = Self::build_internal_document(&rtf_content, plain);

        // Filter headers/footers based on content_filter config.
        // When content_filter is None, keep current behavior (headers/footers included).
        // When content_filter is Some(...), respect include_headers/include_footers flags.
        if let Some(ref filter) = config.content_filter {
            use crate::types::document_structure::ContentLayer;
            doc.elements.retain(|elem| match elem.layer {
                ContentLayer::Header => filter.include_headers,
                ContentLayer::Footer => filter.include_footers,
                _ => true,
            });
        }

        doc.mime_type = Cow::Owned(mime_type.to_string());
        doc.metadata = Metadata {
            title,
            subject,
            authors,
            created_by,
            modified_by,
            created_at,
            modified_at,
            additional: metadata_map,
            ..Default::default()
        };

        tracing::debug!(
            element_count = doc.elements.len(),
            format = "rtf",
            "extraction complete"
        );
        Ok(doc)
    }

    fn supported_mime_types(&self) -> &[&str] {
        &["application/rtf", "text/rtf"]
    }

    fn priority(&self) -> i32 {
        50
    }
}

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

    #[tokio::test]
    async fn test_rtf_extractor_plugin_interface() {
        let extractor = RtfExtractor::new();
        assert_eq!(extractor.name(), "rtf-extractor");
        assert_eq!(extractor.version(), env!("CARGO_PKG_VERSION"));
        assert!(extractor.supported_mime_types().contains(&"application/rtf"));
        assert_eq!(extractor.priority(), 50);
    }

    #[test]
    fn test_simple_rtf_extraction() {
        let _extractor = RtfExtractor;
        let rtf_content = r#"{\rtf1 Hello World}"#;
        let (extracted, _, _, _, _) = extract_text_from_rtf(rtf_content, false);
        assert!(extracted.contains("Hello") || extracted.contains("World"));
    }

    #[test]
    fn test_plain_text_no_image_markdown() {
        let rtf_content = r#"{\rtf1 Before image {\pict\jpegblip\picw100\pich100 ffd8ffe0} After image}"#;
        let (plain, _, _, _, _) = extract_text_from_rtf(rtf_content, true);
        assert!(!plain.contains("!["), "Plain text should not contain image markdown");
        assert!(
            !plain.contains("]("),
            "Plain text should not contain image markdown links"
        );

        let (md, _, _, _, _) = extract_text_from_rtf(rtf_content, false);
        assert!(md.contains("![image]"), "Markdown output should contain image markers");
    }

    #[test]
    fn test_plain_text_table_uses_pipes() {
        let rtf_content = r#"{\rtf1 {\trowd Cell1\cell Cell2\cell\row}}"#;
        let (plain, tables, _, _, _) = extract_text_from_rtf(rtf_content, true);
        // Table rows produce [TABLE_ROW] placeholders in plain text
        assert!(
            plain.contains("[TABLE_ROW]"),
            "Plain text should contain table row placeholder, got: {}",
            plain
        );
        assert!(!tables.is_empty(), "Tables should still be extracted");
    }

    #[test]
    fn test_rtf_bold_formatting_extraction() {
        let rtf_content = r#"{\rtf1 Normal {\b Bold text} more normal}"#;
        let formatting = extract_rtf_formatting(rtf_content);
        // Should have at least one bold span
        let has_bold = formatting.spans.iter().any(|s| s.bold);
        assert!(has_bold, "Should detect bold formatting in RTF");
    }

    #[test]
    fn test_rtf_italic_formatting_extraction() {
        let rtf_content = r#"{\rtf1 Normal {\i Italic text} more normal}"#;
        let formatting = extract_rtf_formatting(rtf_content);
        let has_italic = formatting.spans.iter().any(|s| s.italic);
        assert!(has_italic, "Should detect italic formatting in RTF");
    }

    #[test]
    fn test_rtf_underline_formatting_extraction() {
        let rtf_content = r#"{\rtf1 Normal {\ul Underlined text} more normal}"#;
        let formatting = extract_rtf_formatting(rtf_content);
        let has_underline = formatting.spans.iter().any(|s| s.underline);
        assert!(has_underline, "Should detect underline formatting in RTF");
    }

    #[test]
    fn test_rtf_color_table_extraction() {
        let rtf_content = r#"{\rtf1{\colortbl;\red255\green0\blue0;\red0\green255\blue0;}Normal {\cf1 Red text} more}"#;
        let formatting = extract_rtf_formatting(rtf_content);
        assert!(
            formatting.color_table.len() >= 2,
            "Should parse at least 2 color entries"
        );
        assert_eq!(formatting.color_table[1], "#ff0000", "First color should be red");
    }

    #[test]
    fn test_rtf_header_footer_extraction() {
        let rtf_content = r#"{\rtf1{\header My Header Text}{\footer My Footer Text}Body text}"#;
        let formatting = extract_rtf_formatting(rtf_content);
        assert!(formatting.header_text.is_some(), "Should extract header text");
        assert!(formatting.footer_text.is_some(), "Should extract footer text");
        assert!(
            formatting.header_text.as_deref().unwrap_or("").contains("Header"),
            "Header text should contain 'Header'"
        );
        assert!(
            formatting.footer_text.as_deref().unwrap_or("").contains("Footer"),
            "Footer text should contain 'Footer'"
        );
    }

    #[tokio::test]
    async fn test_rtf_document_structure_with_annotations() {
        let rtf_content = r#"{\rtf1 Normal text\par {\b Bold paragraph}\par More normal text}"#;
        let extractor = RtfExtractor::new();
        let config = ExtractionConfig {
            include_document_structure: true,
            ..Default::default()
        };
        let result = extractor
            .extract_bytes(rtf_content.as_bytes(), "application/rtf", &config)
            .await
            .expect("RTF extraction failed");
        let result =
            crate::extraction::derive::derive_extraction_result(result, true, crate::core::config::OutputFormat::Plain);
        assert!(result.document.is_some(), "Should produce document structure");
        let doc = result.document.unwrap();
        assert!(!doc.nodes.is_empty(), "Document structure should have nodes");
    }

    #[test]
    fn test_bookmark_hyperlink_boundaries() {
        let rtf_content = r#"{\rtf1\ansi
\pard
{\*\bkmkstart bookmark_1}Bookmark_1{\*\bkmkend bookmark_1}
\par
\pard
{\field{\*\fldinst { HYPERLINK  \\l "bookmark_1" }}{\fldrslt{click me}}}
\par
}"#;
        let (text, _, _, _, formatting) = extract_text_from_rtf(rtf_content, false);
        assert!(!formatting.hyperlinks.is_empty(), "Should have hyperlinks");
        let (start, end, ref url) = formatting.hyperlinks[0];
        let link_text = &text[start..end];
        assert_eq!(
            link_text, "click me",
            "Hyperlink should cover 'click me', got: {:?}",
            link_text
        );
        assert_eq!(url, "#bookmark_1");
    }

    #[test]
    fn test_bold_italic_span_alignment() {
        let rtf_content = r#"{\rtf1 Normal {\b bold text} more normal}"#;
        let (text, _, _, _, formatting) = extract_text_from_rtf(rtf_content, false);
        let bold_spans: Vec<_> = formatting.spans.iter().filter(|s| s.bold).collect();
        assert!(!bold_spans.is_empty(), "Should have bold spans");
        let span = &bold_spans[0];
        let bold_text = &text[span.start..span.end];
        assert_eq!(
            bold_text, "bold text",
            "Bold span should exactly cover 'bold text', got: {:?}",
            bold_text
        );
    }

    #[test]
    fn test_italic_span_alignment() {
        let rtf_content = r#"{\rtf1 Normal {\i italic text} more normal}"#;
        let (text, _, _, _, formatting) = extract_text_from_rtf(rtf_content, false);
        let italic_spans: Vec<_> = formatting.spans.iter().filter(|s| s.italic).collect();
        assert!(!italic_spans.is_empty(), "Should have italic spans");
        let span = &italic_spans[0];
        let italic_text = &text[span.start..span.end];
        assert_eq!(
            italic_text, "italic text",
            "Italic span should exactly cover 'italic text', got: {:?}",
            italic_text
        );
    }

    #[test]
    fn test_bold_and_italic_span_alignment() {
        let rtf_content = r#"{\rtf1 {\b bold }{\b\i and italics} end}"#;
        let (text, _, _, _, formatting) = extract_text_from_rtf(rtf_content, false);
        let bold_spans: Vec<_> = formatting.spans.iter().filter(|s| s.bold).collect();
        assert!(!bold_spans.is_empty(), "Should have bold spans");
        let full_bold: String = bold_spans
            .iter()
            .map(|s| &text[s.start..s.end])
            .collect::<Vec<_>>()
            .join("");
        assert!(
            full_bold.contains("bold"),
            "Bold text should include 'bold', got: {:?}",
            full_bold
        );
        let bi_spans: Vec<_> = formatting.spans.iter().filter(|s| s.bold && s.italic).collect();
        assert!(!bi_spans.is_empty(), "Should have bold+italic spans");
        let bi_text = &text[bi_spans[0].start..bi_spans[0].end];
        assert_eq!(
            bi_text, "and italics",
            "Bold+italic span should cover 'and italics', got: {:?}",
            bi_text
        );
    }

    #[test]
    fn test_formatting_with_paragraph_breaks() {
        let rtf_content = r#"{\rtf1 Normal text\par {\b bold}\par {\i italics}\par }"#;
        let (text, _, _, _, formatting) = extract_text_from_rtf(rtf_content, true);
        let bold_spans: Vec<_> = formatting.spans.iter().filter(|s| s.bold).collect();
        if !bold_spans.is_empty() {
            let span = &bold_spans[0];
            assert!(
                span.end <= text.len(),
                "Bold span end {} exceeds text length {}",
                span.end,
                text.len()
            );
            let bold_text = &text[span.start..span.end];
            assert_eq!(bold_text, "bold", "Bold span should cover 'bold', got: {:?}", bold_text);
        }
        let italic_spans: Vec<_> = formatting.spans.iter().filter(|s| s.italic).collect();
        if !italic_spans.is_empty() {
            let span = &italic_spans[0];
            assert!(
                span.end <= text.len(),
                "Italic span end {} exceeds text length {}",
                span.end,
                text.len()
            );
            let italic_text = &text[span.start..span.end];
            assert_eq!(
                italic_text, "italics",
                "Italic span should cover 'italics', got: {:?}",
                italic_text
            );
        }
    }

    #[test]
    fn test_bookmark_hyperlink_with_formatting() {
        // Nested groups inside fldrslt (e.g. color/font changes)
        let rtf_content = r#"{\rtf1\ansi
\pard
{\*\bkmkstart bookmark_1}Bookmark_1{\*\bkmkend bookmark_1}
\par
\pard
{\field{\*\fldinst { HYPERLINK  \\l "bookmark_1" }}{\fldrslt {\cf1\ul click me}}}
\par
}"#;
        let (text, _, _, _, formatting) = extract_text_from_rtf(rtf_content, false);
        assert!(!formatting.hyperlinks.is_empty(), "Should have hyperlinks");
        let (start, end, ref url) = formatting.hyperlinks[0];
        let link_text = &text[start..end];
        assert_eq!(
            link_text, "click me",
            "Hyperlink should cover 'click me', got: {:?}",
            link_text
        );
        assert_eq!(url, "#bookmark_1");
    }

    #[test]
    fn test_bookmark_hyperlink_with_extra_spaces() {
        // Extra spaces before the field that cause normalization drift
        let rtf_content = r#"{\rtf1\ansi
\pard
{\*\bkmkstart bookmark_1}Bookmark_1{\*\bkmkend bookmark_1}
\par
\pard
Some  text  before {\field{\*\fldinst { HYPERLINK  \\l "bookmark_1" }}{\fldrslt {\cf1\ul click me}}} after
\par
}"#;
        let (text, _, _, _, formatting) = extract_text_from_rtf(rtf_content, false);
        assert!(!formatting.hyperlinks.is_empty(), "Should have hyperlinks");
        let (start, end, ref url) = formatting.hyperlinks[0];
        let link_text = &text[start..end];
        assert_eq!(
            link_text, "click me",
            "Hyperlink should cover 'click me', got: {:?}",
            link_text
        );
        assert_eq!(url, "#bookmark_1");
    }

    #[test]
    fn test_listtext_ordered_detection() {
        let rtf_content = r#"{\rtf1\ansi
\pard\ilvl0\ls1{\listtext 1.\tab}First item\par
\pard\ilvl0\ls1{\listtext 2.\tab}Second item\par
}"#;
        let (_text, _, _, metas, _) = extract_text_from_rtf(rtf_content, true);
        let ordered_items: Vec<_> = metas.iter().filter(|m| m.ordered && m.list_level.is_some()).collect();
        assert!(
            !ordered_items.is_empty(),
            "Should have ordered list items, metas: {:?}",
            metas
        );
    }

    #[test]
    fn test_bookmark_hyperlink_end_to_end() {
        let rtf_content = r#"{\rtf1\ansi
\pard
{\*\bkmkstart bookmark_1}Bookmark_1{\*\bkmkend bookmark_1}
\par
\pard
{\field{\*\fldinst { HYPERLINK  \\l "bookmark_1" }}{\fldrslt{click me}}}
\par
}"#;
        let doc = RtfExtractor::build_internal_document(rtf_content, true);
        for elem in &doc.elements {
            for ann in &elem.annotations {
                if let crate::types::document_structure::AnnotationKind::Link { ref url, .. } = ann.kind {
                    let link_text = &elem.text[ann.start as usize..ann.end as usize];
                    assert_eq!(
                        link_text, "click me",
                        "E2E: Hyperlink should cover 'click me', got: {:?}",
                        link_text
                    );
                    assert_eq!(url, "#bookmark_1");
                }
            }
        }
    }

    #[test]
    fn test_bookmark_hyperlink_rendered_markdown() {
        let rtf_content = r#"{\rtf1\ansi
\pard
{\*\bkmkstart bookmark_1}Bookmark_1{\*\bkmkend bookmark_1}
\par
\pard
{\field{\*\fldinst { HYPERLINK  \\l "bookmark_1" }}{\fldrslt{click me}}}
\par
}"#;
        let doc = RtfExtractor::build_internal_document(rtf_content, false);
        let rendered = crate::rendering::render_markdown(&doc);
        assert!(
            rendered.contains("[click me](#bookmark_1)"),
            "Should render as [click me](#bookmark_1), got: {:?}",
            rendered
        );
        assert!(
            !rendered.contains("[click m](#bookmark_1)e"),
            "Link boundary should not be off by one"
        );
    }

    #[test]
    fn test_fake_doc_table_extraction() {
        let rtf_content = r#"{\rtf1\ansi\deff0
{\pard \ql \f0 \sa180 \li0 \fi0 \outlinelevel0 \b \fs36 My First Heading\par}
{\pard \ql \f0 \sa180 \li0 \fi0 My first paragraph.\par}
{\pard \sa180 \li0 \fi0 \b Table Example:\par}
{\trowd\cellx3000\cellx6000
\pard\intbl\qc\fs20 Column 1\cell Column 2\cell\row
\pard\intbl\qc\fs20 Row 1, Cell 1\cell Row 1, Cell 2\cell\row
\pard\intbl\qc\fs20 Row 2, Cell 1\cell Row 2, Cell 2\cell\row
}
}"#;
        let (_text, tables, _, metas, _) = extract_text_from_rtf(rtf_content, true);
        assert!(!tables.is_empty(), "Should extract at least one table");
        assert_eq!(
            tables[0].cells.len(),
            3,
            "Table should have 3 rows (header + 2 data), got: {:?}",
            tables[0].cells
        );
        assert!(_text.contains("Table Example:"), "Should have 'Table Example:' in text");
        let table_metas: Vec<_> = metas.iter().filter(|m| m.is_table).collect();
        assert_eq!(
            table_metas.len(),
            3,
            "Should have 3 table row metas, got: {}",
            table_metas.len()
        );
    }

    #[test]
    fn test_multiple_tables_with_captions() {
        let rtf_content = r#"{\rtf1\ansi
{\pard First Caption\par}
{\trowd\cellx3000\cellx6000
\pard\intbl A1\cell A2\cell\row
\pard\intbl B1\cell B2\cell\row
}
{\pard Second Caption\par}
{\trowd\cellx3000\cellx6000
\pard\intbl C1\cell C2\cell\row
\pard\intbl D1\cell D2\cell\row
}
}"#;
        let (text, tables, _, _metas, _) = extract_text_from_rtf(rtf_content, true);
        assert_eq!(tables.len(), 2, "Should have 2 tables, got: {}", tables.len());
        assert_eq!(tables[0].cells.len(), 2, "First table: 2 rows");
        assert_eq!(tables[1].cells.len(), 2, "Second table: 2 rows");
        assert!(text.contains("First Caption"), "Should have 'First Caption'");
        assert!(text.contains("Second Caption"), "Should have 'Second Caption'");
    }

    #[test]
    fn test_listtext_ignorable_ordered_detection() {
        // \listtext inside ignorable destination (\*) must still detect ordered lists
        let rtf_content = r#"{\rtf1\ansi
\pard\ilvl0\ls1{\*\listtext 1.\tab}First item\par
\pard\ilvl0\ls1{\*\listtext 2.\tab}Second item\par
}"#;
        let (_text, _, _, metas, _) = extract_text_from_rtf(rtf_content, true);
        let ordered_items: Vec<_> = metas.iter().filter(|m| m.ordered && m.list_level.is_some()).collect();
        assert!(
            !ordered_items.is_empty(),
            "Should have ordered list items with \\* prefix, metas: {:?}",
            metas
        );
    }
}