mdx-gen 0.0.4

A robust Rust library for processing Markdown and converting it to HTML with support for custom blocks, enhanced table formatting, and flexible configuration options.
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
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
//! Core Markdown processing functionality.
//!
//! This module handles the conversion of Markdown content into HTML,
//! with support for custom blocks, enhanced tables, and syntax highlighting.
//!
//! ## Processing Pipeline
//!
//! 1. **Parse** — Markdown source → comrak AST (arena-allocated).
//! 2. **Transform** — Walk the AST to rewrite custom-block `HtmlBlock`
//!    nodes in-place.
//! 3. **Render** — Convert the (possibly modified) AST to HTML, using
//!    comrak's plugin system for syntax highlighting.
//! 4. **Enhance** — Post-process table HTML for responsive wrappers and
//!    alignment classes.
//! 5. **Sanitize** — When `allow_unsafe_html` is `false`, run ammonia to
//!    strip dangerous tags while preserving safe structural markup.

use crate::error::MarkdownError;
use crate::extensions::{
    collect_headings, enhance_table_nodes, process_custom_block_nodes,
    CustomBlockConfig, Heading,
};
use comrak::options::{Plugins, RenderPlugins};
use comrak::{Arena, Options};
use log::{debug, info, warn};
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::io::Write;
use std::sync::LazyLock;

#[cfg(feature = "syntax_highlighting")]
use crate::highlight::SyntectAdapter;

/// Default maximum input size: 1 MiB.
pub const DEFAULT_MAX_INPUT_SIZE: usize = 1_048_576;

/// Options for configuring Markdown processing behavior.
#[derive(Clone)]
pub struct MarkdownOptions<'a> {
    /// Options for the underlying Comrak Markdown parser.
    pub comrak_options: Options<'a>,
    /// Enable or disable processing of custom blocks.
    pub enable_custom_blocks: bool,
    /// Enable or disable syntax highlighting for code blocks.
    pub enable_syntax_highlighting: bool,
    /// Enable or disable enhanced table formatting.
    pub enable_enhanced_tables: bool,
    /// Optional custom theme for syntax highlighting.
    pub syntax_theme: Option<String>,
    /// Allow raw HTML pass-through in Markdown output.
    ///
    /// When `true`, raw HTML in the Markdown source is passed through
    /// unchanged. When `false`, output is sanitized with ammonia to
    /// strip dangerous tags while preserving safe structural HTML
    /// (our generated alert divs, tables, code blocks, etc.).
    pub allow_unsafe_html: bool,
    /// Configuration for custom block rendering.
    pub custom_block_config: CustomBlockConfig,
    /// Maximum input size in bytes. `0` means no limit.
    pub max_input_size: usize,
    /// Enable automatic `id` attributes on headings for anchor links.
    ///
    /// When `Some(prefix)`, headings get `id="prefix-slug"` attributes.
    /// Use `Some("")` for bare `id="slug"` without a prefix.
    /// `None` disables header IDs (default).
    pub header_ids: Option<String>,
    /// Optional extensions to the default HTML sanitizer allow-list.
    ///
    /// When `None`, the cached default sanitizer is used — the hot
    /// path. When `Some`, a fresh `ammonia::Builder` is constructed
    /// per call that merges the defaults with the extras declared in
    /// [`SanitizerConfig`].
    pub sanitizer_config: Option<SanitizerConfig>,
    /// Rewrite fenced code blocks tagged `mermaid`, `geojson`,
    /// `topojson`, or `stl` into sanitizer-safe containers that a
    /// client-side JS hydrator (see
    /// [`crate::hydration_script_html`]) replaces with inline SVG.
    ///
    /// Off by default so existing users with plain `mermaid` code
    /// blocks continue to see syntax-highlighted source. Opt in via
    /// [`MarkdownOptions::with_diagrams`].
    pub enable_diagrams: bool,
}

impl<'a> Default for MarkdownOptions<'a> {
    fn default() -> Self {
        // Keep the default internally consistent: enhanced tables
        // depend on comrak's `extension.table`, so enable both
        // together. Callers who want either piece off can disable
        // via the builder.
        let mut comrak_options = Options::default();
        comrak_options.extension.table = true;
        Self {
            comrak_options,
            enable_custom_blocks: true,
            enable_syntax_highlighting: true,
            enable_enhanced_tables: true,
            syntax_theme: None,
            allow_unsafe_html: true,
            custom_block_config: CustomBlockConfig::default(),
            max_input_size: DEFAULT_MAX_INPUT_SIZE,
            header_ids: None,
            sanitizer_config: None,
            enable_diagrams: false,
        }
    }
}

impl<'a> MarkdownOptions<'a> {
    /// Creates a new instance with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Enables or disables custom blocks.
    pub fn with_custom_blocks(mut self, enable: bool) -> Self {
        self.enable_custom_blocks = enable;
        self
    }

    /// Enables or disables syntax highlighting for code blocks.
    pub fn with_syntax_highlighting(mut self, enable: bool) -> Self {
        self.enable_syntax_highlighting = enable;
        self
    }

    /// Enables or disables enhanced table formatting.
    pub fn with_enhanced_tables(mut self, enable: bool) -> Self {
        self.enable_enhanced_tables = enable;
        self
    }

    /// Sets a custom theme for syntax highlighting.
    pub fn with_custom_theme(mut self, theme: String) -> Self {
        self.syntax_theme = Some(theme);
        self
    }

    /// Sets custom Comrak options.
    ///
    /// Also syncs `allow_unsafe_html` from `render.unsafe`.
    pub fn with_comrak_options(mut self, options: Options<'a>) -> Self {
        self.allow_unsafe_html = options.render.r#unsafe;
        self.comrak_options = options;
        self
    }

    /// Enables or disables raw HTML pass-through.
    ///
    /// This is the authoritative control. Call **after**
    /// `with_comrak_options` if you need to override.
    pub fn with_unsafe_html(mut self, enable: bool) -> Self {
        self.allow_unsafe_html = enable;
        self
    }

    /// Sets the custom block configuration.
    pub fn with_custom_block_config(
        mut self,
        config: CustomBlockConfig,
    ) -> Self {
        self.custom_block_config = config;
        self
    }

    /// Sets the maximum input size in bytes. `0` means no limit.
    pub fn with_max_input_size(mut self, size: usize) -> Self {
        self.max_input_size = size;
        self
    }

    /// Enables automatic `id` attributes on headings.
    ///
    /// Pass `""` for bare slugs, or a prefix like `"user-content-"`
    /// to namespace them (GitHub-style).
    pub fn with_header_ids(
        mut self,
        prefix: impl Into<String>,
    ) -> Self {
        self.header_ids = Some(prefix.into());
        self
    }

    /// Extends the HTML sanitizer allow-list.
    ///
    /// Setting this disables the cached default sanitizer for calls
    /// made with these options — a fresh `ammonia::Builder` is
    /// constructed per call that merges the defaults with the extras.
    /// Only used when `allow_unsafe_html` is `false`.
    pub fn with_sanitizer_config(
        mut self,
        config: SanitizerConfig,
    ) -> Self {
        self.sanitizer_config = Some(config);
        self
    }

    /// Enables or disables diagram code-block rendering (mermaid,
    /// geojson, topojson, stl). See [`crate::diagrams`] for the
    /// supported info strings and the client-side hydration
    /// contract.
    pub fn with_diagrams(mut self, enable: bool) -> Self {
        self.enable_diagrams = enable;
        self
    }

