reasonkit-web 0.1.7

High-performance MCP server for browser automation, web capture, and content extraction. Rust-powered CDP client for AI agents.
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
//! Main content extraction
//!
//! This module extracts the main content from web pages, converting it
//! to clean text or markdown format.

use crate::browser::PageHandle;
use crate::error::{ExtractionError, Result};
use serde::{Deserialize, Serialize};
use tracing::{debug, info, instrument};

/// Extracted content from a page
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtractedContent {
    /// Plain text content
    pub text: String,
    /// Content as markdown (if converted)
    pub markdown: Option<String>,
    /// HTML of the main content
    pub html: String,
    /// Word count
    pub word_count: usize,
    /// Character count
    pub char_count: usize,
    /// Whether content was extracted from article/main element
    pub from_main: bool,
}

/// Content extraction functionality
pub struct ContentExtractor;

impl ContentExtractor {
    /// Extract main content from the page
    #[instrument(skip(page))]
    pub async fn extract_main_content(page: &PageHandle) -> Result<ExtractedContent> {
        info!("Extracting main content");

        // Try to find the main content using various strategies
        let (html, from_main) = Self::find_main_content(&page.page).await?;
        let text = Self::html_to_text(&html);
        let markdown = Self::html_to_markdown(&html);

        let word_count = text.split_whitespace().count();
        let char_count = text.chars().count();

        debug!(
            "Extracted {} words, {} chars, from_main={}",
            word_count, char_count, from_main
        );

        Ok(ExtractedContent {
            text,
            markdown: Some(markdown),
            html,
            word_count,
            char_count,
            from_main,
        })
    }

    /// Extract content from a specific selector
    #[instrument(skip(page))]
    pub async fn extract_from_selector(
        page: &PageHandle,
        selector: &str,
    ) -> Result<ExtractedContent> {
        info!("Extracting from selector: {}", selector);

        let script = format!(
            r#"
            (() => {{
                const el = document.querySelector('{}');
                if (!el) return null;
                return {{
                    html: el.innerHTML,
                    text: el.innerText
                }};
            }})()
            "#,
            selector.replace('\'', "\\'")
        );

        let result: Option<serde_json::Value> = page
            .page
            .evaluate(script.as_str())
            .await
            .map_err(|e| ExtractionError::ExtractionFailed(e.to_string()))?
            .into_value()
            .map_err(|e| ExtractionError::ExtractionFailed(e.to_string()))?;

        let result =
            result.ok_or_else(|| ExtractionError::ElementNotFound(selector.to_string()))?;

        let html = result["html"].as_str().unwrap_or("").to_string();
        let text = result["text"].as_str().unwrap_or("").to_string();

        let markdown = Self::html_to_markdown(&html);
        let word_count = text.split_whitespace().count();
        let char_count = text.chars().count();

        Ok(ExtractedContent {
            text,
            markdown: Some(markdown),
            html,
            word_count,
            char_count,
            from_main: false,
        })
    }

    /// Extract all text from the page body
    #[instrument(skip(page))]
    pub async fn extract_all_text(page: &PageHandle) -> Result<String> {
        let script = r#"
            document.body.innerText
        "#;

        let text: String = page
            .page
            .evaluate(script)
            .await
            .map_err(|e| ExtractionError::ExtractionFailed(e.to_string()))?
            .into_value()
            .map_err(|e| ExtractionError::ExtractionFailed(e.to_string()))?;

        Ok(text)
    }

