nab 0.7.1

Token-optimized HTTP client for LLMs — fetches any URL as clean markdown
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
//! HTML to Markdown conversion handler.
//!
//! Uses Mozilla-style readability extraction to extract clean article content
//! before converting to markdown. Falls back to raw html2md for non-article pages.
//!
//! # Pipeline
//!
//! 1. **SPA data extraction** (Next.js/Nuxt): Extract from `__NEXT_DATA__` / `__NUXT__` JSON
//! 2. **Pre-strip hidden sections**: Remove `<details>`, `<noscript>`, `<dialog>` (browser-hidden)
//! 3. **Pre-strip embedded code**: Remove `<script>`, `<style>`, `<template>` noise
//! 4. **Pre-strip noise sections**: Remove advisories, cookie banners, vulnerability text
//! 5. **Pre-strip comments**: Remove comment threads, Disqus, discussion sections
//! 6. **Readability extraction** (default): Extract main article content, strip boilerplate
//! 7. **Fallback to raw html2md**: If extraction fails, use cleaned HTML with basic filtering
//!
//! The readability step significantly improves output quality by removing navigation,
//! footers, ads, and other noise before markdown conversion.

use anyhow::Result;
use scraper::Selector;
use std::sync::LazyLock;

use super::quality;
use super::readability;
use super::spa_extract;
use super::{ContentHandler, ConversionResult};

static HIDDEN_SECTION_SELECTOR: LazyLock<Selector> = LazyLock::new(|| {
    Selector::parse("details, noscript, dialog").expect("static hidden section selector")
});
static EMBEDDED_NON_SCRIPT_SELECTOR: LazyLock<Selector> =
    LazyLock::new(|| Selector::parse("style, template").expect("static embedded selector"));
static SCRIPT_SELECTOR: LazyLock<Selector> =
    LazyLock::new(|| Selector::parse("script").expect("static script selector"));
static BODY_SELECTOR: LazyLock<Selector> =
    LazyLock::new(|| Selector::parse("body").expect("static body selector"));
static ATTR_SELECTOR: LazyLock<Selector> =
    LazyLock::new(|| Selector::parse("[class], [id]").expect("static attr selector"));
static HEADING_WITH_ID_SELECTOR: LazyLock<Selector> = LazyLock::new(|| {
    Selector::parse("h1[id], h2[id], h3[id], h4[id], h5[id], h6[id]")
        .expect("static heading selector")
});
static COMMENT_CONTAINER_SELECTOR: LazyLock<Selector> = LazyLock::new(|| {
    Selector::parse("div[class], div[id], section[class], section[id]")
        .expect("static comment container selector")
});

/// Converts HTML responses to clean markdown.
pub struct HtmlHandler;

/// HTML-specific extraction controls threaded from higher-level commands.
#[derive(Debug, Clone, Copy)]
pub struct HtmlConversionOptions {
    /// Allow local SPA hydration-data extraction (`__NEXT_DATA__`, JSON-LD, etc.).
    pub allow_spa_extraction: bool,
    /// Allow remote thin-content recovery via `r.jina.ai`.
    pub allow_jina_fallback: bool,
}

impl Default for HtmlConversionOptions {
    fn default() -> Self {
        Self {
            allow_spa_extraction: true,
            allow_jina_fallback: true,
        }
    }
}

impl ContentHandler for HtmlHandler {
    fn supported_types(&self) -> &[&str] {
        &["text/html", "application/xhtml+xml"]
    }

    fn to_markdown(&self, bytes: &[u8], content_type: &str) -> Result<ConversionResult> {
        self.to_markdown_with_url(bytes, content_type, None)
    }
}

impl HtmlHandler {
    /// Convert HTML to markdown, using the real page URL for readability heuristics.
    ///
    /// Providing `url` improves extraction quality: the readability crate uses it
    /// for relative link resolution and site-specific scoring.
    ///
    /// # Errors
    ///
    /// Infallible in practice — returns `Err` only if the internal `Ok(...)` somehow
    /// fails, which cannot happen with the current implementation.
    pub fn to_markdown_with_url(
        &self,
        bytes: &[u8],
        content_type: &str,
        url: Option<&str>,
    ) -> Result<ConversionResult> {
        self.to_markdown_with_url_and_options(
            bytes,
            content_type,
            url,
            HtmlConversionOptions::default(),
        )
    }

    /// Convert HTML to markdown with explicit extraction controls.
    pub fn to_markdown_with_url_and_options(
        &self,
        bytes: &[u8],
        content_type: &str,
        url: Option<&str>,
        options: HtmlConversionOptions,
    ) -> Result<ConversionResult> {
        let start = std::time::Instant::now();
        let html = String::from_utf8_lossy(bytes);
        let markdown = html_to_markdown_with_url_and_options(&html, url, options);
        let quality = quality::score_extraction(bytes, &markdown);

        Ok(ConversionResult {
            markdown,
            page_count: None,
            content_type: content_type.to_string(),
            elapsed_ms: start.elapsed().as_secs_f64() * 1000.0,
            quality: Some(quality),
        })
    }
}

/// Convert HTML to markdown with URL-aware readability extraction.
///
/// # Pipeline
///
/// 1. **SPA extraction**: Try `__NEXT_DATA__` / `__NUXT_DATA__` for React/Vue SPAs
/// 2. **Pre-strip**: Hidden elements → embedded code → noise sections → comment sections
/// 3. **Readability extraction**: Extract article content from cleaned HTML
/// 4. **Fallback**: If extraction fails, use cleaned HTML with basic filtering
/// 5. **Thin-content warning**: Emit a tracing warning when output is disproportionately
///    small vs. the HTML body size, indicating JS-rendered content may be missing.
///
/// Passing the real `url` significantly improves readability quality for sites with
/// complex DOM structures (`LessWrong`, `Ghost CMS`, etc.).
#[must_use]
pub fn html_to_markdown_with_url(html: &str, url: Option<&str>) -> String {
    html_to_markdown_with_url_and_options(html, url, HtmlConversionOptions::default())
}