    /// Validates that options are internally consistent and that
    /// every string the pipeline will splice into HTML is well-
    /// formed.
    ///
    /// Uses [`crate::validation::Validator`] so every failing
    /// check is reported in one pass — callers get the full list
    /// of problems, not just the first. Each entry in the returned
    /// `Vec` is `(field_name, ValidationError)`.
    ///
    /// # Checks
    ///
    /// 1. `enhanced_tables` requires `comrak.extension.table`.
    /// 2. `syntax_theme`, if set, must name a theme bundled with
    ///    syntect (feature-gated).
    /// 3. `syntax_theme` set but `enable_syntax_highlighting =
    ///    false` is a silent no-op — rejected so the mistake
    ///    surfaces.
    /// 4. `sanitizer_config` set but `allow_unsafe_html = true`
    ///    skips sanitization entirely — rejected.
    /// 5. `header_ids` prefix must not contain whitespace or any
    ///    of `" ' < > & =` (would break the emitted `id="…"`).
    /// 6. `sanitizer_config.extra_tags` / `extra_tag_attributes`
    ///    keys must be valid HTML names; attribute lists must
    ///    contain only valid HTML names.
    /// 7. `sanitizer_config.extra_generic_attributes` must be
    ///    valid HTML names.
    /// 8. `sanitizer_config.extra_allowed_classes` keys must be
    ///    valid HTML names; class values must be non-empty and
    ///    free of whitespace / quote characters.
    /// 9. `custom_block_config` override values must be non-empty
    ///    and free of whitespace / quote characters (class
    ///    overrides) or non-empty (title overrides).
    ///
    /// # Errors
    ///
    /// Returns `Err(errors)` when any check fails. The pipeline
    /// converts the list into a single
    /// [`MarkdownError::InvalidOptionsError`] via the `From` impl
    /// in [`crate::error`].
    pub fn validate(
        &self,
    ) -> Result<(), Vec<(String, crate::validation::ValidationError)>>
    {
        use crate::validation::{ValidationError, Validator};

        let mut v = Validator::new();

        // 1. enhanced_tables requires comrak.extension.table
        v.check("enable_enhanced_tables", || {
            if self.enable_enhanced_tables
                && !self.comrak_options.extension.table
            {
                Err(ValidationError::Custom(
                    "enhanced_tables = true requires comrak_options.extension.table = true"
                        .into(),
                ))
            } else {
                Ok(())
            }
        });

        // 2. syntax_theme must be a bundled theme.
        #[cfg(feature = "syntax_highlighting")]
        v.check("syntax_theme", || {
            if let Some(ref theme) = self.syntax_theme {
                let available =
                    crate::highlight::SyntectAdapter::available_themes(
                    );
                if !available.contains(&theme.as_str()) {
                    return Err(ValidationError::NotInSet {
                        allowed: available
                            .iter()
                            .map(|s| (*s).to_string())
                            .collect(),
                    });
                }
            }
            Ok(())
        });

        // 3. syntax_theme + highlighter disabled is a no-op.
        v.check("syntax_theme", || {
            if !self.enable_syntax_highlighting
                && self.syntax_theme.is_some()
            {
                Err(ValidationError::Custom(
                    "syntax_theme is set but enable_syntax_highlighting = false (theme would be ignored)"
                        .into(),
                ))
            } else {
                Ok(())
            }
        });

        // 4. sanitizer_config + unsafe_html is a no-op (sanitizer
        //    never runs when unsafe_html is true).
        v.check("sanitizer_config", || {
            if self.allow_unsafe_html && self.sanitizer_config.is_some()
            {
                Err(ValidationError::Custom(
                    "sanitizer_config is set but allow_unsafe_html = true (sanitizer is skipped)"
                        .into(),
                ))
            } else {
                Ok(())
            }
        });

        // 5. header_ids prefix — no chars that would escape out of
        //    the `id="…"` attribute.
        v.check("header_ids", || {
            if let Some(ref prefix) = self.header_ids {
                if let Some(c) = prefix.chars().find(|c| {
                    c.is_whitespace()
                        || matches!(
                            c,
                            '"' | '\''
                                | '<'
                                | '>'
                                | '&'
                                | '='
                        )
                }) {
                    return Err(ValidationError::InvalidPattern {
                        pattern: format!(
                            "no whitespace or HTML-special chars (found {c:?})"
                        ),
                    });
                }
            }
            Ok(())
        });

        // 6. & 7. & 8. — SanitizerConfig.
        if let Some(ref cfg) = self.sanitizer_config {
            check_tag_list(
                &mut v,
                "sanitizer_config.extra_tags",
                &cfg.extra_tags,
            );
            check_tag_attr_map(
                &mut v,
                "sanitizer_config.extra_tag_attributes",
                &cfg.extra_tag_attributes,
            );
            check_attr_list(
                &mut v,
                "sanitizer_config.extra_generic_attributes",
                &cfg.extra_generic_attributes,
            );
            check_allowed_classes_map(
                &mut v,
                "sanitizer_config.extra_allowed_classes",
                &cfg.extra_allowed_classes,
            );
        }

        // 9. CustomBlockConfig override values.
        for (block_type, class) in
            &self.custom_block_config.class_overrides
        {
            let field = format!(
                "custom_block_config.class_overrides[{block_type:?}]"
            );
            let c = class.clone();
            v.check(&field, move || {
                if c.is_empty() {
                    return Err(ValidationError::Empty);
                }
                if let Some(ch) = c.chars().find(|c| {
                    c.is_whitespace() || matches!(c, '"' | '\'')
                }) {
                    return Err(ValidationError::InvalidPattern {
                        pattern: format!(
                            "non-empty, no whitespace or quotes (found {ch:?})"
                        ),
                    });
                }
                Ok(())
            });
        }
        for (block_type, title) in
            &self.custom_block_config.title_overrides
        {
            let field = format!(
                "custom_block_config.title_overrides[{block_type:?}]"
            );
            let t = title.clone();
            v.check(&field, move || {
                if t.trim().is_empty() {
                    return Err(ValidationError::Empty);
                }
                Ok(())
            });
        }

        v.finish()
    }
}

/// ASCII-alphabetic first character, ASCII alphanumeric + `-` + `_`
/// after. Empty strings rejected. Conservative shape for HTML tag
/// and attribute names.
fn is_html_name(s: &str) -> bool {
    let mut chars = s.chars();
    match chars.next() {
        Some(c) if c.is_ascii_alphabetic() => {}
        _ => return false,
    }
    chars.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
}

fn check_tag_list(
    v: &mut crate::validation::Validator,
    field: &str,
    tags: &[String],
) {
    for (i, tag) in tags.iter().enumerate() {
        let f = format!("{field}[{i}]");
        let t = tag.clone();
        v.check(&f, move || {
            if !is_html_name(&t) {
                Err(crate::validation::ValidationError::InvalidPattern {
                    pattern: format!(
                        "valid HTML tag name (got {t:?})"
                    ),
                })
            } else {
                Ok(())
            }
        });
    }
}

fn check_attr_list(
    v: &mut crate::validation::Validator,
    field: &str,
    attrs: &[String],
) {
    for (i, attr) in attrs.iter().enumerate() {
        let f = format!("{field}[{i}]");
        let a = attr.clone();
        v.check(&f, move || {
            if !is_html_name(&a) {
                Err(crate::validation::ValidationError::InvalidPattern {
                    pattern: format!(
                        "valid HTML attribute name (got {a:?})"
                    ),
                })
            } else {
                Ok(())
            }
        });
    }
}

fn check_tag_attr_map(
    v: &mut crate::validation::Validator,
    field: &str,
    map: &HashMap<String, Vec<String>>,
) {
    for (tag, attrs) in map {
        let f_tag = format!("{field}.{tag}");
        let t = tag.clone();
        v.check(&f_tag, move || {
            if !is_html_name(&t) {
                Err(crate::validation::ValidationError::InvalidPattern {
                    pattern: format!(
                        "valid HTML tag name (got {t:?})"
                    ),
                })
            } else {
                Ok(())
            }
        });
        check_attr_list(v, &f_tag, attrs);
    }
}

fn check_allowed_classes_map(
    v: &mut crate::validation::Validator,
    field: &str,
    map: &HashMap<String, Vec<String>>,
) {
    for (tag, classes) in map {
        let f_tag = format!("{field}.{tag}");
        let t = tag.clone();
        v.check(&f_tag, move || {
            if !is_html_name(&t) {
                Err(crate::validation::ValidationError::InvalidPattern {
                    pattern: format!(
                        "valid HTML tag name (got {t:?})"
                    ),
                })
            } else {
                Ok(())
            }
        });
        for (i, class) in classes.iter().enumerate() {
            let f = format!("{f_tag}[{i}]");
            let c = class.clone();
            v.check(&f, move || {
                if c.is_empty() {
                    return Err(
                        crate::validation::ValidationError::Empty,
                    );
                }
                if let Some(ch) = c.chars().find(|c| {
                    c.is_whitespace() || matches!(c, '"' | '\'')
                }) {
                    return Err(
                        crate::validation::ValidationError::InvalidPattern {
                            pattern: format!(
                                "non-empty, no whitespace or quotes (got {ch:?})"
                            ),
                        },
                    );
                }
                Ok(())
            });
        }
    }
}