    /// Find the main content element using various strategies
    async fn find_main_content(page: &chromiumoxide::Page) -> Result<(String, bool)> {
        let script = r#"
            (() => {
                // Strategy 1: Look for article or main elements
                const mainSelectors = [
                    'article',
                    'main',
                    '[role="main"]',
                    '[role="article"]',
                    '.article',
                    '.post',
                    '.content',
                    '.entry-content',
                    '.post-content',
                    '#content',
                    '#main-content',
                    '.main-content'
                ];

                for (const selector of mainSelectors) {
                    const el = document.querySelector(selector);
                    if (el && el.innerText.length > 200) {
                        return { html: el.innerHTML, fromMain: true };
                    }
                }

                // Strategy 2: Find the largest text block
                const textBlocks = [];
                const walker = document.createTreeWalker(
                    document.body,
                    NodeFilter.SHOW_ELEMENT,
                    {
                        acceptNode: (node) => {
                            const tag = node.tagName.toLowerCase();
                            if (['script', 'style', 'nav', 'header', 'footer', 'aside', 'noscript'].includes(tag)) {
                                return NodeFilter.FILTER_REJECT;
                            }
                            return NodeFilter.FILTER_ACCEPT;
                        }
                    }
                );

                let node;
                while (node = walker.nextNode()) {
                    const text = node.innerText || '';
                    if (text.length > 200) {
                        textBlocks.push({
                            el: node,
                            length: text.length
                        });
                    }
                }

                if (textBlocks.length > 0) {
                    // Sort by length and get the longest
                    textBlocks.sort((a, b) => b.length - a.length);
                    return { html: textBlocks[0].el.innerHTML, fromMain: false };
                }

                // Fallback: return body
                return { html: document.body.innerHTML, fromMain: false };
            })()
        "#;

        let result: serde_json::Value = page
            .evaluate(script)
            .await
            .map_err(|e| ExtractionError::ExtractionFailed(e.to_string()))?
            .into_value()
            .map_err(|e| ExtractionError::ExtractionFailed(e.to_string()))?;

        let html = result["html"].as_str().unwrap_or("").to_string();
        let from_main = result["fromMain"].as_bool().unwrap_or(false);

        Ok((html, from_main))
    }

    /// Convert HTML to plain text
    pub fn html_to_text(html: &str) -> String {
        // Remove script and style tags
        let mut text = html.to_string();

        // Remove script tags and content
        let script_re = regex::Regex::new(r"<script[^>]*>[\s\S]*?</script>").unwrap();
        text = script_re.replace_all(&text, "").to_string();

        // Remove style tags and content
        let style_re = regex::Regex::new(r"<style[^>]*>[\s\S]*?</style>").unwrap();
        text = style_re.replace_all(&text, "").to_string();

        // Replace block elements with newlines
        let block_re = regex::Regex::new(r"</(p|div|br|li|h[1-6])>").unwrap();
        text = block_re.replace_all(&text, "\n").to_string();

        // Remove all remaining HTML tags
        let tag_re = regex::Regex::new(r"<[^>]+>").unwrap();
        text = tag_re.replace_all(&text, "").to_string();

        // Decode common HTML entities
        text = Self::decode_html_entities(&text);

        // Normalize whitespace
        let ws_re = regex::Regex::new(r"\s+").unwrap();
        text = ws_re.replace_all(&text, " ").to_string();

        // Normalize newlines
        let nl_re = regex::Regex::new(r"\n\s*\n+").unwrap();
        text = nl_re.replace_all(&text, "\n\n").to_string();

        text.trim().to_string()
    }

    /// Decode common HTML entities
    pub fn decode_html_entities(text: &str) -> String {
        text.replace("&nbsp;", " ")
            .replace("&lt;", "<")
            .replace("&gt;", ">")
            .replace("&amp;", "&")
            .replace("&quot;", "\"")
            .replace("&#39;", "'")
            .replace("&apos;", "'")
            .replace("&#x27;", "'")
            .replace("&#x2F;", "/")
            .replace("&copy;", "(c)")
            .replace("&reg;", "(R)")
            .replace("&trade;", "(TM)")
            .replace("&ndash;", "-")
            .replace("&mdash;", "--")
            .replace("&hellip;", "...")
            .replace("&lsquo;", "'")
            .replace("&rsquo;", "'")
            .replace("&ldquo;", "\"")
            .replace("&rdquo;", "\"")
    }