/// Convert HTML to markdown with URL-aware readability extraction and explicit controls.
#[must_use]
pub fn html_to_markdown_with_url_and_options(
    html: &str,
    url: Option<&str>,
    options: HtmlConversionOptions,
) -> String {
    html_to_markdown_with_url_and_fetcher(html, url, options, fetch_jina_reader)
}

fn html_to_markdown_with_url_and_fetcher<F>(
    html: &str,
    url: Option<&str>,
    options: HtmlConversionOptions,
    jina_fetcher: F,
) -> String
where
    F: Fn(&str) -> Option<String>,
{
    html_to_markdown_with_url_and_sources(
        html,
        url,
        options,
        spa_extract::extract_spa_data,
        jina_fetcher,
    )
}

fn html_to_markdown_with_url_and_sources<S, F>(
    html: &str,
    url: Option<&str>,
    options: HtmlConversionOptions,
    spa_extractor: S,
    jina_fetcher: F,
) -> String
where
    S: Fn(&str) -> Option<String>,
    F: Fn(&str) -> Option<String>,
{
    const MIN_READABILITY_LEN: usize = 50;

    // Try SPA data extraction first (Next.js, Nuxt, etc.)
    let spa_candidate = if options.allow_spa_extraction {
        spa_extractor(html)
    } else {
        None
    };

    if let Some(spa_content) = spa_candidate.as_ref() {
        if !is_thin_content(html.len(), spa_content.len()) {
            return spa_content.clone();
        }

        tracing::debug!(
            "Thin SPA extraction detected ({} chars from {} bytes HTML) — continuing to fallback paths",
            spa_content.len(),
            html.len()
        );
    }

    // Pre-strip noise before readability to prevent non-article content
    // from dominating the scoring. Order: hidden elements → embedded code → noise sections → comments.
    let cleaned_html = strip_hidden_sections(html);
    let cleaned_html = strip_embedded_data_sections(&cleaned_html);
    let cleaned_html = strip_noise_sections(&cleaned_html);
    let cleaned_html = strip_comment_sections(&cleaned_html);

    // Try readability extraction with real URL (or fallback placeholder)
    let effective_url = url.unwrap_or("https://example.com");
    let readability_md =
        readability::extract_article(&cleaned_html, effective_url).map(|article| {
            let md = html2md::parse_html(&article.content_html);
            let lines: Vec<&str> = md
                .lines()
                .map(str::trim)
                .filter(|l| !l.is_empty())
                .collect();
            let md_result = lines.join("\n");

            // html2md sometimes truncates list-heavy content (<ol>/<li>).
            // If the article's plain text is significantly longer than the
            // markdown output, fall back to the plain text which preserves
            // all content from the DOM.
            if article.text_content.len() > md_result.len() + 100 {
                article.text_content
            } else {
                md_result
            }
        });

    // Direct html2md on cleaned HTML (preserves tables, lists, etc.)
    let direct_md = html_to_markdown(&cleaned_html);

    // Pick the better result.  Readability produces clean article content but
    // is much shorter than direct conversion (which includes navigation, CSS,
    // JSON-LD, footers, etc.).  Prefer readability whenever it extracted
    // non-trivial content (>= 50 chars), since readability strips boilerplate
    // that direct_md preserves.  Only fall back to direct when readability
    // produced nearly nothing.
    let markdown = match readability_md {
        Some(ref r_md) if r_md.len() >= MIN_READABILITY_LEN => r_md.clone(),
        Some(ref r_md) if r_md.len() > direct_md.len() => r_md.clone(),
        _ => direct_md,
    };

    // Post-process: strip markdown noise from html2md output
    let markdown = clean_markdown_noise(&markdown);
    let markdown = match spa_candidate {
        Some(spa_content) if spa_content.len() > markdown.len() => spa_content,
        _ => markdown,
    };

    // Auto-recover when output is suspiciously thin relative to the HTML input.
    // A ratio below 2% usually means JS-rendered content was not captured.
    // Re-try SPA extraction on the *original* (uncleaned) HTML — the first
    // extraction pass may have missed embedded JSON that only appears in
    // deeply nested script tags or variable assignments.
    if is_thin_content(html.len(), markdown.len()) {
        tracing::debug!(
            "Thin content detected ({} chars from {} bytes HTML) — attempting recovery",
            markdown.len(),
            html.len()
        );

        // Last-resort fallback: Jina reader can render JS-heavy pages.
        // Only attempt when we have a URL and local extraction failed.
        if options.allow_jina_fallback
            && let Some(page_url) = url
            && let Some(jina_md) = jina_fetcher(page_url)
        {
            tracing::info!(
                "Thin content recovered via Jina reader ({} chars)",
                jina_md.len()
            );
            return jina_md;
        }

        // Jina fallback either wasn't attempted (no URL) or failed.
        // Emit actionable guidance as a warning.
        tracing::warn!(
            "Output is suspiciously thin ({} chars from {} bytes of HTML). \
             The page likely uses JavaScript rendering. Try:\n  \
             1. nab spa <url>              (extract embedded SPA data)\n  \
             2. nab fetch <url>            (uses default browser cookies automatically)\n  \
             3. nab fetch --cookies brave <url>  (override the browser profile if needed)",
            markdown.len(),
            html.len()
        );
    }

    markdown
}

/// Check if markdown output is suspiciously thin relative to HTML input size.
///
/// Returns `true` when the markdown is disproportionately small compared to
/// the raw HTML. This typically indicates JS-rendered content not captured.
///
/// Thresholds: HTML >= 5 KB, markdown < 800 chars, ratio < 2%.
#[must_use]
fn is_thin_content(html_len: usize, markdown_len: usize) -> bool {
    const MIN_HTML_LEN: usize = 5_000;
    const MIN_MARKDOWN_LEN: usize = 800;
    const THIN_RATIO_PERCENT: usize = 2;

    if html_len < MIN_HTML_LEN || markdown_len >= MIN_MARKDOWN_LEN {
        return false;
    }

    let ratio_percent = (markdown_len * 100) / html_len.max(1);
    ratio_percent < THIN_RATIO_PERCENT
}