impl fmt::Debug for MarkdownOptions<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MarkdownOptions")
            .field("enable_custom_blocks", &self.enable_custom_blocks)
            .field(
                "enable_syntax_highlighting",
                &self.enable_syntax_highlighting,
            )
            .field(
                "enable_enhanced_tables",
                &self.enable_enhanced_tables,
            )
            .field("syntax_theme", &self.syntax_theme)
            .field("allow_unsafe_html", &self.allow_unsafe_html)
            .field("max_input_size", &self.max_input_size)
            .field("header_ids", &self.header_ids)
            .field("sanitizer_config", &self.sanitizer_config)
            .field("enable_diagrams", &self.enable_diagrams)
            .finish()
    }
}

// ── Sanitizer configuration ─────────────────────────────────────────

/// User-supplied extensions to the default HTML sanitizer allow-list.
///
/// Each field is additive: values here are merged on top of the
/// defaults that ship with `mdx-gen`. Wire an instance into
/// [`MarkdownOptions::with_sanitizer_config`].
#[derive(Debug, Clone, Default)]
pub struct SanitizerConfig {
    /// Additional tags to allow (beyond the defaults).
    pub extra_tags: Vec<String>,
    /// Additional attributes per tag, in the form `tag -> attrs`.
    pub extra_tag_attributes: HashMap<String, Vec<String>>,
    /// Additional generic attributes that may appear on any tag.
    pub extra_generic_attributes: Vec<String>,
    /// Additional allowed class values per tag.
    pub extra_allowed_classes: HashMap<String, Vec<String>>,
}

impl SanitizerConfig {
    /// Creates a new, empty config (equivalent to `Default`).
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds one extra tag to the allow-list.
    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
        self.extra_tags.push(tag.into());
        self
    }

    /// Adds one extra attribute for a specific tag.
    pub fn with_tag_attribute(
        mut self,
        tag: impl Into<String>,
        attr: impl Into<String>,
    ) -> Self {
        self.extra_tag_attributes
            .entry(tag.into())
            .or_default()
            .push(attr.into());
        self
    }

    /// Adds one extra generic attribute (applies to any allowed tag).
    pub fn with_generic_attribute(
        mut self,
        attr: impl Into<String>,
    ) -> Self {
        self.extra_generic_attributes.push(attr.into());
        self
    }

    /// Adds one extra allowed class value for a specific tag.
    pub fn with_allowed_class(
        mut self,
        tag: impl Into<String>,
        class: impl Into<String>,
    ) -> Self {
        self.extra_allowed_classes
            .entry(tag.into())
            .or_default()
            .push(class.into());
        self
    }
}

/// Creates a convenience set of options with all features enabled.
pub fn default_markdown_options() -> MarkdownOptions<'static> {
    MarkdownOptions::new()
        .with_custom_blocks(true)
        .with_syntax_highlighting(true)
        .with_enhanced_tables(true)
        .with_comrak_options({
            let mut opts = Options::default();
            opts.extension.table = true;
            opts
        })
        .with_unsafe_html(true)
}

// ── Core processing pipeline ────────────────────────────────────────

/// Processes the input Markdown content and converts it into HTML.
///
/// The pipeline:
/// 1. Validate options and check resource limits.
/// 2. Parse Markdown to a comrak AST.
/// 3. (Optional) Transform custom-block `HtmlBlock` nodes in the AST.
/// 4. Render to HTML, using comrak's syntax-highlighting plugin.
/// 5. (Optional) Enhance tables with responsive wrappers.
/// 6. (Optional) Sanitize HTML when `allow_unsafe_html` is `false`.
pub fn process_markdown(
    content: &str,
    options: &MarkdownOptions,
) -> Result<String, MarkdownError> {
    let mut buf: Vec<u8> = Vec::new();
    process_markdown_to_writer(content, &mut buf, options)?;
    // comrak and ammonia both emit valid UTF-8, so this should never
    // fail in practice — but surface the error rather than panic.
    String::from_utf8(buf).map_err(|e| {
        MarkdownError::RenderError(format!(
            "non-UTF-8 output from pipeline: {e}"
        ))
    })
}

/// Streams processed HTML directly to a `Write` sink.
///
/// Semantically equivalent to [`process_markdown`], but avoids one
/// intermediate allocation when callers already have a `Write`
/// destination (a file, a buffered network writer, a template engine).
/// The comrak render stage still produces a `String` internally — the
/// 1 MiB default input cap means end-to-end streaming would add API
/// surface without meaningful memory savings.
///
/// # Errors
///
/// Returns [`MarkdownError::IoError`] if the writer fails. All other
/// error conditions mirror [`process_markdown`].
pub fn process_markdown_to_writer<W: Write>(
    content: &str,
    writer: &mut W,
    options: &MarkdownOptions,
) -> Result<(), MarkdownError> {
    pipeline(content, writer, options, None)
}

/// Processes Markdown and returns both the rendered HTML and a
/// document-order table of contents.
///
/// Each [`Heading`] carries the level, the plain-text content, and
/// the anchor id that comrak emits for that heading. To make those
/// ids actually appear in the rendered HTML, set
/// [`MarkdownOptions::header_ids`] (the same prefix is reflected in
/// `Heading::id`).
///
/// # Errors
///
/// Mirrors [`process_markdown`].
pub fn process_markdown_with_toc(
    content: &str,
    options: &MarkdownOptions,
) -> Result<(String, Vec<Heading>), MarkdownError> {
    let mut buf: Vec<u8> = Vec::new();
    let mut toc = Vec::new();
    pipeline(content, &mut buf, options, Some(&mut toc))?;
    let html = String::from_utf8(buf).map_err(|e| {
        MarkdownError::RenderError(format!(
            "non-UTF-8 output from pipeline: {e}"
        ))
    })?;
    Ok((html, toc))
}

/// Streams processed HTML to `writer` and returns the table of
/// contents collected during the AST walk.
///
/// Same shape as [`process_markdown_to_writer`] but with the toc
/// metadata returned alongside the IO result.
pub fn process_markdown_with_toc_to_writer<W: Write>(
    content: &str,
    writer: &mut W,
    options: &MarkdownOptions,
) -> Result<Vec<Heading>, MarkdownError> {
    let mut toc = Vec::new();
    pipeline(content, writer, options, Some(&mut toc))?;
    Ok(toc)
}

/// Extracts plain-text content from Markdown, stripping all
/// formatting and markup.
///
/// This is useful for building search indexes, generating
/// plain-text excerpts, or calculating reading time.
///
/// # Errors
///
/// Returns a [`MarkdownError`] if input exceeds the size limit.
pub fn process_markdown_to_plain_text(
    content: &str,
    options: &MarkdownOptions,
) -> Result<String, MarkdownError> {
    if options.max_input_size > 0
        && content.len() > options.max_input_size
    {
        return Err(MarkdownError::InputTooLarge {
            size: content.len(),
            limit: options.max_input_size,
        });
    }

    let arena = Arena::new();
    let root = comrak::parse_document(
        &arena,
        content,
        &options.comrak_options,
    );

    Ok(crate::extensions::collect_all_text(root))
}