    /// Convert HTML to markdown
    pub fn html_to_markdown(html: &str) -> String {
        let mut md = html.to_string();

        // Remove script and style
        let script_re = regex::Regex::new(r"<script[^>]*>[\s\S]*?</script>").unwrap();
        md = script_re.replace_all(&md, "").to_string();
        let style_re = regex::Regex::new(r"<style[^>]*>[\s\S]*?</style>").unwrap();
        md = style_re.replace_all(&md, "").to_string();

        // Convert headers
        for i in (1..=6).rev() {
            let h_re = regex::Regex::new(&format!(r"<h{}[^>]*>(.*?)</h{}>", i, i)).unwrap();
            let prefix = "#".repeat(i);
            md = h_re
                .replace_all(&md, format!("{} $1\n\n", prefix))
                .to_string();
        }

        // Convert paragraphs
        let p_re = regex::Regex::new(r"<p[^>]*>(.*?)</p>").unwrap();
        md = p_re.replace_all(&md, "$1\n\n").to_string();

        // Convert line breaks
        let br_re = regex::Regex::new(r"<br\s*/?>").unwrap();
        md = br_re.replace_all(&md, "\n").to_string();

        // Convert bold
        let b_re = regex::Regex::new(r"<(b|strong)[^>]*>(.*?)</(b|strong)>").unwrap();
        md = b_re.replace_all(&md, "**$2**").to_string();

        // Convert italic
        let i_re = regex::Regex::new(r"<(i|em)[^>]*>(.*?)</(i|em)>").unwrap();
        md = i_re.replace_all(&md, "*$2*").to_string();

        // Convert links
        let a_re = regex::Regex::new(r#"<a[^>]*href=["']([^"']+)["'][^>]*>(.*?)</a>"#).unwrap();
        md = a_re.replace_all(&md, "[$2]($1)").to_string();

        // Convert code
        let code_re = regex::Regex::new(r"<code[^>]*>(.*?)</code>").unwrap();
        md = code_re.replace_all(&md, "`$1`").to_string();

        // Convert pre blocks (use [\s\S]*? to match across newlines)
        let pre_re = regex::Regex::new(r"<pre[^>]*>([\s\S]*?)</pre>").unwrap();
        md = pre_re.replace_all(&md, "```\n$1\n```").to_string();

        // Convert lists
        let li_re = regex::Regex::new(r"<li[^>]*>(.*?)</li>").unwrap();
        md = li_re.replace_all(&md, "- $1\n").to_string();

        // Remove remaining tags
        let tag_re = regex::Regex::new(r"<[^>]+>").unwrap();
        md = tag_re.replace_all(&md, "").to_string();

        // Decode HTML entities
        md = Self::decode_html_entities(&md);

        // Clean up whitespace
        let ws_re = regex::Regex::new(r"\n{3,}").unwrap();
        md = ws_re.replace_all(&md, "\n\n").to_string();

        md.trim().to_string()
    }

    /// Normalize whitespace in text
    pub fn normalize_whitespace(text: &str) -> String {
        let ws_re = regex::Regex::new(r"\s+").unwrap();
        ws_re.replace_all(text.trim(), " ").to_string()
    }

    /// Truncate text to a maximum length, adding ellipsis if truncated
    pub fn truncate(text: &str, max_len: usize) -> String {
        if text.len() <= max_len {
            text.to_string()
        } else if max_len <= 3 {
            text.chars().take(max_len).collect()
        } else {
            let truncated: String = text.chars().take(max_len - 3).collect();
            format!("{}...", truncated)
        }
    }
}

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

    // ========================================================================
    // HTML to Text Conversion Tests
    // ========================================================================

    #[test]
    fn test_html_to_text() {
        let html = "<p>Hello <b>world</b>!</p><p>Second paragraph.</p>";
        let text = ContentExtractor::html_to_text(html);
        assert!(text.contains("Hello"));
        assert!(text.contains("world"));
        assert!(!text.contains("<"));
    }

    #[test]
    fn test_html_to_text_removes_scripts() {
        let html = "<p>Content</p><script>evil();</script><p>More</p>";
        let text = ContentExtractor::html_to_text(html);
        assert!(!text.contains("evil"));
        assert!(text.contains("Content"));
        assert!(text.contains("More"));
    }

    #[test]
    fn test_html_to_text_removes_styles() {
        let html = "<p>Content</p><style>.hidden { display: none; }</style><p>More</p>";
        let text = ContentExtractor::html_to_text(html);
        assert!(!text.contains("hidden"));
        assert!(!text.contains("display"));
        assert!(text.contains("Content"));
        assert!(text.contains("More"));
    }

    #[test]
    fn test_html_to_text_multiline_script() {
        let html = r#"
            <p>Before</p>
            <script type="text/javascript">
                function evil() {
                    console.log("bad");
                }
                evil();
            </script>
            <p>After</p>
        "#;
        let text = ContentExtractor::html_to_text(html);
        assert!(!text.contains("evil"));
        assert!(!text.contains("console"));
        assert!(text.contains("Before"));
        assert!(text.contains("After"));
    }