/// Fetch content from Jina reader as a last-resort fallback for JS-rendered pages.
///
/// Jina reader (`r.jina.ai`) renders JavaScript and returns clean markdown.
/// This is used when local extraction produces suspiciously thin content,
/// indicating the page relies on client-side rendering.
///
/// This helper performs blocking network I/O. Async fetch paths should call it
/// only from blocking conversion contexts.
///
/// Returns `Some(markdown)` if Jina returns substantial content (> 200 chars),
/// `None` on any failure (network error, empty response, timeout).
fn fetch_jina_reader(url: &str) -> Option<String> {
    const MIN_JINA_CONTENT_LEN: usize = 200;
    const JINA_TIMEOUT_SECS: u64 = 15;

    let jina_url = format!("https://r.jina.ai/{url}");

    tracing::debug!("Fetching Jina reader fallback: {}", jina_url);

    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(JINA_TIMEOUT_SECS))
        .build()
        .ok()?;

    let response = client
        .get(&jina_url)
        .header("Accept", "text/markdown")
        .send()
        .ok()?;

    if !response.status().is_success() {
        tracing::debug!("Jina reader returned HTTP {}", response.status().as_u16());
        return None;
    }

    let body = response.text().ok()?;
    let trimmed = body.trim();

    if trimmed.len() >= MIN_JINA_CONTENT_LEN {
        Some(trimmed.to_string())
    } else {
        tracing::debug!(
            "Jina reader returned thin content ({} chars), discarding",
            trimmed.len()
        );
        None
    }
}

/// Detect suspiciously thin markdown output relative to HTML input size.
///
/// Returns a warning message when the markdown is disproportionately small
/// compared to the raw HTML. This typically indicates JavaScript-rendered
/// content that was not captured by the static HTML parser.
///
/// # Returns
///
/// `Some(warning)` when the ratio is below the threshold, `None` otherwise.
#[must_use]
pub fn detect_thin_content(html_len: usize, markdown_len: usize) -> Option<String> {
    if is_thin_content(html_len, markdown_len) {
        let ratio_percent = (markdown_len * 100) / html_len.max(1);
        Some(format!(
            "Output is suspiciously thin ({markdown_len} chars from {html_len} bytes of HTML, \
             {ratio_percent}% ratio). The page likely uses JavaScript rendering — \
             the article body may be missing. Try:\n  \
             1. nab spa <url>              (extract embedded SPA data)\n  \
             2. nab fetch <url>            (uses default browser cookies automatically)\n  \
             3. nab fetch --cookies brave <url>  (override the browser profile if needed)"
        ))
    } else {
        None
    }
}

/// Convert HTML to markdown with readability extraction (URL-unaware).
///
/// Calls [`html_to_markdown_with_url`] with no URL. Prefer the URL-aware
/// variant when the fetch URL is available.
#[must_use]
pub fn html_to_markdown_with_readability(html: &str) -> String {
    html_to_markdown_with_url_and_options(html, None, HtmlConversionOptions::default())
}

/// Remove hidden/collapsed sections from HTML before readability processing.
///
/// Browsers collapse `<details>` elements by default and hide `<noscript>` content
/// when JavaScript is enabled. However, readability and html2md see the full DOM
/// text, so these elements can dominate scoring and crowd out the actual page content.
///
/// This is critical for pages like `deps.rs` where dozens of `<details>` elements
/// containing security advisory text overwhelm the dependency status table.
///
/// # Targeted elements
///
/// - `<details>` — collapsed by default unless `open` attribute is present
/// - `<noscript>` — hidden when JS is available (which static fetch implies)
/// - `<dialog>` — hidden unless `open` attribute is present
///
/// Elements with the `open` attribute are preserved since the page author
/// explicitly intended them to be visible.
#[must_use]
pub fn strip_hidden_sections(html: &str) -> String {
    let document = scraper::Html::parse_document(html);

    // Collect node IDs of hidden elements to remove
    // Keep <details open>, <dialog open> — those are intentionally visible
    let hidden_ids: std::collections::HashSet<ego_tree::NodeId> = document
        .select(&HIDDEN_SECTION_SELECTOR)
        .filter(|el| {
            let name = el.value().name();
            // noscript is always hidden in a JS-capable context
            if name == "noscript" {
                return true;
            }
            // details/dialog: remove only if NOT open
            el.value().attr("open").is_none()
        })
        .map(|el| el.id())
        .collect();

    rebuild_document_excluding(&document, html, &hidden_ids)
}

/// Remove embedded code and templating payloads before fallback extraction.
///
/// JS-heavy pages often ship large inline bootstrap blobs in `<script>` tags or
/// templating payloads in `<template>` blocks. When readability cannot recover the
/// real article body, `html2md` may otherwise emit those blobs as markdown, which
/// prevents the thin-content heuristic from reaching the existing Jina fallback.
///
/// Preserve Schema.org JSON-LD scripts: they are structured content rather than
/// bootstrap noise, and some pages rely on them as the only meaningful fallback.
#[must_use]
pub fn strip_embedded_data_sections(html: &str) -> String {
    let document = scraper::Html::parse_document(html);

    let mut embedded_ids = std::collections::HashSet::new();

    embedded_ids.extend(
        document
            .select(&EMBEDDED_NON_SCRIPT_SELECTOR)
            .map(|el| el.id()),
    );
    embedded_ids.extend(
        document
            .select(&SCRIPT_SELECTOR)
            .filter(|script| !is_jsonld_script(script))
            .map(|script| script.id()),
    );

    rebuild_document_excluding(&document, html, &embedded_ids)
}