/// Internal pipeline shared by every public entry point. When
/// `toc_out` is `Some`, headings are collected during the AST pass
/// using [`collect_headings`].
fn pipeline<W: Write>(
    content: &str,
    writer: &mut W,
    options: &MarkdownOptions,
    toc_out: Option<&mut Vec<Heading>>,
) -> Result<(), MarkdownError> {
    info!("Starting markdown processing");
    debug!("Markdown options: {:?}", options);

    // ── 0. Resource limits ──────────────────────────────────────
    if options.max_input_size > 0
        && content.len() > options.max_input_size
    {
        return Err(MarkdownError::InputTooLarge {
            size: content.len(),
            limit: options.max_input_size,
        });
    }

    // ── 1. Validate options ─────────────────────────────────────
    if let Err(errors) = options.validate() {
        for (field, err) in &errors {
            warn!("Invalid MarkdownOptions.{field}: {err}");
        }
        return Err(MarkdownError::from(errors));
    }

    // ── 2. Build comrak options ─────────────────────────────────
    let mut comrak_opts = options.comrak_options.clone();
    // Always enable unsafe for internal rendering — we sanitize
    // at the end if the caller wants safety.
    comrak_opts.render.r#unsafe = true;

    // Wire header_ids into comrak's extension
    if let Some(ref prefix) = options.header_ids {
        comrak_opts.extension.header_id_prefix = Some(prefix.clone());
    }

    // ── 3. Parse → AST ─────────────────────────────────────────
    let arena = Arena::new();
    let root = comrak::parse_document(&arena, content, &comrak_opts);

    // ── 4. AST transforms ───────────────────────────────────────
    if options.enable_custom_blocks {
        debug!("Processing custom blocks at AST level");
        process_custom_block_nodes(root, &options.custom_block_config);
    }
    if options.enable_diagrams {
        debug!("Rewriting diagram code blocks");
        crate::diagrams::process_diagram_code_blocks(root);
    }
    if let Some(toc) = toc_out {
        // Collect after custom-block transforms (which may add
        // structural divs) but before table enhancement (which
        // detaches table nodes — irrelevant to headings, but keeps
        // the heading walk on a stable subtree).
        debug!("Collecting headings for table of contents");
        *toc = collect_headings(root, options.header_ids.as_deref());
    }
    if options.enable_enhanced_tables {
        debug!("Enhancing tables at AST level");
        enhance_table_nodes(root, &arena, &comrak_opts);
    }

    // ── 5. Render to HTML ───────────────────────────────────────
    debug!("Rendering AST to HTML");

    #[cfg(feature = "syntax_highlighting")]
    let adapter;
    #[cfg(feature = "syntax_highlighting")]
    let plugins = if options.enable_syntax_highlighting {
        adapter = SyntectAdapter::new(options.syntax_theme.as_deref());
        Plugins {
            render: RenderPlugins {
                codefence_syntax_highlighter: Some(&adapter),
                ..Default::default()
            },
        }
    } else {
        Plugins::default()
    };
    #[cfg(not(feature = "syntax_highlighting"))]
    let plugins = Plugins::default();

    let mut html = String::new();
    comrak::format_html_with_plugins(
        root,
        &comrak_opts,
        &mut html,
        &plugins,
    )
    .map_err(|e| MarkdownError::RenderError(e.to_string()))?;

    // ── 6. Sanitize and emit ────────────────────────────────────
    if options.allow_unsafe_html {
        writer.write_all(html.as_bytes())?;
    } else {
        debug!("Sanitizing HTML output");
        sanitize_html_to_writer(
            &html,
            writer,
            options.sanitizer_config.as_ref(),
        )?;
    }

    info!("Markdown processing completed successfully");
    Ok(())
}

// ── HTML sanitization ───────────────────────────────────────────────

/// Pre-generated `language-*` class names for code elements,
/// allocated once and reused across all sanitize calls.
static CODE_LANG_CLASSES: LazyLock<HashSet<String>> =
    LazyLock::new(|| {
        [
            "rust",
            "python",
            "javascript",
            "typescript",
            "java",
            "c",
            "cpp",
            "csharp",
            "go",
            "ruby",
            "swift",
            "kotlin",
            "php",
            "html",
            "css",
            "sql",
            "bash",
            "shell",
            "json",
            "yaml",
            "toml",
            "xml",
            "markdown",
            "plaintext",
            "text",
        ]
        .iter()
        .map(|lang| format!("language-{lang}"))
        .collect()
    });

/// Applies the default sanitizer allow-list to a `Builder<'a>`.
///
/// Kept separate so the cached default builder and any per-call
/// builder (used when the caller supplies a [`SanitizerConfig`])
/// share one source of truth for the base policy. All strings
/// threaded through here are `'static`, which coerces into any `'a`.
fn configure_default_sanitizer<'a>(builder: &mut ammonia::Builder<'a>) {
    let code_class_refs: HashSet<&'static str> =
        CODE_LANG_CLASSES.iter().map(|s| s.as_str()).collect();

    let mut allowed_classes: HashMap<
        &'static str,
        HashSet<&'static str>,
    > = HashMap::new();

    allowed_classes.insert(
        "div",
        [
            "alert",
            "alert-info",
            "alert-warning",
            "alert-success",
            "alert-primary",
            "alert-danger",
            "alert-secondary",
            "table-responsive",
        ]
        .into_iter()
        .collect(),
    );
    allowed_classes.insert("table", ["table"].into_iter().collect());
    allowed_classes.insert(
        "td",
        ["text-left", "text-center", "text-right"]
            .into_iter()
            .collect(),
    );
    allowed_classes.insert("code", code_class_refs);
    // Mermaid's JS library looks for <pre class="mermaid">.
    allowed_classes.insert("pre", ["mermaid"].into_iter().collect());

    builder
        .add_tags(["div", "pre", "code", "span", "input"])
        .add_tag_attributes("div", &["role", "id"])
        .add_tag_attributes("td", &["align"])
        .add_tag_attributes("th", &["align"])
        .add_tag_attributes("input", &["type", "checked", "disabled"])
        .add_tag_attributes("h1", &["id"])
        .add_tag_attributes("h2", &["id"])
        .add_tag_attributes("h3", &["id"])
        .add_tag_attributes("h4", &["id"])
        .add_tag_attributes("h5", &["id"])
        .add_tag_attributes("h6", &["id"])
        .add_tag_attributes("a", &["id"])
        .allowed_classes(allowed_classes)
        // Syntect's class-based highlighter emits open-ended class
        // names on <span> (one per grammar scope). Whitelisting them
        // individually is impractical, so we allow `class` on <span>
        // with unrestricted values — class attributes are CSS hooks,
        // they cannot execute script.
        .add_tag_attributes("span", &["class", "data-math-style"]);
}

/// Pre-configured ammonia sanitizer, built once and reused across
/// every default-config call to the sanitizer.
///
/// Why: `ammonia::Builder` is relatively expensive to construct — it
/// allocates several tag/attribute hash sets and the allowed-classes
/// map. Since the default configuration is static (all `'static`
/// strs), we build a single `Builder<'static>` behind a `LazyLock`
/// and call `clean(&self, …)` on it repeatedly.
static SANITIZE_BUILDER: LazyLock<ammonia::Builder<'static>> =
    LazyLock::new(|| {
        let mut builder = ammonia::Builder::default();
        configure_default_sanitizer(&mut builder);
        builder
    });

/// Writes sanitized HTML to the given writer.
///
/// Uses the cached default sanitizer when `cfg` is `None` (hot path).
/// When `cfg` is `Some`, builds a fresh `Builder` that merges the
/// defaults with the caller's extras — per-call cost, but scoped to
/// the uncommon case.
fn sanitize_html_to_writer<W: Write>(
    html: &str,
    writer: &mut W,
    cfg: Option<&SanitizerConfig>,
) -> std::io::Result<()> {
    match cfg {
        None => SANITIZE_BUILDER.clean(html).write_to(writer),
        Some(custom) => {
            build_custom_sanitizer(custom).clean(html).write_to(writer)
        }
    }
}

/// Builds a one-shot sanitizer that layers `cfg`'s extras over the
/// default allow-list. Lifetime is tied to `cfg` since the extras
/// are `String`-owned on the caller side.
fn build_custom_sanitizer(
    cfg: &SanitizerConfig,
) -> ammonia::Builder<'_> {
    let mut builder = ammonia::Builder::default();
    configure_default_sanitizer(&mut builder);

    // ammonia forbids a tag from appearing in both `tag_attributes`
    // (with "class") and `allowed_classes`. The default config
    // grants <span> open `class` via tag_attributes so syntect's
    // class-based highlighter survives sanitization. If the caller
    // is now restricting classes for any of those tags via
    // SanitizerConfig, swap them out of permissive mode before
    // adding the whitelist.
    for tag in cfg.extra_allowed_classes.keys() {
        builder.rm_tag_attributes(tag.as_str(), &["class"]);
    }

    if !cfg.extra_tags.is_empty() {
        builder.add_tags(cfg.extra_tags.iter().map(String::as_str));
    }
    for (tag, attrs) in &cfg.extra_tag_attributes {
        builder.add_tag_attributes(
            tag.as_str(),
            attrs.iter().map(String::as_str),
        );
    }
    if !cfg.extra_generic_attributes.is_empty() {
        builder.add_generic_attributes(
            cfg.extra_generic_attributes.iter().map(String::as_str),
        );
    }
    for (tag, classes) in &cfg.extra_allowed_classes {
        builder.add_allowed_classes(
            tag.as_str(),
            classes.iter().map(String::as_str),
        );
    }
    builder
}