    #[test]
    fn test_html_to_text_preserves_newlines_for_blocks() {
        let html = "<p>Para 1</p><p>Para 2</p>";
        let text = ContentExtractor::html_to_text(html);
        // Should have some separation between paragraphs
        assert!(text.contains("Para 1"));
        assert!(text.contains("Para 2"));
    }

    #[test]
    fn test_html_to_text_strips_all_tags() {
        let html = "<div class=\"container\"><span id=\"test\">Hello</span></div>";
        let text = ContentExtractor::html_to_text(html);
        assert_eq!(text, "Hello");
        assert!(!text.contains("<"));
        assert!(!text.contains(">"));
        assert!(!text.contains("class"));
    }

    // ========================================================================
    // HTML Entity Decoding Tests
    // ========================================================================

    #[test]
    fn test_html_entity_decode_basic() {
        assert_eq!(
            ContentExtractor::decode_html_entities("&lt;div&gt;"),
            "<div>"
        );
        assert_eq!(ContentExtractor::decode_html_entities("&amp;"), "&");
        assert_eq!(ContentExtractor::decode_html_entities("&quot;"), "\"");
    }

    #[test]
    fn test_html_entity_decode_quotes() {
        assert_eq!(ContentExtractor::decode_html_entities("&#39;"), "'");
        assert_eq!(ContentExtractor::decode_html_entities("&apos;"), "'");
        assert_eq!(ContentExtractor::decode_html_entities("&#x27;"), "'");
    }

    #[test]
    fn test_html_entity_decode_typography() {
        assert_eq!(ContentExtractor::decode_html_entities("&ndash;"), "-");
        assert_eq!(ContentExtractor::decode_html_entities("&mdash;"), "--");
        assert_eq!(ContentExtractor::decode_html_entities("&hellip;"), "...");
        assert_eq!(ContentExtractor::decode_html_entities("&lsquo;"), "'");
        assert_eq!(ContentExtractor::decode_html_entities("&rsquo;"), "'");
        assert_eq!(ContentExtractor::decode_html_entities("&ldquo;"), "\"");
        assert_eq!(ContentExtractor::decode_html_entities("&rdquo;"), "\"");
    }

    #[test]
    fn test_html_entity_decode_symbols() {
        assert_eq!(ContentExtractor::decode_html_entities("&copy;"), "(c)");
        assert_eq!(ContentExtractor::decode_html_entities("&reg;"), "(R)");
        assert_eq!(ContentExtractor::decode_html_entities("&trade;"), "(TM)");
    }

    #[test]
    fn test_html_entity_decode_nbsp() {
        assert_eq!(
            ContentExtractor::decode_html_entities("Hello&nbsp;World"),
            "Hello World"
        );
    }

    #[test]
    fn test_html_entity_decode_mixed() {
        let input = "Copyright &copy; 2024 &mdash; All rights reserved &amp; more";
        let output = ContentExtractor::decode_html_entities(input);
        assert_eq!(output, "Copyright (c) 2024 -- All rights reserved & more");
    }

    // ========================================================================
    // Script Removal Tests
    // ========================================================================

    #[test]
    fn test_script_removal_inline() {
        let html = "<script>alert('xss')</script><p>Safe</p>";
        let text = ContentExtractor::html_to_text(html);
        assert!(!text.contains("alert"));
        assert!(!text.contains("xss"));
        assert!(text.contains("Safe"));
    }

    #[test]
    fn test_script_removal_with_attributes() {
        let html = "<script type=\"text/javascript\" src=\"bad.js\">code()</script><p>Safe</p>";
        let text = ContentExtractor::html_to_text(html);
        assert!(!text.contains("code"));
        assert!(!text.contains("javascript"));
        assert!(text.contains("Safe"));
    }

    #[test]
    fn test_script_removal_multiple() {
        let html = "<script>one()</script><p>Middle</p><script>two()</script>";
        let text = ContentExtractor::html_to_text(html);
        assert!(!text.contains("one"));
        assert!(!text.contains("two"));
        assert!(text.contains("Middle"));
    }

    // ========================================================================
    // Whitespace Normalization Tests
    // ========================================================================

    #[test]
    fn test_whitespace_normalization_spaces() {
        let text = "Hello    world";
        let normalized = ContentExtractor::normalize_whitespace(text);
        assert_eq!(normalized, "Hello world");
    }