fn is_jsonld_script(script: &scraper::ElementRef<'_>) -> bool {
    script
        .value()
        .attr("type")
        .and_then(|script_type| script_type.split(';').next())
        .is_some_and(|script_type| {
            script_type
                .trim()
                .eq_ignore_ascii_case("application/ld+json")
        })
}

/// Recursively serialize a subtree, skipping nodes whose IDs are in `exclude`.
///
/// This handles nested hidden elements at any depth (not just direct children
/// of `<body>`), which is essential for pages where `<details>` elements appear
/// inside `<div>` wrappers or table cells.
fn serialize_children_excluding(
    document: &scraper::Html,
    parent_id: ego_tree::NodeId,
    exclude: &std::collections::HashSet<ego_tree::NodeId>,
) -> String {
    let Some(node) = document.tree.get(parent_id) else {
        return String::new();
    };
    let mut out = String::new();

    for child in node.children() {
        if exclude.contains(&child.id()) {
            continue;
        }
        match child.value() {
            scraper::Node::Element(el) => {
                // Open tag
                out.push('<');
                out.push_str(el.name());
                for (k, v) in el.attrs() {
                    out.push(' ');
                    out.push_str(k);
                    out.push_str("=\"");
                    out.push_str(&v.replace('"', "&quot;"));
                    out.push('"');
                }
                out.push('>');
                // Recurse into children (may also contain excluded nodes)
                out.push_str(&serialize_children_excluding(document, child.id(), exclude));
                // Close tag (skip void elements)
                if !is_void_element(el.name()) {
                    out.push_str("</");
                    out.push_str(el.name());
                    out.push('>');
                }
            }
            scraper::Node::Text(text) => {
                out.push_str(text);
            }
            _ => {}
        }
    }

    out
}

/// Rebuild a document while excluding a set of node IDs from both `<head>` and `<body>`.
fn rebuild_document_excluding(
    document: &scraper::Html,
    original_html: &str,
    exclude: &std::collections::HashSet<ego_tree::NodeId>,
) -> String {
    if exclude.is_empty() {
        return original_html.to_string();
    }

    let head = scraper::Selector::parse("head")
        .ok()
        .and_then(|sel| document.select(&sel).next());
    let body = scraper::Selector::parse("body")
        .ok()
        .and_then(|sel| document.select(&sel).next());

    let Some(body) = body else {
        return original_html.to_string();
    };

    let head_html = head
        .map(|head| serialize_children_excluding(document, head.id(), exclude))
        .unwrap_or_default();
    let body_html = serialize_children_excluding(document, body.id(), exclude);

    if head_html.is_empty() {
        format!("<html><body>{body_html}</body></html>")
    } else {
        format!("<html><head>{head_html}</head><body>{body_html}</body></html>")
    }
}

/// Returns `true` for HTML void elements that must not have a closing tag.
fn is_void_element(name: &str) -> bool {
    matches!(
        name,
        "area"
            | "base"
            | "br"
            | "col"
            | "embed"
            | "hr"
            | "img"
            | "input"
            | "link"
            | "meta"
            | "param"
            | "source"
            | "track"
            | "wbr"
    )
}

/// Remove noise sections (advisories, cookie banners, etc.) from HTML.
///
/// Identifies elements whose class or id attributes match known noise patterns
/// and strips them using deep recursive exclusion. Unlike `strip_comment_sections`
/// (which only filters direct children of `<body>`), this handles noise elements
/// nested at any depth — critical for pages like `deps.rs` where advisory `<div>`
/// elements sit inside wrapper containers.
///
/// # Targeted patterns
///
/// - `#vulnerabilities`, `.advisories`, `.security-advisories` — security advisory sections
/// - `.cookie-banner`, `.consent-banner`, `.gdpr-banner` — cookie/consent UI
/// - `.newsletter-signup`, `.subscribe-form` — signup noise
/// - Headings (`<h1>`–`<h6>`) with noise IDs and all sibling elements that follow
#[must_use]
pub fn strip_noise_sections(html: &str) -> String {
    let document = scraper::Html::parse_document(html);

    // Collect node IDs of noise elements
    let mut noise_ids: std::collections::HashSet<ego_tree::NodeId> = document
        .select(&ATTR_SELECTOR)
        .filter(|el| {
            let class = el.value().attr("class").unwrap_or("");
            let id = el.value().attr("id").unwrap_or("");
            let combined = format!("{class} {id}").to_lowercase();
            is_noise_section(&combined)
        })
        .map(|el| el.id())
        .collect();

    // Special case: headings with noise IDs (e.g., <h3 id="vulnerabilities">)
    // Strip all subsequent siblings of such headings, since the advisory content
    // follows the heading as sibling <div> elements (deps.rs pattern).
    for heading in document.select(&HEADING_WITH_ID_SELECTOR) {
        let id = heading.value().attr("id").unwrap_or("");
        if is_noise_section(&id.to_lowercase()) {
            // Mark the heading itself
            noise_ids.insert(heading.id());
            // Mark all subsequent siblings
            let mut sibling = document
                .tree
                .get(heading.id())
                .and_then(|n| n.next_sibling());
            while let Some(sib) = sibling {
                noise_ids.insert(sib.id());
                sibling = sib.next_sibling();
            }
        }
    }

    rebuild_document_excluding(&document, html, &noise_ids)
}