// ── Tests ───────────────────────────────────────────────────────────

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

    #[test]
    fn test_process_markdown_with_all_features() {
        let markdown = r#"
# Test Markdown

| Left | Center | Right |
|:-----|:------:|------:|
| 1    |   2    |     3 |

```rust
fn main() {
    println!("Hello, world!");
}
```

<div class="note">This is a note.</div>
<div class="warning">This is a warning.</div>
<div class="tip">This is a tip.</div>
"#;

        let options = default_markdown_options();
        let result = process_markdown(markdown, &options);
        assert!(result.is_ok(), "Failed: {:?}", result.err());

        let html = result.unwrap();
        assert!(html.contains("table-responsive"));
        assert!(html.contains("language-rust"));
        assert!(html.contains("alert alert-info"));
        assert!(html.contains("alert alert-warning"));
        assert!(html.contains("alert alert-success"));
    }

    #[test]
    fn test_process_markdown_without_custom_blocks() {
        let markdown = "# Test\n<div class=\"note\">Note.</div>";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_comrak_options({
                let mut opts = Options::default();
                opts.extension.table = true;
                opts
            })
            .with_unsafe_html(true);

        let html = process_markdown(markdown, &options).unwrap();
        // The div should remain as-is (not converted to alert)
        assert!(html.contains("<div class=\"note\">"));
        assert!(!html.contains("alert"));
    }

    #[test]
    fn test_process_markdown_without_enhanced_tables() {
        let markdown = "| H1 | H2 |\n|---|---|\n| A | B |";
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_blocks(false)
            .with_comrak_options({
                let mut opts = Options::default();
                opts.extension.table = true;
                opts
            });

        let html = process_markdown(markdown, &options).unwrap();
        assert!(!html.contains("table-responsive"));
        assert!(html.contains("<table>"));
    }

    #[test]
    fn test_validation_enhanced_tables_without_extension() {
        let options = MarkdownOptions::new()
            .with_enhanced_tables(true)
            .with_custom_blocks(false)
            .with_comrak_options({
                let mut opts = Options::default();
                opts.extension.table = false;
                opts
            });
        let errors = options.validate().unwrap_err();
        assert!(errors
            .iter()
            .any(|(f, _)| f == "enable_enhanced_tables"));
    }

    #[test]
    fn test_validation_default_options_pass() {
        // Defaults should pass every check in validate() — the
        // suite is "tight" but not hostile to normal config.
        // Note: defaults have enable_enhanced_tables = true but
        // Options::default() has extension.table = false, so this
        // catches check #1 by design. Enable the extension to see
        // the all-green path.
        let mut comrak = Options::default();
        comrak.extension.table = true;
        let options =
            MarkdownOptions::new().with_comrak_options(comrak);
        assert!(
            options.validate().is_ok(),
            "{:?}",
            options.validate().unwrap_err()
        );
    }

    #[test]
    fn test_validation_unknown_syntax_theme() {
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_blocks(false)
            .with_custom_theme("no-such-theme-exists".into());
        let errors = options.validate().unwrap_err();
        assert!(
            errors.iter().any(|(f, _)| f == "syntax_theme"),
            "expected syntax_theme failure, got {errors:?}"
        );
    }

    #[test]
    fn test_validation_theme_without_highlighter_disabled() {
        // syntax_theme set + syntax_highlighting = false is a
        // silent no-op — rejected.
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_blocks(false)
            .with_syntax_highlighting(false)
            .with_custom_theme("base16-ocean.dark".into());
        let errors = options.validate().unwrap_err();
        assert!(errors.iter().any(|(f, _)| f == "syntax_theme"));
    }

    #[test]
    fn test_validation_sanitizer_with_unsafe_html() {
        // sanitizer_config set + allow_unsafe_html = true skips
        // sanitization entirely — reject the silent no-op.
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_blocks(false)
            .with_unsafe_html(true)
            .with_sanitizer_config(
                SanitizerConfig::new().with_tag("main"),
            );
        let errors = options.validate().unwrap_err();
        assert!(errors.iter().any(|(f, _)| f == "sanitizer_config"));
    }

    #[test]
    fn test_validation_header_ids_bad_chars() {
        for bad in [
            "user content ", // whitespace
            "quo\"te-",
            "ang<le-",
            "amp&-",
        ] {
            let options = MarkdownOptions::new()
                .with_enhanced_tables(false)
                .with_custom_blocks(false)
                .with_header_ids(bad);
            let errors = options.validate().unwrap_err();
            assert!(
                errors.iter().any(|(f, _)| f == "header_ids"),
                "expected header_ids failure for {bad:?}, got {errors:?}"
            );
        }
    }

    #[test]
    fn test_validation_header_ids_clean_prefix_ok() {
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_blocks(false)
            .with_header_ids("user-content-");
        assert!(options.validate().is_ok());
    }

    #[test]
    fn test_validation_sanitizer_extra_tag_invalid() {
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_blocks(false)
            .with_unsafe_html(false)
            .with_sanitizer_config(
                SanitizerConfig::new().with_tag("has space"),
            );
        let errors = options.validate().unwrap_err();
        assert!(
            errors
                .iter()
                .any(|(f, _)| f
                    .starts_with("sanitizer_config.extra_tags"))
        );
    }

    #[test]
    fn test_validation_sanitizer_extra_generic_attribute_invalid() {
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_blocks(false)
            .with_unsafe_html(false)
            .with_sanitizer_config(
                SanitizerConfig::new().with_generic_attribute(""),
            );
        let errors = options.validate().unwrap_err();
        assert!(errors.iter().any(|(f, _)| f
            .starts_with("sanitizer_config.extra_generic_attributes")));
    }

    #[test]
    fn test_validation_sanitizer_allowed_class_with_whitespace() {
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_blocks(false)
            .with_unsafe_html(false)
            .with_sanitizer_config(
                SanitizerConfig::new()
                    .with_allowed_class("span", "has space"),
            );
        let errors = options.validate().unwrap_err();
        assert!(errors.iter().any(|(f, _)| f
            .starts_with("sanitizer_config.extra_allowed_classes")));
    }

    #[test]
    fn test_validation_custom_block_class_override_empty() {
        let cfg = CustomBlockConfig::new()
            .with_class(CustomBlockType::Note, "");
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_block_config(cfg);
        let errors = options.validate().unwrap_err();
        assert!(errors.iter().any(|(f, _)| f
            .starts_with("custom_block_config.class_overrides")));
    }

    #[test]
    fn test_validation_custom_block_title_override_blank() {
        let cfg = CustomBlockConfig::new()
            .with_title(CustomBlockType::Warning, "   ");
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_block_config(cfg);
        let errors = options.validate().unwrap_err();
        assert!(errors.iter().any(|(f, _)| f
            .starts_with("custom_block_config.title_overrides")));
    }

    #[test]
    fn test_sanitizer_config_applies_extra_tag_attribute() {
        // Drives build_custom_sanitizer past validation with a
        // tag-specific attribute add — exercises the
        // add_tag_attributes branch in the sanitiser factory.
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_unsafe_html(false)
            .with_sanitizer_config(
                SanitizerConfig::new()
                    .with_tag("section")
                    .with_tag_attribute("section", "data-foo"),
            );

        let md = r#"<section data-foo="bar">hello</section>"#;
        let html = process_markdown(md, &options).unwrap();
        assert!(html.contains("<section"));
        assert!(html.contains("data-foo=\"bar\""));
    }

    #[test]
    fn test_sanitizer_config_applies_extra_generic_attribute() {
        // Drives build_custom_sanitizer past validation with a
        // generic-attr add — exercises the add_generic_attributes
        // branch.
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_unsafe_html(false)
            .with_sanitizer_config(
                SanitizerConfig::new().with_generic_attribute("data-x"),
            );

        let md = r#"<p data-x="v">hi</p>"#;
        let html = process_markdown(md, &options).unwrap();
        assert!(html.contains("data-x=\"v\""));
    }

    #[test]
    fn test_sanitizer_config_with_tag_attribute_direct() {
        // The builder method had zero direct coverage.
        let cfg = SanitizerConfig::new()
            .with_tag_attribute("div", "role")
            .with_tag_attribute("div", "id");
        let attrs = cfg
            .extra_tag_attributes
            .get("div")
            .expect("div should exist");
        assert_eq!(attrs, &vec!["role".to_string(), "id".to_string()]);
    }

    #[test]
    fn test_validation_sanitizer_tag_attr_invalid_tag() {
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_blocks(false)
            .with_unsafe_html(false)
            .with_sanitizer_config(
                SanitizerConfig::new()
                    .with_tag_attribute("has space", "id"),
            );
        let errors = options.validate().unwrap_err();
        assert!(errors.iter().any(|(f, _)| f
            .starts_with("sanitizer_config.extra_tag_attributes")));
    }

    #[test]
    fn test_validation_sanitizer_tag_attr_invalid_attr_name() {
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_blocks(false)
            .with_unsafe_html(false)
            .with_sanitizer_config(
                SanitizerConfig::new().with_tag_attribute("div", ""),
            );
        let errors = options.validate().unwrap_err();
        assert!(errors.iter().any(|(f, _)| f
            .starts_with("sanitizer_config.extra_tag_attributes")));
    }

    #[test]
    fn test_validation_sanitizer_allowed_class_invalid_tag() {
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_blocks(false)
            .with_unsafe_html(false)
            .with_sanitizer_config(
                SanitizerConfig::new()
                    .with_allowed_class("bad tag", "foo"),
            );
        let errors = options.validate().unwrap_err();
        assert!(errors.iter().any(|(f, _)| f
            .starts_with("sanitizer_config.extra_allowed_classes")));
    }

    #[test]
    fn test_validation_sanitizer_allowed_class_empty() {
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_blocks(false)
            .with_unsafe_html(false)
            .with_sanitizer_config(
                SanitizerConfig::new().with_allowed_class("span", ""),
            );
        let errors = options.validate().unwrap_err();
        assert!(errors.iter().any(|(f, _)| f
            .starts_with("sanitizer_config.extra_allowed_classes")));
    }

    #[test]
    fn test_validation_custom_block_class_override_whitespace() {
        let cfg = CustomBlockConfig::new()
            .with_class(CustomBlockType::Note, "bad class");
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_block_config(cfg);
        let errors = options.validate().unwrap_err();
        assert!(errors.iter().any(|(f, _)| f
            .starts_with("custom_block_config.class_overrides")));
    }

    #[test]
    fn test_toc_extracts_image_title() {
        // Exercises the NodeValue::Image branch of `extract_text`.
        let md = "# See ![alt](logo.png \"Logo Title\") here\n";
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_blocks(false);
        let (_html, toc) =
            process_markdown_with_toc(md, &options).unwrap();
        assert_eq!(toc.len(), 1);
        // Image title text should make it into the heading's plain text.
        assert!(
            toc[0].text.contains("Logo Title")
                || toc[0].text.contains("alt"),
            "expected image title/alt in: {:?}",
            toc[0].text
        );
    }

    #[test]
    fn test_plain_text_soft_break_inserts_space() {
        // Two text nodes joined by a soft break should get a space
        // between them (covers SoftBreak/LineBreak arm in
        // collect_all_text).
        let md = "one\ntwo\nthree\n";
        let text = process_markdown_to_plain_text(
            md,
            &MarkdownOptions::default(),
        )
        .unwrap();
        assert_eq!(text, "one two three");
    }

    #[test]
    fn test_plain_text_image_title_included() {
        // Image titles end up in plain-text output too.
        let md = "Caption: ![alt](x.png \"Title Here\")\n";
        let text = process_markdown_to_plain_text(
            md,
            &MarkdownOptions::default(),
        )
        .unwrap();
        // Plain text should at minimum keep the surrounding caption.
        assert!(text.contains("Caption:"));
    }

    #[test]
    fn test_validation_reports_all_failures_at_once() {
        // Three independent violations in one options instance.
        // The validator must collect all of them, not bail on the
        // first.
        let cfg = CustomBlockConfig::new()
            .with_class(CustomBlockType::Note, "");
        let options = MarkdownOptions::new()
            .with_enhanced_tables(true) // ← 1: needs comrak.extension.table
            .with_header_ids("a b") // ← 2: whitespace
            .with_custom_block_config(cfg) // ← 3: empty override
            .with_comrak_options({
                let mut opts = Options::default();
                opts.extension.table = false;
                opts
            });
        let errors = options.validate().unwrap_err();
        assert!(
            errors.len() >= 3,
            "expected 3+ errors, got {errors:?}"
        );
    }

    #[test]
    fn test_empty_content() {
        let options = MarkdownOptions::new()
            .with_enhanced_tables(false)
            .with_custom_blocks(false);
        let html = process_markdown("", &options).unwrap();
        assert!(html.trim().is_empty());
    }

    #[test]
    fn test_no_features_enabled() {
        let markdown = "# Title\n\nPlain text.";
        let options = MarkdownOptions::new()
            .with_syntax_highlighting(false)
            .with_custom_blocks(false)
            .with_enhanced_tables(false);

        let html = process_markdown(markdown, &options).unwrap();
        assert!(html.contains("<h1>Title</h1>"));
        assert!(html.contains("Plain text."));
    }

    #[test]
    fn test_sanitization_strips_script() {
        let markdown = "<script>alert('xss')</script>";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_unsafe_html(false);

        let html = process_markdown(markdown, &options).unwrap();
        assert!(
            !html.contains("<script>"),
            "Script tags should be stripped"
        );
    }

    #[test]
    fn test_sanitization_preserves_alerts() {
        let markdown = "<div class=\"note\">Important info.</div>";
        let options = MarkdownOptions::new()
            .with_custom_blocks(true)
            .with_enhanced_tables(false)
            .with_unsafe_html(false);

        let html = process_markdown(markdown, &options).unwrap();
        assert!(
            html.contains("alert alert-info"),
            "Alert divs should survive sanitization"
        );
    }

    #[test]
    fn test_input_too_large() {
        let options = MarkdownOptions::new()
            .with_max_input_size(10)
            .with_custom_blocks(false)
            .with_enhanced_tables(false);

        let result =
            process_markdown("a]".repeat(20).as_str(), &options);
        assert!(matches!(
            result,
            Err(MarkdownError::InputTooLarge { .. })
        ));
    }

    #[test]
    fn test_syntax_theme_customization() {
        let markdown = "```rust\nfn main() {}\n```";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_custom_theme("InspiredGitHub".to_string());

        let result = process_markdown(markdown, &options);
        assert!(result.is_ok());
    }

    #[test]
    fn test_custom_block_config() {
        let markdown = "<div class=\"note\">Custom styled.</div>";
        let config = CustomBlockConfig::new()
            .with_class(
                crate::extensions::CustomBlockType::Note,
                "my-note",
            )
            .with_title(
                crate::extensions::CustomBlockType::Note,
                "Heads up",
            );

        let options = MarkdownOptions::new()
            .with_custom_blocks(true)
            .with_enhanced_tables(false)
            .with_custom_block_config(config)
            .with_unsafe_html(true);

        let html = process_markdown(markdown, &options).unwrap();
        assert!(html.contains("my-note"));
        assert!(html.contains("Heads up:"));
    }

    #[test]
    fn test_builder_order_comrak_then_unsafe() {
        let options = MarkdownOptions::new()
            .with_comrak_options(Options::default())
            .with_unsafe_html(true);
        assert!(options.allow_unsafe_html);
    }

    #[test]
    fn test_comrak_options_syncs_unsafe() {
        let mut opts = Options::default();
        opts.render.r#unsafe = true;
        let options = MarkdownOptions::new().with_comrak_options(opts);
        assert!(options.allow_unsafe_html);
    }

    #[test]
    fn test_markdown_options_debug_impl() {
        let options = MarkdownOptions::new()
            .with_custom_blocks(true)
            .with_syntax_highlighting(false)
            .with_enhanced_tables(true)
            .with_custom_theme("InspiredGitHub".to_string())
            .with_unsafe_html(false)
            .with_max_input_size(2048);

        let debug_output = format!("{:?}", options);
        assert!(debug_output.contains("MarkdownOptions"));
        assert!(debug_output.contains("enable_custom_blocks: true"));
        assert!(
            debug_output.contains("enable_syntax_highlighting: false")
        );
        assert!(debug_output.contains("enable_enhanced_tables: true"));
        assert!(debug_output.contains("InspiredGitHub"));
        assert!(debug_output.contains("allow_unsafe_html: false"));
        assert!(debug_output.contains("max_input_size: 2048"));
    }

    #[test]
    fn test_header_ids() {
        let markdown = "# Hello World\n## Sub Section";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_header_ids("")
            .with_unsafe_html(true);

        let html = process_markdown(markdown, &options).unwrap();
        assert!(
            html.contains("id=\"hello-world\""),
            "H1 should have id attribute: {html}"
        );
        assert!(
            html.contains("id=\"sub-section\""),
            "H2 should have id attribute: {html}"
        );
    }

    #[test]
    fn test_header_ids_with_prefix() {
        let markdown = "# Title";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_header_ids("user-content-")
            .with_unsafe_html(true);

        let html = process_markdown(markdown, &options).unwrap();
        assert!(
            html.contains("id=\"user-content-title\""),
            "Should have prefixed id: {html}"
        );
    }

    #[test]
    fn test_header_ids_survive_sanitization() {
        let markdown = "# Hello World";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_header_ids("")
            .with_unsafe_html(false);

        let html = process_markdown(markdown, &options).unwrap();
        assert!(
            html.contains("id=\"hello-world\""),
            "Header id should survive ammonia sanitization: {html}"
        );
    }

    #[test]
    fn test_ast_table_enhancement() {
        let markdown =
            "| H1 | H2 |\n|:---|---:|\n| L | R |\n\nParagraph\n\n| A | B |\n|---|---|\n| C | D |";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_comrak_options({
                let mut opts = Options::default();
                opts.extension.table = true;
                opts
            })
            .with_unsafe_html(true);

        let html = process_markdown(markdown, &options).unwrap();
        // Both tables should be wrapped
        assert_eq!(
            html.matches("table-responsive").count(),
            2,
            "Both tables should get responsive wrapper: {html}"
        );
        assert!(
            html.contains("text-right"),
            "Right-aligned cells should have class"
        );
    }

    // ── Streaming API ───────────────────────────────────────────

    #[test]
    fn test_process_markdown_to_writer_matches_string_variant() {
        let markdown = "# Title\n\nParagraph with *emphasis*.";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_syntax_highlighting(false);

        let as_string = process_markdown(markdown, &options).unwrap();

        let mut buf: Vec<u8> = Vec::new();
        process_markdown_to_writer(markdown, &mut buf, &options)
            .unwrap();
        let as_bytes = String::from_utf8(buf).unwrap();

        assert_eq!(
            as_string, as_bytes,
            "writer variant must produce byte-identical output"
        );
    }

    #[test]
    fn test_process_markdown_to_writer_sanitizes() {
        let markdown = "<script>alert('xss')</script>\n\n# Safe";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_unsafe_html(false);

        let mut buf: Vec<u8> = Vec::new();
        process_markdown_to_writer(markdown, &mut buf, &options)
            .unwrap();
        let html = String::from_utf8(buf).unwrap();
        assert!(!html.contains("<script>"));
        assert!(html.contains("<h1>Safe</h1>"));
    }

    #[test]
    fn test_process_markdown_to_writer_propagates_io_error() {
        struct AlwaysFails;
        impl Write for AlwaysFails {
            fn write(&mut self, _: &[u8]) -> std::io::Result<usize> {
                Err(std::io::Error::new(
                    std::io::ErrorKind::BrokenPipe,
                    "nope",
                ))
            }
            fn flush(&mut self) -> std::io::Result<()> {
                Ok(())
            }
        }

        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false);
        let err = process_markdown_to_writer(
            "# hi",
            &mut AlwaysFails,
            &options,
        )
        .unwrap_err();
        assert!(matches!(err, MarkdownError::IoError(_)));
    }

    // ── SanitizerConfig ─────────────────────────────────────────

    #[test]
    fn test_sanitizer_config_allows_extra_tag() {
        // <main> is not in ammonia's default tag allow-list and is
        // not added by our defaults, so it's stripped to text unless
        // the SanitizerConfig extends the list.
        let markdown = "<main>wrapper</main>";

        let strict = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_unsafe_html(false);
        let stripped = process_markdown(markdown, &strict).unwrap();
        assert!(
            !stripped.contains("<main>"),
            "default sanitizer drops <main>: {stripped}"
        );

        let extended = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_unsafe_html(false)
            .with_sanitizer_config(
                SanitizerConfig::new().with_tag("main"),
            );
        let kept = process_markdown(markdown, &extended).unwrap();
        assert!(
            kept.contains("<main>wrapper</main>"),
            "extended sanitizer keeps <main>: {kept}"
        );
    }

    #[test]
    fn test_sanitizer_config_adds_allowed_class() {
        let markdown =
            "<span class=\"badge\">new</span> <span class=\"danger\">x</span>";

        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_unsafe_html(false)
            .with_sanitizer_config(
                SanitizerConfig::new()
                    .with_allowed_class("span", "badge"),
            );

        let html = process_markdown(markdown, &options).unwrap();
        assert!(
            html.contains("class=\"badge\""),
            "whitelisted class survives: {html}"
        );
        assert!(
            !html.contains("class=\"danger\""),
            "non-whitelisted class dropped: {html}"
        );
    }

    #[cfg(feature = "syntax_highlighting")]
    #[test]
    fn test_sanitized_output_keeps_syntect_span_classes() {
        // Sanitized pipeline must preserve the open-ended class
        // names that ClassedHTMLGenerator emits on <span> — without
        // them, code blocks render unstyled.
        let markdown = "```rust\nfn main() {}\n```";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_unsafe_html(false);

        let html = process_markdown(markdown, &options).unwrap();
        assert!(
            html.contains("<span class=\""),
            "syntect classes were stripped by sanitizer: {html}"
        );
    }

    #[test]
    fn test_sanitizer_config_restricts_span_class() {
        // Custom config with extra_allowed_classes for span must
        // override the default permissive span policy: only the
        // whitelisted class survives.
        let markdown = "<span class=\"badge\">a</span> <span class=\"danger\">b</span>";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_unsafe_html(false)
            .with_sanitizer_config(
                SanitizerConfig::new()
                    .with_allowed_class("span", "badge"),
            );

        let html = process_markdown(markdown, &options).unwrap();
        assert!(
            html.contains("class=\"badge\""),
            "whitelisted class survives: {html}"
        );
        assert!(
            !html.contains("class=\"danger\""),
            "non-whitelisted class dropped: {html}"
        );
    }

    #[test]
    fn test_sanitizer_strips_style_attribute() {
        // The default sanitizer no longer allows `style` on any tag.
        // Clickjacking via position:fixed/z-index would otherwise be
        // possible through raw HTML in user-supplied Markdown.
        let markdown =
            "<div style=\"position:fixed;top:0;left:0;width:100%;height:100%;z-index:9999;\">overlay</div>";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_unsafe_html(false);

        let html = process_markdown(markdown, &options).unwrap();
        assert!(
            !html.contains("style="),
            "style attribute must be stripped: {html}"
        );
        // The div itself is still allowed; only the attribute goes.
        assert!(html.contains("<div"), "div tag dropped: {html}");
    }

    // ── Table of contents ───────────────────────────────────────

    #[test]
    fn test_toc_collects_headings_in_document_order() {
        let markdown = "\
# First
Some text.

## Second-A
More text.

## Second-B

# Third\n";

        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false);

        let (_html, toc) =
            process_markdown_with_toc(markdown, &options).unwrap();
        let levels: Vec<u8> = toc.iter().map(|h| h.level).collect();
        let texts: Vec<&str> =
            toc.iter().map(|h| h.text.as_str()).collect();
        assert_eq!(levels, vec![1, 2, 2, 1]);
        assert_eq!(
            texts,
            vec!["First", "Second-A", "Second-B", "Third"]
        );
    }

    #[test]
    fn test_toc_ids_match_rendered_html() {
        // With header_ids enabled, the rendered HTML must contain
        // an id matching every Heading::id we report.
        let markdown = "# Hello World\n\n## A Second Heading\n";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_header_ids("");

        let (html, toc) =
            process_markdown_with_toc(markdown, &options).unwrap();
        assert_eq!(toc.len(), 2);
        for h in &toc {
            let needle = format!("id=\"{}\"", h.id);
            assert!(
                html.contains(&needle),
                "ToC id {:?} not found in HTML: {}",
                h.id,
                html
            );
        }
    }

    #[test]
    fn test_toc_prefix_propagates() {
        let markdown = "# Intro\n";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_header_ids("user-content-");

        let (html, toc) =
            process_markdown_with_toc(markdown, &options).unwrap();
        assert_eq!(toc.len(), 1);
        assert_eq!(toc[0].id, "user-content-intro");
        assert!(html.contains("id=\"user-content-intro\""));
    }

    #[test]
    fn test_toc_dedup_with_repeated_headings() {
        let markdown = "# Notes\n## Notes\n### Notes\n";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_header_ids("");

        let (_html, toc) =
            process_markdown_with_toc(markdown, &options).unwrap();
        let ids: Vec<&str> =
            toc.iter().map(|h| h.id.as_str()).collect();
        assert_eq!(ids, vec!["notes", "notes-1", "notes-2"]);
    }

    #[test]
    fn test_toc_empty_document() {
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false);
        let (html, toc) =
            process_markdown_with_toc("", &options).unwrap();
        assert!(toc.is_empty());
        assert!(html.trim().is_empty());
    }

    #[test]
    fn test_toc_writer_variant_writes_html_and_returns_toc() {
        let markdown = "# Title\n\n## Sub\n";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false);

        let mut buf: Vec<u8> = Vec::new();
        let toc = process_markdown_with_toc_to_writer(
            markdown, &mut buf, &options,
        )
        .unwrap();
        let html = String::from_utf8(buf).unwrap();
        assert!(html.contains("<h1>Title</h1>"));
        assert!(html.contains("<h2>Sub</h2>"));
        assert_eq!(toc.len(), 2);
    }

    #[test]
    fn test_toc_extracts_inline_code_text() {
        let markdown = "# Using `&str` types\n";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false);

        let (_html, toc) =
            process_markdown_with_toc(markdown, &options).unwrap();
        assert_eq!(toc.len(), 1);
        assert_eq!(toc[0].text, "Using &str types");
    }

    #[test]
    fn test_sanitizer_config_default_path_unchanged() {
        // Options with no sanitizer_config must go through the cached
        // default builder and produce the same output as before the
        // feature was added.
        let markdown = "<script>x</script>\n<div class=\"alert alert-info\">safe</div>";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_unsafe_html(false);

        let html = process_markdown(markdown, &options).unwrap();
        assert!(!html.contains("<script>"));
        assert!(html.contains("alert alert-info"));
    }

    // ── Diagrams (mermaid / geojson / topojson / stl) ───────────

    #[test]
    fn test_diagrams_off_by_default_leaves_code_block() {
        let md = "```mermaid\ngraph TD\nA-->B\n```\n";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false);
        let html = process_markdown(md, &options).unwrap();
        // Default syntax highlighting kicks in — the content is
        // rendered as a syntax-highlighted code block, NOT a mermaid
        // container.
        assert!(html.contains("<code class=\"language-mermaid\">"));
        assert!(!html.contains("class=\"mermaid\""));
    }

    #[test]
    fn test_diagrams_mermaid_survives_sanitizer() {
        let md = "```mermaid\ngraph TD\nA-->B\n```\n";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_syntax_highlighting(false)
            .with_diagrams(true)
            .with_unsafe_html(false);
        let html = process_markdown(md, &options).unwrap();
        assert!(
            html.contains("<pre class=\"mermaid\">"),
            "mermaid container stripped: {html}"
        );
        assert!(html.contains("graph TD"));
    }

    #[test]
    fn test_diagrams_non_matching_lang_still_highlighted() {
        // With diagrams on, a non-mermaid language still gets the
        // normal syntax-highlighter treatment.
        let md = "```python\nprint('hi')\n```\n";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_diagrams(true);
        let html = process_markdown(md, &options).unwrap();
        assert!(html.contains("<code class=\"language-python\">"));
        assert!(!html.contains("class=\"mermaid\""));
    }

    #[test]
    fn test_diagrams_formerly_supported_langs_highlight_as_usual() {
        // `geojson`, `topojson`, `stl` used to be recognised but
        // produced lifeless output even with rich demo data and
        // proper lighting, so the project narrowed scope to
        // mermaid. Those code blocks now flow through the standard
        // syntax-highlighter path (or render as plain code when
        // the syntax is unknown to syntect), never as a mermaid
        // container.
        let md = "```geojson\n{\"type\":\"Feature\"}\n```\n\n```stl\nsolid x\nendsolid x\n```\n";
        let options = MarkdownOptions::new()
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_diagrams(true);
        let html = process_markdown(md, &options).unwrap();
        assert!(!html.contains("class=\"mermaid\""));
        assert!(!html.contains("mdx-diagram"));
    }

    // ── Plain text extractor ────────────────────────────────────

    #[test]
    fn test_plain_text_basic() {
        let md = "# Hello World\n\nA **bold** paragraph.";
        let text = process_markdown_to_plain_text(
            md,
            &MarkdownOptions::default(),
        )
        .unwrap();
        assert_eq!(text, "Hello World A bold paragraph.");
    }

    #[test]
    fn test_plain_text_lists_and_code() {
        let md =
            "# Title\n\nDesc.\n\n- one\n- two\n\n```\nfn main() {}\n```";
        let text = process_markdown_to_plain_text(
            md,
            &MarkdownOptions::default(),
        )
        .unwrap();
        assert!(text.contains("Title"));
        assert!(text.contains("Desc."));
        assert!(text.contains("one"));
        assert!(text.contains("two"));
        assert!(text.contains("fn main() {}"));
        // No words merged into each other.
        assert!(!text.contains("Titleone"));
        assert!(!text.contains("onetwo"));
    }

    #[test]
    fn test_plain_text_strips_html() {
        // Raw HTML should NOT appear in the plain-text output.
        let md = "A <strong>bold</strong> word";
        let text = process_markdown_to_plain_text(
            md,
            &MarkdownOptions::default(),
        )
        .unwrap();
        assert!(!text.contains("<strong>"));
        assert!(!text.contains("</strong>"));
        assert!(text.contains("bold"));
    }

    #[test]
    fn test_plain_text_respects_input_cap() {
        let options = MarkdownOptions::new().with_max_input_size(8);
        let err =
            process_markdown_to_plain_text(&"a".repeat(64), &options)
                .unwrap_err();
        assert!(matches!(err, MarkdownError::InputTooLarge { .. }));
    }

    #[test]
    fn test_plain_text_includes_inline_code() {
        // Covers the `NodeValue::Code` arm of `collect_all_text` —
        // inline backtick code was the only text-emitting AST node
        // variant not touched by any other plain-text test.
        let md = "Use `println!` to print, then `drop`.";
        let text = process_markdown_to_plain_text(
            md,
            &MarkdownOptions::default(),
        )
        .unwrap();
        assert!(
            text.contains("println!"),
            "expected inline code literal, got: {text:?}"
        );
        assert!(
            text.contains("drop"),
            "expected second inline code literal, got: {text:?}"
        );
    }

    // ── Math + footnote sanitizer survival ──────────────────────

    #[test]
    fn test_math_dollars_survives_sanitizer() {
        // `extension.math_dollars` renders as
        // `<span data-math-style="inline">…</span>`. The default
        // sanitizer must preserve the attribute so a frontend
        // library (KaTeX, MathJax) can find and render it.
        let mut comrak = Options::default();
        comrak.extension.math_dollars = true;
        let options = MarkdownOptions::new()
            .with_comrak_options(comrak)
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_syntax_highlighting(false)
            .with_unsafe_html(false);

        let html =
            process_markdown("Inline $a^2 + b^2$ math.", &options)
                .unwrap();
        assert!(
            html.contains("data-math-style"),
            "data-math-style attribute stripped by sanitizer: {html}"
        );
    }

    #[test]
    fn test_footnote_link_survives_sanitizer() {
        // GFM footnotes emit `<sup><a href="#fn-1" id="fnref-1">`
        // + a back-reference link. The default sanitizer must
        // preserve both so footnote navigation works.
        let mut comrak = Options::default();
        comrak.extension.footnotes = true;
        let options = MarkdownOptions::new()
            .with_comrak_options(comrak)
            .with_custom_blocks(false)
            .with_enhanced_tables(false)
            .with_syntax_highlighting(false)
            .with_unsafe_html(false);

        let md = "Claim[^1].\n\n[^1]: Reason.\n";
        let html = process_markdown(md, &options).unwrap();
        assert!(html.contains("<sup"), "missing <sup>: {html}");
        assert!(
            html.contains("href=\"#fn-1\"")
                || html.contains("href=\"#fn1\""),
            "missing forward link: {html}"
        );
        assert!(
            html.contains("href=\"#fnref-1\"")
                || html.contains("href=\"#fnref1\""),
            "missing back-reference link: {html}"
        );
    }
}