    #[test]
    fn test_whitespace_normalization_tabs() {
        let text = "Hello\t\tworld";
        let normalized = ContentExtractor::normalize_whitespace(text);
        assert_eq!(normalized, "Hello world");
    }

    #[test]
    fn test_whitespace_normalization_newlines() {
        let text = "Hello\n\n\nworld";
        let normalized = ContentExtractor::normalize_whitespace(text);
        assert_eq!(normalized, "Hello world");
    }

    #[test]
    fn test_whitespace_normalization_mixed() {
        let text = "  Hello   \t\n  world  ";
        let normalized = ContentExtractor::normalize_whitespace(text);
        assert_eq!(normalized, "Hello world");
    }

    #[test]
    fn test_whitespace_normalization_empty() {
        let text = "   ";
        let normalized = ContentExtractor::normalize_whitespace(text);
        assert_eq!(normalized, "");
    }

    #[test]
    fn test_whitespace_normalization_single_word() {
        let text = "  Hello  ";
        let normalized = ContentExtractor::normalize_whitespace(text);
        assert_eq!(normalized, "Hello");
    }

    // ========================================================================
    // Truncation Tests
    // ========================================================================

    #[test]
    fn test_truncation_short_text() {
        let text = "Hello";
        let truncated = ContentExtractor::truncate(text, 10);
        assert_eq!(truncated, "Hello");
    }

    #[test]
    fn test_truncation_exact_length() {
        let text = "Hello";
        let truncated = ContentExtractor::truncate(text, 5);
        assert_eq!(truncated, "Hello");
    }

    #[test]
    fn test_truncation_adds_ellipsis() {
        let text = "Hello World";
        let truncated = ContentExtractor::truncate(text, 8);
        assert_eq!(truncated, "Hello...");
        assert_eq!(truncated.len(), 8);
    }

    #[test]
    fn test_truncation_very_short_limit() {
        let text = "Hello";
        let truncated = ContentExtractor::truncate(text, 3);
        assert_eq!(truncated, "Hel");
    }

    #[test]
    fn test_truncation_zero_limit() {
        let text = "Hello";
        let truncated = ContentExtractor::truncate(text, 0);
        assert_eq!(truncated, "");
    }

    #[test]
    fn test_truncation_empty_text() {
        let text = "";
        let truncated = ContentExtractor::truncate(text, 10);
        assert_eq!(truncated, "");
    }

    #[test]
    fn test_truncation_unicode() {
        let text = "Hello World";
        let truncated = ContentExtractor::truncate(text, 10);
        // Should handle unicode correctly (ellipsis counts as 3 chars)
        assert!(truncated.len() <= 10 || truncated.ends_with("..."));
    }

    // ========================================================================
    // HTML to Markdown Conversion Tests
    // ========================================================================

    #[test]
    fn test_html_to_markdown() {
        let html = "<h1>Title</h1><p>Para with <b>bold</b> and <a href=\"http://example.com\">link</a>.</p>";
        let md = ContentExtractor::html_to_markdown(html);
        assert!(md.contains("# Title"));
        assert!(md.contains("**bold**"));
        assert!(md.contains("[link](http://example.com)"));
    }

    #[test]
    fn test_html_to_markdown_headers() {
        let html = "<h1>H1</h1><h2>H2</h2><h3>H3</h3><h4>H4</h4><h5>H5</h5><h6>H6</h6>";
        let md = ContentExtractor::html_to_markdown(html);
        assert!(md.contains("# H1"));
        assert!(md.contains("## H2"));
        assert!(md.contains("### H3"));
        assert!(md.contains("#### H4"));
        assert!(md.contains("##### H5"));
        assert!(md.contains("###### H6"));
    }

    #[test]
    fn test_html_to_markdown_emphasis() {
        let html = "<p><b>bold</b> and <strong>strong</strong> and <i>italic</i> and <em>emphasis</em></p>";
        let md = ContentExtractor::html_to_markdown(html);
        assert!(md.contains("**bold**"));
        assert!(md.contains("**strong**"));
        assert!(md.contains("*italic*"));
        assert!(md.contains("*emphasis*"));
    }

    #[test]
    fn test_html_to_markdown_code() {
        let html = "<p>Use <code>println!</code> for output.</p>";
        let md = ContentExtractor::html_to_markdown(html);
        assert!(md.contains("`println!`"));
    }