/// Remove comment section DOM nodes from HTML before readability processing.
///
/// Sites like `LessWrong` have dense comment sections that confuse the readability
/// crate's scoring heuristics. We identify comment containers by common class/id
/// patterns and blank out their text content, preserving the DOM structure so
/// the readability crate can still score remaining nodes correctly.
///
/// This is a fast string-scan approach — not full DOM manipulation — so it
/// operates on the raw HTML string and handles nested structures by targeting
/// outermost comment containers.
///
/// # Panics
///
/// Never panics in practice — the `"div"` CSS selector used as a last-resort
/// fallback is always valid and cannot fail to parse.
#[must_use]
pub fn strip_comment_sections(html: &str) -> String {
    let document = scraper::Html::parse_document(html);

    // We can't mutate the scraper DOM, so we serialize element HTML of everything
    // EXCEPT comment containers, then reconstruct a valid document.
    // Build a set of comment container hashes to skip
    let comment_container_ids: std::collections::HashSet<u64> = document
        .select(&COMMENT_CONTAINER_SELECTOR)
        .filter(|el| is_comment_container(el))
        .map(|el| element_hash(el))
        .collect();

    if comment_container_ids.is_empty() {
        return html.to_string();
    }

    // Rebuild HTML without comment containers by serializing the body's
    // direct children that aren't comment containers, then wrapping.
    // For simplicity and correctness, we blank the comment node HTML.
    let body_html = document.select(&BODY_SELECTOR).next().map_or_else(
        || html.to_string(),
        |body| {
            // Filter children: collect HTML of non-comment children
            body.children()
                .filter_map(|child| {
                    let el_ref = scraper::ElementRef::wrap(child)?;
                    if comment_container_ids.contains(&element_hash(el_ref)) {
                        None
                    } else {
                        Some(el_ref.html())
                    }
                })
                .collect::<String>()
        },
    );

    // Preserve head and wrap body
    let head_html = scraper::Selector::parse("head")
        .ok()
        .and_then(|sel| document.select(&sel).next())
        .map(|h| h.html())
        .unwrap_or_default();

    format!("<html>{head_html}<body>{body_html}</body></html>")
}

/// Returns `true` if this element looks like a comment section container.
fn is_comment_container(element: &scraper::ElementRef<'_>) -> bool {
    let class = element.value().attr("class").unwrap_or("");
    let id = element.value().attr("id").unwrap_or("");
    let combined = format!("{class} {id}").to_lowercase();

    is_noise_section(&combined)
}

/// Returns `true` if a class/id string indicates a noise section that should
/// be stripped before readability processing.
///
/// Covers: comment sections, vulnerability advisories, cookie banners,
/// newsletter signup forms, and similar non-article content that tends to
/// dominate readability scoring due to high text density.
fn is_noise_section(combined: &str) -> bool {
    const NOISE_MARKERS: &[&str] = &[
        // Comment sections
        "comment-section",
        "comments-section",
        "comments-container",
        "comment-list",
        "comment-thread",
        "disqus",
        "discussion-section",
        "replies-section",
        // Security advisories / vulnerability sections (deps.rs, GitHub, etc.)
        "vulnerabilities",
        "advisories",
        "security-advisories",
        "advisory-list",
        // Cookie/consent banners
        "cookie-banner",
        "cookie-consent",
        "consent-banner",
        "gdpr-banner",
        // Newsletter/signup noise
        "newsletter-signup",
        "subscribe-form",
        // Footer noise
        "site-footer",
        "global-footer",
    ];

    NOISE_MARKERS.iter().any(|marker| combined.contains(marker))
}

/// Stable hash for a DOM element based on its outer HTML.
///
/// Used to identify specific elements across two passes over the parsed DOM.
fn element_hash(element: scraper::ElementRef<'_>) -> u64 {
    use std::hash::{Hash, Hasher};
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    // Hash the element's name + attributes as a structural fingerprint
    element.value().name().hash(&mut hasher);
    for (k, v) in element.value().attrs() {
        k.hash(&mut hasher);
        v.hash(&mut hasher);
    }
    hasher.finish()
}

/// Strip common markdown noise produced by html2md.
///
/// Removes artefacts that are technically valid markdown but carry no useful
/// information for an LLM or human reader:
///
/// - **Base64 data URIs**: Inline `![](data:image/...)` badges/icons (often huge)
/// - **Empty links**: `[](url)` from `<a><img/></a>` patterns (icon-only links)
/// - **Bare image noise in tables**: `[](url)` cells that are just crate icons
///
/// Applied as a final post-processing pass on the markdown string.
#[must_use]
fn clean_markdown_noise(md: &str) -> String {
    md.lines()
        .map(|line| {
            let mut cleaned = line.to_string();

            // Remove inline base64 data URIs: ![alt](data:...) or ![](data:...)
            while let Some(start) = cleaned.find("![") {
                // Find the matching ](data:
                let after_bang = &cleaned[start + 2..];
                if let Some(paren) = after_bang.find("](data:") {
                    // Find the closing )
                    let data_start = start + 2 + paren + 2; // past ](
                    if let Some(close) = cleaned[data_start..].find(')') {
                        let end = data_start + close + 1;
                        cleaned = format!("{}{}", &cleaned[..start], &cleaned[end..]);
                        continue;
                    }
                }
                break;
            }

            // Remove empty link artefacts: [](url) — no link text, just icon wrappers
            while let Some(start) = cleaned.find("[](") {
                if let Some(close) = cleaned[start + 3..].find(')') {
                    let end = start + 3 + close + 1;
                    let replacement = &cleaned[end..];
                    cleaned = format!("{}{}", cleaned[..start].trim_end(), replacement);
                    continue;
                }
                break;
            }

            cleaned
        })
        .filter(|l| {
            let trimmed = l.trim();
            // Drop lines that are entirely base64 data or became empty after cleaning
            !trimmed.is_empty()
                && !trimmed.starts_with("![](data:")
                && !trimmed.starts_with("```\n[![")
        })
        .collect::<Vec<_>>()
        .join("\n")
}

/// Convert HTML to markdown with boilerplate filtering (fallback).
///
/// Uses `html2md` for the heavy lifting, then post-processes to remove
/// common web boilerplate (cookie notices, navigation, privacy footers)
/// and collapse excessive whitespace.
pub fn html_to_markdown(html: &str) -> String {
    let md = html2md::parse_html(html);

    let lines: Vec<&str> = md
        .lines()
        .map(str::trim)
        .filter(|l| !l.is_empty())
        .filter(|l| !is_boilerplate(l))
        .collect();

    lines.join("\n")
}

/// Returns `true` if a line looks like web boilerplate.
pub fn is_boilerplate(line: &str) -> bool {
    // Preserve markdown links -- never filter lines containing link syntax
    if line.contains("](") {
        return false;
    }

    let lower = line.to_lowercase();
    lower.contains("skip to content")
        || lower.contains("cookie")
        || lower.contains("privacy policy")
        || lower.contains("terms of service")
        || lower.starts_with("©")
        || lower.starts_with("copyright")
        || (lower.len() < 3 && !lower.chars().any(char::is_alphanumeric))
}

#[cfg(test)]
mod tests {
    use super::{
        HtmlConversionOptions, html_to_markdown_with_url, html_to_markdown_with_url_and_fetcher,
        html_to_markdown_with_url_and_sources, is_thin_content, strip_embedded_data_sections,
        strip_hidden_sections, strip_noise_sections,
    };

    #[test]
    fn is_thin_content_returns_false_for_small_html_below_threshold() {
        // GIVEN: HTML smaller than the 5 KB minimum threshold
        // WHEN: checking thin content
        let result = is_thin_content(1_000, 10);
        // THEN: not considered thin (threshold not reached)
        assert!(!result);
    }

    #[test]
    fn is_thin_content_returns_false_for_adequate_markdown() {
        // GIVEN: large HTML but markdown of 900 chars (>= 800 minimum)
        // WHEN: checking thin content
        let result = is_thin_content(10_000, 900);
        // THEN: not considered thin (markdown exceeds the partial-content floor)
        assert!(!result);
    }

    #[test]
    fn is_thin_content_returns_true_for_thin_spa_page() {
        // GIVEN: 50 KB HTML with only 50 chars of markdown output
        // WHEN: checking thin content
        let result = is_thin_content(50_000, 50);
        // THEN: flagged as thin (50/50000 = 0.1%, well below 2% threshold)
        assert!(result);
    }

    #[test]
    fn is_thin_content_returns_true_for_partial_extraction_below_new_floor() {
        // GIVEN: a JS-heavy page where only a short excerpt was extracted
        // WHEN: checking thin content with 600 chars from 50 KB HTML
        let result = is_thin_content(50_000, 600);
        // THEN: still flagged as thin so fallback recovery can run
        assert!(result);
    }

    #[test]
    fn is_thin_content_boundary_at_799_chars_is_thin() {
        // GIVEN: 50 KB HTML with 799 chars of markdown (one below the new 800-char boundary)
        // WHEN: checking thin content
        let result = is_thin_content(50_000, 799);
        // THEN: flagged as thin (799 < 800 minimum, ratio < 2%)
        assert!(result);
    }

    #[test]
    fn is_thin_content_boundary_at_800_chars_is_not_thin() {
        // GIVEN: 50 KB HTML with exactly 800 chars of markdown (at the boundary)
        // WHEN: checking thin content
        let result = is_thin_content(50_000, 800);
        // THEN: NOT flagged as thin (800 >= 800 minimum satisfies the condition)
        assert!(!result);
    }

    #[test]
    fn strip_embedded_data_sections_removes_bootstrap_but_preserves_jsonld() {
        let html = r#"<html>
            <head>
                <style>.hidden { display: none; }</style>
                <script>window.__BOOT__ = {"title":"Hidden"};</script>
                <script type="application/ld+json">
                    {"@context":"https://schema.org","@type":"Article","description":"Structured fallback"}
                </script>
            </head>
            <body>
                <article><p>Visible content</p></article>
                <template><div>Template payload</div></template>
                <script>window.__NEXT_DATA__ = {"props":{"pageProps":{"body":"noise"}}};</script>
            </body>
        </html>"#;

        let result = strip_embedded_data_sections(html);

        assert!(result.contains("Visible content"));
        assert!(!result.contains("window.__BOOT__"));
        assert!(!result.contains("Template payload"));
        assert!(!result.contains("__NEXT_DATA__"));
        assert!(!result.contains("display: none"));
        assert!(result.contains("application/ld+json"));
        assert!(result.contains("Structured fallback"));
    }

    #[test]
    fn html_to_markdown_with_url_drops_inline_script_bootstrap_noise() {
        let bootstrap = "const boot='very noisy js bootstrap';".repeat(120);
        let html = format!(
            "<html><body><div id='app'>Loading…</div><script>{bootstrap}</script></body></html>"
        );

        let markdown = html_to_markdown_with_url(&html, None);

        assert!(!markdown.contains("very noisy js bootstrap"));
        assert!(!markdown.contains("const boot="));
    }

    #[test]
    fn html_to_markdown_uses_jina_fallback_when_enabled() {
        let bootstrap = "const boot='very noisy js bootstrap';".repeat(220);
        let html = format!(
            "<html><body><div id='app'>Loading…</div><script>{bootstrap}</script></body></html>"
        );
        assert!(html.len() > 5_000);
        let calls = std::cell::Cell::new(0);

        let markdown = html_to_markdown_with_url_and_fetcher(
            &html,
            Some("https://example.com/article"),
            HtmlConversionOptions::default(),
            |url| {
                calls.set(calls.get() + 1);
                Some(format!("Recovered remotely from {url}"))
            },
        );

        assert_eq!(calls.get(), 1);
        assert_eq!(
            markdown,
            "Recovered remotely from https://example.com/article"
        );
    }

    #[test]
    fn thin_spa_extraction_uses_jina_fallback_when_enabled() {
        let code_sample = "from vllm import LLM\\n".repeat(18);
        let bootstrap = "const boot='very noisy js bootstrap';".repeat(1_000);
        let html = format!(
            "<html><body><div id='app'>Loading…</div><script>{bootstrap}</script></body></html>"
        );
        let calls = std::cell::Cell::new(0);

        let markdown = html_to_markdown_with_url_and_sources(
            &html,
            Some("https://example.com/article"),
            HtmlConversionOptions::default(),
            |_html| Some(code_sample.clone()),
            |url| {
                calls.set(calls.get() + 1);
                Some(format!("Recovered remotely from {url}"))
            },
        );

        assert_eq!(calls.get(), 1);
        assert_eq!(
            markdown,
            "Recovered remotely from https://example.com/article"
        );
    }