    #[test]
    fn test_html_to_markdown_pre() {
        let html = "<pre>fn main() {\n    println!(\"Hello\");\n}</pre>";
        let md = ContentExtractor::html_to_markdown(html);
        assert!(md.contains("```"));
        assert!(md.contains("fn main()"));
    }

    #[test]
    fn test_html_to_markdown_list() {
        let html = "<ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul>";
        let md = ContentExtractor::html_to_markdown(html);
        assert!(md.contains("- Item 1"));
        assert!(md.contains("- Item 2"));
        assert!(md.contains("- Item 3"));
    }

    #[test]
    fn test_html_to_markdown_removes_scripts() {
        let html = "<p>Safe</p><script>evil()</script>";
        let md = ContentExtractor::html_to_markdown(html);
        assert!(!md.contains("evil"));
        assert!(md.contains("Safe"));
    }

    #[test]
    fn test_html_to_markdown_line_breaks() {
        let html = "Line 1<br>Line 2<br/>Line 3";
        let md = ContentExtractor::html_to_markdown(html);
        assert!(md.contains("Line 1"));
        assert!(md.contains("Line 2"));
        assert!(md.contains("Line 3"));
    }

    // ========================================================================
    // ExtractedContent Structure Tests
    // ========================================================================

    #[test]
    fn test_extracted_content_structure() {
        let content = ExtractedContent {
            text: "Hello world".to_string(),
            markdown: Some("Hello world".to_string()),
            html: "<p>Hello world</p>".to_string(),
            word_count: 2,
            char_count: 11,
            from_main: true,
        };
        assert_eq!(content.word_count, 2);
        assert!(content.from_main);
    }

    #[test]
    fn test_extracted_content_serialization() {
        let content = ExtractedContent {
            text: "Hello".to_string(),
            markdown: Some("Hello".to_string()),
            html: "<p>Hello</p>".to_string(),
            word_count: 1,
            char_count: 5,
            from_main: false,
        };

        let json = serde_json::to_string(&content).unwrap();
        assert!(json.contains("\"text\":\"Hello\""));
        assert!(json.contains("\"word_count\":1"));
        assert!(json.contains("\"from_main\":false"));

        let deserialized: ExtractedContent = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.text, "Hello");
        assert_eq!(deserialized.word_count, 1);
    }

    #[test]
    fn test_extracted_content_empty() {
        let content = ExtractedContent {
            text: String::new(),
            markdown: None,
            html: String::new(),
            word_count: 0,
            char_count: 0,
            from_main: false,
        };
        assert_eq!(content.word_count, 0);
        assert_eq!(content.char_count, 0);
        assert!(content.markdown.is_none());
    }

    // ========================================================================
    // Edge Cases Tests
    // ========================================================================

    #[test]
    fn test_html_to_text_nested_tags() {
        let html = "<div><p><span><b>Nested</b> content</span></p></div>";
        let text = ContentExtractor::html_to_text(html);
        assert!(text.contains("Nested"));
        assert!(text.contains("content"));
        assert!(!text.contains("<"));
    }

    #[test]
    fn test_html_to_text_malformed_html() {
        let html = "<p>Unclosed paragraph <b>bold";
        let text = ContentExtractor::html_to_text(html);
        // Should still extract text even with malformed HTML
        assert!(text.contains("Unclosed"));
        assert!(text.contains("bold"));
    }

    #[test]
    fn test_html_to_text_self_closing_tags() {
        let html = "Hello<br/>World<hr/>Done";
        let text = ContentExtractor::html_to_text(html);
        assert!(text.contains("Hello"));
        assert!(text.contains("World"));
        assert!(text.contains("Done"));
    }

    #[test]
    fn test_html_to_text_comments() {
        let html = "<p>Before</p><!-- This is a comment --><p>After</p>";
        let text = ContentExtractor::html_to_text(html);
        assert!(!text.contains("comment"));
        assert!(text.contains("Before"));
        assert!(text.contains("After"));
    }

    #[test]
    fn test_html_to_text_empty() {
        let html = "";
        let text = ContentExtractor::html_to_text(html);
        assert_eq!(text, "");
    }

    #[test]
    fn test_html_to_text_only_whitespace() {
        let html = "   \n\t   ";
        let text = ContentExtractor::html_to_text(html);
        assert_eq!(text, "");
    }
}