    #[test]
    fn html_to_markdown_skips_jina_fallback_when_disabled() {
        let bootstrap = "const boot='very noisy js bootstrap';".repeat(220);
        let html = format!(
            "<html><body><div id='app'>Loading…</div><script>{bootstrap}</script></body></html>"
        );
        assert!(html.len() > 5_000);
        let calls = std::cell::Cell::new(0);

        let markdown = html_to_markdown_with_url_and_fetcher(
            &html,
            Some("https://example.com/article"),
            HtmlConversionOptions {
                allow_spa_extraction: true,
                allow_jina_fallback: false,
            },
            |_url| {
                calls.set(calls.get() + 1);
                Some("Recovered remotely".to_string())
            },
        );

        assert_eq!(calls.get(), 0);
        assert!(markdown.contains("Loading"));
        assert!(!markdown.contains("Recovered remotely"));
    }

    #[test]
    fn thin_spa_extraction_is_retained_when_jina_fallback_disabled() {
        let code_sample = "from vllm import LLM\\n".repeat(18);
        let bootstrap = "const boot='very noisy js bootstrap';".repeat(1_000);
        let html = format!(
            "<html><body><div id='app'>Loading…</div><script>{bootstrap}</script></body></html>"
        );
        let calls = std::cell::Cell::new(0);

        let markdown = html_to_markdown_with_url_and_sources(
            &html,
            Some("https://example.com/article"),
            HtmlConversionOptions {
                allow_spa_extraction: true,
                allow_jina_fallback: false,
            },
            |_html| Some(code_sample.clone()),
            |_url| {
                calls.set(calls.get() + 1);
                Some("Recovered remotely".to_string())
            },
        );

        assert_eq!(calls.get(), 0);
        assert!(markdown.contains("from vllm import LLM"));
        assert!(!markdown.contains("Recovered remotely"));
    }

    #[test]
    fn html_to_markdown_can_disable_spa_extraction() {
        let article_body = "A".repeat(320);
        let html = format!(
            r#"<html><body>
                <p>Loading…</p>
                <script id="__NEXT_DATA__" type="application/json">
                    {{"props":{{"pageProps":{{"body":"{article_body}"}}}}}}
                </script>
            </body></html>"#
        );

        let markdown = html_to_markdown_with_url_and_fetcher(
            &html,
            Some("https://example.com/article"),
            HtmlConversionOptions {
                allow_spa_extraction: false,
                allow_jina_fallback: false,
            },
            |_url| Some("Recovered remotely".to_string()),
        );

        assert!(!markdown.contains(&article_body));
        assert!(markdown.contains("Loading"));
    }

    #[test]
    fn strip_hidden_sections_removes_closed_details() {
        let html = r"<html><body>
            <h1>Status</h1>
            <p>All good</p>
            <details><summary>Advisory</summary><p>CVE-2024-1234</p></details>
        </body></html>";
        let result = strip_hidden_sections(html);
        assert!(!result.contains("CVE-2024-1234"));
        assert!(result.contains("All good"));
        assert!(result.contains("Status"));
    }

    #[test]
    fn strip_hidden_sections_preserves_open_details() {
        let html = r"<html><body>
            <details open><summary>Visible</summary><p>Important info</p></details>
            <details><summary>Hidden</summary><p>Secret</p></details>
        </body></html>";
        let result = strip_hidden_sections(html);
        assert!(result.contains("Important info"));
        assert!(!result.contains("Secret"));
    }

    #[test]
    fn strip_hidden_sections_removes_noscript() {
        let html = r"<html><body>
            <p>Main content</p>
            <noscript><p>Enable JavaScript</p></noscript>
        </body></html>";
        let result = strip_hidden_sections(html);
        assert!(result.contains("Main content"));
        assert!(!result.contains("Enable JavaScript"));
    }

    #[test]
    fn strip_hidden_sections_removes_closed_dialog() {
        let html = r"<html><body>
            <p>Page</p>
            <dialog><p>Modal content</p></dialog>
            <dialog open><p>Visible modal</p></dialog>
        </body></html>";
        let result = strip_hidden_sections(html);
        assert!(result.contains("Page"));
        assert!(!result.contains("Modal content"));
        assert!(result.contains("Visible modal"));
    }

    #[test]
    fn strip_hidden_sections_handles_nested_details_in_divs() {
        let html = r#"<html><body>
            <div class="content"><p>Real content</p></div>
            <div class="advisories">
                <details><summary>CVE-1</summary><p>Bad thing 1</p></details>
                <details><summary>CVE-2</summary><p>Bad thing 2</p></details>
            </div>
        </body></html>"#;
        let result = strip_hidden_sections(html);
        assert!(result.contains("Real content"));
        assert!(!result.contains("Bad thing 1"));
        assert!(!result.contains("Bad thing 2"));
    }

    #[test]
    fn strip_hidden_sections_noop_when_no_hidden_elements() {
        let html = r"<html><body><p>Just text</p></body></html>";
        let result = strip_hidden_sections(html);
        assert!(result.contains("Just text"));
    }

    #[test]
    fn strip_noise_sections_removes_vulnerabilities_heading_and_siblings() {
        // Simulates deps.rs structure: h3#vulnerabilities followed by advisory boxes
        let html = r#"<html><body>
            <table><tr><td>reqwest</td><td>up to date</td></tr></table>
            <h3 id="vulnerabilities">Security Vulnerabilities</h3>
            <div class="box"><p>CVE-2022-24713 regex advisory</p></div>
            <div class="box"><p>chrono segfault advisory</p></div>
        </body></html>"#;
        let result = strip_noise_sections(html);
        assert!(result.contains("reqwest"));
        assert!(result.contains("up to date"));
        assert!(!result.contains("CVE-2022-24713"));
        assert!(!result.contains("chrono segfault"));
        assert!(!result.contains("Security Vulnerabilities"));
    }

    #[test]
    fn strip_noise_sections_removes_advisory_by_class() {
        let html = r#"<html><body>
            <p>Main content</p>
            <div class="advisories"><p>RUSTSEC-2020-0159</p></div>
        </body></html>"#;
        let result = strip_noise_sections(html);
        assert!(result.contains("Main content"));
        assert!(!result.contains("RUSTSEC-2020-0159"));
    }

    #[test]
    fn strip_noise_sections_removes_cookie_banner() {
        let html = r#"<html><body>
            <article><p>Article text</p></article>
            <div class="cookie-banner"><p>Accept cookies</p></div>
        </body></html>"#;
        let result = strip_noise_sections(html);
        assert!(result.contains("Article text"));
        assert!(!result.contains("Accept cookies"));
    }

    #[test]
    fn strip_noise_sections_preserves_all_when_no_noise() {
        let html = r"<html><body><p>Clean page</p></body></html>";
        let result = strip_noise_sections(html);
        assert!(result.contains("Clean page"));
    }

    #[test]
    fn ghost_blog_ordered_list_not_truncated() {
        // Ghost CMS article with <ol><li> content.
        // html2md truncates ordered lists; the pipeline must detect this
        // and fall back to plain text to preserve the full article.
        use super::html_to_markdown_with_url;

        let html = r#"
            <html>
            <head><title>Porting Software</title></head>
            <body>
                <header><nav>Site Nav</nav></header>
                <main class="site-main">
                    <article class="gh-article post tag-ai">
                        <header class="gh-article-header">
                            <h1 class="gh-article-title">porting software has been trivial</h1>
                        </header>
                        <div class="gh-content gh-canvas">
                            <p>This one is short and sweet. if you want to port a codebase from one language to another here's the approach:</p>
                            <ol>
                                <li>Run a ralph loop which compresses all tests into specs which looks similar to study every file in tests using separate subagents and document in specs and link the implementation as citations in the specification</li>
                                <li>Then do a separate Ralph loop for all product functionality ensuring there are citations to the specification. Study every file in src using separate subagents per file and link the implementation as citations in the specification</li>
                                <li>Once you have that within the same repo run a Ralph loop to create a TODO file and then execute a classic ralph doing just one thing and the most important thing per loop. Remind the agent that it can study the specifications and follow the citations to reference source code.</li>
                                <li>For best outcomes you wanna configure your target language to have strict compilation</li>
                            </ol>
                            <p>The key theory here is usage of citations in the specifications which tease the file_read tool to study the original implementation during stage 3. Reducing stage 1 and stage 2 to specs is the precursor which transforms a code base into high level PRDs without coupling the implementation from the source language.</p>
                        </div>
                    </article>
                </main>
                <section class="newsletter-signup"><p>Subscribe</p></section>
                <footer>Copyright</footer>
            </body>
            </html>
        "#;

        let md = html_to_markdown_with_url(html, Some("https://ghuntley.com/porting/"));

        // Must contain the conclusion paragraph (the part html2md truncates)
        assert!(
            md.contains("high level PRDs without coupling"),
            "Missing conclusion paragraph in markdown output ({} chars): {}",
            md.len(),
            md
        );
        // Must contain list items
        assert!(
            md.contains("Ralph loop"),
            "Missing list content in markdown output: {}",
            md
        );
    }

    #[test]
    fn ghost_blog_real_html_not_truncated() {
        // Test with actual Ghost CMS HTML structure that triggers html2md truncation.
        // The key difference from the simplified test above: the <em> tags inside
        // the <li> elements and the inline <ol> (no whitespace between tags).
        use super::html_to_markdown_with_url;

        let html = r#"<html>
<head><title>porting software has been trivial for a while now. here's how you do it.</title></head>
<body>
<header><nav>Site Nav</nav></header>
<main class="site-main">
<article class="gh-article post tag-ai">
<header class="gh-article-header"><h1 class="gh-article-title">porting software has been trivial</h1></header>
<div class="gh-content gh-canvas">
            <p>This one is short and sweet. if you want to port a codebase from one language to another here's the approach:</p><ol><li>Run a ralph loop which compresses all tests into /specs/<em>.md which looks similar to "study every file in tests/</em>* using separate subagents and document in /specs/*.md and link the implementation as citations in the specification"</li><li>Then do a separate Ralph loop for all product functionality - ensuring there's citations to the specification. "study every file in src/* using seperate subagents per file and link the implementation as citations in the specification"</li><li>Once you have that - within the same repo run a Ralph loop to create a TODO file and then execute a classic ralph - doing just one thing and the most important thing per loop. Remind the agent that it can study the specifications and follow the citations to reference source code.</li><li>For best outcomes you wanna configure your target language to have strict compilation </li></ol><p>The key theory here is usage of citations in the specifications which tease the file_read tool to study the original implementation during stage 3. Reducing stage 1 and stage 2 to specs is the precursor which transforms a code base into high level PRDs without coupling the implementation from the source language.</p>
        </div>
</article>
</main>
<section class="newsletter-signup"><h3>Subscribe</h3><p>Join subscribers</p></section>
<footer><p>Copyright 2026</p></footer>
</body></html>"#;

        let md = html_to_markdown_with_url(html, Some("https://ghuntley.com/porting/"));

        // Must contain the conclusion paragraph
        assert!(
            md.contains("high level PRDs without coupling"),
            "Missing conclusion paragraph in markdown output ({} chars): {}",
            md.len(),
            md
        );
        // Must contain list item content
        assert!(
            md.contains("Ralph loop") || md.contains("ralph loop"),
            "Missing list content in markdown output: {}",
            md
        );
        // Should be substantial
        assert!(
            md.len() > 500,
            "Markdown too short: {} chars. Content: {}",
            md.len(),
            md
        );
    }
}