rumdl 0.1.51

A fast Markdown linter written in Rust (Ru(st) MarkDown Linter)
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
//! Filtered line iteration for markdown linting
//!
//! This module provides a zero-cost abstraction for iterating over markdown lines
//! while automatically filtering out non-content regions like front matter, code blocks,
//! and HTML blocks. This ensures rules only process actual markdown content.
//!
//! # Architecture
//!
//! The filtered iterator approach centralizes the logic of what content should be
//! processed by rules, eliminating error-prone manual checks in each rule implementation.
//!
//! # Examples
//!
//! ```rust
//! use rumdl_lib::lint_context::LintContext;
//! use rumdl_lib::filtered_lines::FilteredLinesExt;
//!
//! let content = "---\nurl: http://example.com\n---\n\n# Title\n\nContent";
//! let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
//!
//! // Simple: get all content lines (skips front matter by default)
//! for line in ctx.content_lines() {
//!     println!("Line {}: {}", line.line_num, line.content);
//! }
//!
//! // Advanced: custom filter configuration
//! for line in ctx.filtered_lines()
//!     .skip_code_blocks()
//!     .skip_front_matter()
//!     .skip_html_blocks() {
//!     println!("Line {}: {}", line.line_num, line.content);
//! }
//! ```

use crate::lint_context::{LineInfo, LintContext};

/// A single line from a filtered iteration, with guaranteed 1-indexed line numbers
#[derive(Debug, Clone)]
pub struct FilteredLine<'a> {
    /// The 1-indexed line number in the original document
    pub line_num: usize,
    /// Reference to the line's metadata
    pub line_info: &'a LineInfo,
    /// The actual line content
    pub content: &'a str,
}

/// Configuration for filtering lines during iteration
///
/// Use the builder pattern to configure which types of content should be skipped:
///
/// ```rust
/// use rumdl_lib::filtered_lines::LineFilterConfig;
///
/// let config = LineFilterConfig::new()
///     .skip_front_matter()
///     .skip_code_blocks()
///     .skip_html_blocks()
///     .skip_html_comments()
///     .skip_mkdocstrings()
///     .skip_esm_blocks()
///     .skip_quarto_divs();
/// ```
#[derive(Debug, Clone, Default)]
pub struct LineFilterConfig {
    /// Skip lines inside front matter (YAML/TOML/JSON metadata)
    pub skip_front_matter: bool,
    /// Skip lines inside fenced code blocks
    pub skip_code_blocks: bool,
    /// Skip lines inside HTML blocks
    pub skip_html_blocks: bool,
    /// Skip lines inside HTML comments
    pub skip_html_comments: bool,
    /// Skip lines inside mkdocstrings blocks
    pub skip_mkdocstrings: bool,
    /// Skip lines inside ESM (ECMAScript Module) blocks
    pub skip_esm_blocks: bool,
    /// Skip lines inside math blocks ($$ ... $$)
    pub skip_math_blocks: bool,
    /// Skip lines inside Quarto div blocks (::: ... :::)
    pub skip_quarto_divs: bool,
    /// Skip lines containing or inside JSX expressions (MDX: {expression})
    pub skip_jsx_expressions: bool,
    /// Skip lines inside MDX comments ({/* ... */})
    pub skip_mdx_comments: bool,
    /// Skip lines inside MkDocs admonitions (!!! or ???)
    pub skip_admonitions: bool,
    /// Skip lines inside MkDocs content tabs (=== "Tab")
    pub skip_content_tabs: bool,
    /// Skip lines inside HTML blocks with markdown attribute (MkDocs grid cards, etc.)
    pub skip_mkdocs_html_markdown: bool,
    /// Skip lines inside definition lists (:  definition)
    pub skip_definition_lists: bool,
    /// Skip lines inside Obsidian comments (%%...%%)
    pub skip_obsidian_comments: bool,
    /// Skip lines inside PyMdown Blocks (/// ... ///, MkDocs flavor only)
    pub skip_pymdown_blocks: bool,
    /// Skip lines inside kramdown extension blocks ({::comment}...{:/comment}, etc.)
    pub skip_kramdown_extension_blocks: bool,
    /// Skip lines that are div markers (::: opening or closing)
    /// Unlike `skip_quarto_divs` which skips ALL content inside divs,
    /// this only skips the marker lines themselves (structural delimiters)
    pub skip_div_markers: bool,
    /// Skip lines inside JSX component blocks (MDX only, e.g. `<Tabs>...</Tabs>`)
    pub skip_jsx_blocks: bool,
}

impl LineFilterConfig {
    /// Create a new filter configuration with all filters disabled
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Skip lines that are part of front matter (YAML/TOML/JSON)
    ///
    /// Front matter is metadata at the start of a markdown file and should
    /// not be processed by markdown linting rules.
    #[must_use]
    pub fn skip_front_matter(mut self) -> Self {
        self.skip_front_matter = true;
        self
    }

    /// Skip lines inside fenced code blocks
    ///
    /// Code blocks contain source code, not markdown, and most rules should
    /// not process them.
    #[must_use]
    pub fn skip_code_blocks(mut self) -> Self {
        self.skip_code_blocks = true;
        self
    }

    /// Skip lines inside HTML blocks
    ///
    /// HTML blocks contain raw HTML and most markdown rules should not
    /// process them.
    #[must_use]
    pub fn skip_html_blocks(mut self) -> Self {
        self.skip_html_blocks = true;
        self
    }

    /// Skip lines inside HTML comments
    ///
    /// HTML comments (<!-- ... -->) are metadata and should not be processed
    /// by most markdown linting rules.
    #[must_use]
    pub fn skip_html_comments(mut self) -> Self {
        self.skip_html_comments = true;
        self
    }

    /// Skip lines inside mkdocstrings blocks
    ///
    /// Mkdocstrings blocks contain auto-generated documentation and most
    /// markdown rules should not process them.
    #[must_use]
    pub fn skip_mkdocstrings(mut self) -> Self {
        self.skip_mkdocstrings = true;
        self
    }

    /// Skip lines inside ESM (ECMAScript Module) blocks
    ///
    /// ESM blocks contain JavaScript/TypeScript module code and most
    /// markdown rules should not process them.
    #[must_use]
    pub fn skip_esm_blocks(mut self) -> Self {
        self.skip_esm_blocks = true;
        self
    }

    /// Skip lines inside math blocks ($$ ... $$)
    ///
    /// Math blocks contain LaTeX/mathematical notation and markdown rules
    /// should not process them as regular markdown content.
    #[must_use]
    pub fn skip_math_blocks(mut self) -> Self {
        self.skip_math_blocks = true;
        self
    }

    /// Skip lines inside Quarto div blocks (::: ... :::)
    ///
    /// Quarto divs are fenced containers for callouts, panels, and other
    /// structured content. Rules may need to skip them for accurate processing.
    #[must_use]
    pub fn skip_quarto_divs(mut self) -> Self {
        self.skip_quarto_divs = true;
        self
    }

    /// Skip lines containing or inside JSX expressions (MDX: {expression})
    ///
    /// JSX expressions contain JavaScript code and most markdown rules
    /// should not process them as regular markdown content.
    #[must_use]
    pub fn skip_jsx_expressions(mut self) -> Self {
        self.skip_jsx_expressions = true;
        self
    }

    /// Skip lines inside MDX comments ({/* ... */})
    ///
    /// MDX comments are metadata and should not be processed by most
    /// markdown linting rules.
    #[must_use]
    pub fn skip_mdx_comments(mut self) -> Self {
        self.skip_mdx_comments = true;
        self
    }

    /// Skip lines inside MkDocs admonitions (!!! or ???)
    ///
    /// Admonitions are callout blocks and may have special formatting
    /// that rules should not process as regular content.
    #[must_use]
    pub fn skip_admonitions(mut self) -> Self {
        self.skip_admonitions = true;
        self
    }

    /// Skip lines inside MkDocs content tabs (=== "Tab")
    ///
    /// Content tabs contain tabbed content that may need special handling.
    #[must_use]
    pub fn skip_content_tabs(mut self) -> Self {
        self.skip_content_tabs = true;
        self
    }

    /// Skip lines inside HTML blocks with markdown attribute (MkDocs grid cards, etc.)
    ///
    /// These blocks contain markdown-enabled HTML which may have custom styling rules.
    #[must_use]
    pub fn skip_mkdocs_html_markdown(mut self) -> Self {
        self.skip_mkdocs_html_markdown = true;
        self
    }

    /// Skip lines inside any MkDocs container (admonitions, content tabs, or markdown HTML divs)
    ///
    /// This is a convenience method that enables `skip_admonitions`,
    /// `skip_content_tabs`, and `skip_mkdocs_html_markdown`. MkDocs containers use
    /// 4-space indented content which may need special handling to preserve structure.
    #[must_use]
    pub fn skip_mkdocs_containers(mut self) -> Self {
        self.skip_admonitions = true;
        self.skip_content_tabs = true;
        self.skip_mkdocs_html_markdown = true;
        self
    }

    /// Skip lines inside definition lists (:  definition)
    ///
    /// Definition lists have special formatting that rules should
    /// not process as regular content.
    #[must_use]
    pub fn skip_definition_lists(mut self) -> Self {
        self.skip_definition_lists = true;
        self
    }

    /// Skip lines inside Obsidian comments (%%...%%)
    ///
    /// Obsidian comments are content hidden from rendering and most
    /// markdown rules should not process them.
    #[must_use]
    pub fn skip_obsidian_comments(mut self) -> Self {
        self.skip_obsidian_comments = true;
        self
    }

    /// Skip lines inside PyMdown Blocks (/// ... ///)
    ///
    /// PyMdown Blocks are structured content blocks used by the PyMdown Extensions
    /// library for captions, collapsible details, admonitions, and other features.
    /// Rules may need to skip them for accurate processing.
    #[must_use]
    pub fn skip_pymdown_blocks(mut self) -> Self {
        self.skip_pymdown_blocks = true;
        self
    }

    /// Skip lines inside kramdown extension blocks ({::comment}...{:/comment}, {::nomarkdown}...{:/nomarkdown})
    ///
    /// Kramdown extension blocks contain content that should not be processed
    /// as regular markdown (comments, raw HTML, options directives).
    #[must_use]
    pub fn skip_kramdown_extension_blocks(mut self) -> Self {
        self.skip_kramdown_extension_blocks = true;
        self
    }

    /// Skip lines that are div markers (::: opening or closing)
    ///
    /// Unlike `skip_quarto_divs` which skips ALL lines inside a div block,
    /// this only skips the `:::` marker lines themselves. Use this when you
    /// want to process content inside divs but treat markers as block boundaries.
    #[must_use]
    pub fn skip_div_markers(mut self) -> Self {
        self.skip_div_markers = true;
        self
    }

    /// Skip lines inside JSX component blocks (MDX only)
    ///
    /// JSX blocks like `<Tabs>...</Tabs>` contain content that pulldown-cmark
    /// may misparse. Use this to skip entire JSX component regions.
    #[must_use]
    pub fn skip_jsx_blocks(mut self) -> Self {
        self.skip_jsx_blocks = true;
        self
    }

    /// Check if a line should be filtered out based on this configuration
    fn should_filter(&self, line_info: &LineInfo) -> bool {
        // Kramdown extension blocks are always filtered unconditionally.
        // Their content should never be linted by any rule.
        line_info.in_kramdown_extension_block
            || (self.skip_front_matter && line_info.in_front_matter)
            || (self.skip_code_blocks && line_info.in_code_block)
            || (self.skip_html_blocks && line_info.in_html_block)
            || (self.skip_html_comments && line_info.in_html_comment)
            || (self.skip_mkdocstrings && line_info.in_mkdocstrings)
            || (self.skip_esm_blocks && line_info.in_esm_block)
            || (self.skip_math_blocks && line_info.in_math_block)
            || (self.skip_quarto_divs && line_info.in_quarto_div)
            || (self.skip_jsx_expressions && line_info.in_jsx_expression)
            || (self.skip_mdx_comments && line_info.in_mdx_comment)
            || (self.skip_admonitions && line_info.in_admonition)
            || (self.skip_content_tabs && line_info.in_content_tab)
            || (self.skip_mkdocs_html_markdown && line_info.in_mkdocs_html_markdown)
            || (self.skip_definition_lists && line_info.in_definition_list)
            || (self.skip_obsidian_comments && line_info.in_obsidian_comment)
            || (self.skip_pymdown_blocks && line_info.in_pymdown_block)
            || (self.skip_div_markers && line_info.is_div_marker)
            || (self.skip_jsx_blocks && line_info.in_jsx_block)
    }
}

/// Iterator that yields filtered lines based on configuration
pub struct FilteredLinesIter<'a> {
    ctx: &'a LintContext<'a>,
    config: LineFilterConfig,
    current_index: usize,
}

impl<'a> FilteredLinesIter<'a> {
    /// Create a new filtered lines iterator
    fn new(ctx: &'a LintContext<'a>, config: LineFilterConfig) -> Self {
        Self {
            ctx,
            config,
            current_index: 0,
        }
    }
}

impl<'a> Iterator for FilteredLinesIter<'a> {
    type Item = FilteredLine<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        let lines = &self.ctx.lines;
        let raw_lines = self.ctx.raw_lines();

        while self.current_index < lines.len() {
            let idx = self.current_index;
            self.current_index += 1;

            // Check if this line should be filtered
            if self.config.should_filter(&lines[idx]) {
                continue;
            }

            // Get the actual line content from pre-split lines
            let line_content = raw_lines.get(idx).copied().unwrap_or("");

            // Return the filtered line with 1-indexed line number
            return Some(FilteredLine {
                line_num: idx + 1, // Convert 0-indexed to 1-indexed
                line_info: &lines[idx],
                content: line_content,
            });
        }

        None
    }
}

/// Extension trait that adds filtered iteration methods to `LintContext`
///
/// This trait provides convenient methods for iterating over lines while
/// automatically filtering out non-content regions.
pub trait FilteredLinesExt {
    /// Start building a filtered lines iterator
    ///
    /// Returns a `LineFilterConfig` builder that can be used to configure
    /// which types of content should be filtered out.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rumdl_lib::lint_context::LintContext;
    /// use rumdl_lib::filtered_lines::FilteredLinesExt;
    ///
    /// let content = "# Title\n\n```rust\ncode\n```\n\nContent";
    /// let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    ///
    /// for line in ctx.filtered_lines().skip_code_blocks() {
    ///     println!("Line {}: {}", line.line_num, line.content);
    /// }
    /// ```
    fn filtered_lines(&self) -> FilteredLinesBuilder<'_>;

    /// Get an iterator over content lines only
    ///
    /// This is a convenience method that returns an iterator with front matter
    /// filtered out by default. This is the most common use case for rules that
    /// should only process markdown content.
    ///
    /// Equivalent to: `ctx.filtered_lines().skip_front_matter()`
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rumdl_lib::lint_context::LintContext;
    /// use rumdl_lib::filtered_lines::FilteredLinesExt;
    ///
    /// let content = "---\ntitle: Test\n---\n\n# Content";
    /// let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    ///
    /// for line in ctx.content_lines() {
    ///     // Front matter is automatically skipped
    ///     println!("Line {}: {}", line.line_num, line.content);
    /// }
    /// ```
    fn content_lines(&self) -> FilteredLinesIter<'_>;
}

/// Builder type that allows chaining filter configuration and converting to an iterator
pub struct FilteredLinesBuilder<'a> {
    ctx: &'a LintContext<'a>,
    config: LineFilterConfig,
}

impl<'a> FilteredLinesBuilder<'a> {
    fn new(ctx: &'a LintContext<'a>) -> Self {
        Self {
            ctx,
            config: LineFilterConfig::new(),
        }
    }

    /// Skip lines that are part of front matter (YAML/TOML/JSON)
    #[must_use]
    pub fn skip_front_matter(mut self) -> Self {
        self.config = self.config.skip_front_matter();
        self
    }

    /// Skip lines inside fenced code blocks
    #[must_use]
    pub fn skip_code_blocks(mut self) -> Self {
        self.config = self.config.skip_code_blocks();
        self
    }

    /// Skip lines inside HTML blocks
    #[must_use]
    pub fn skip_html_blocks(mut self) -> Self {
        self.config = self.config.skip_html_blocks();
        self
    }

    /// Skip lines inside HTML comments
    #[must_use]
    pub fn skip_html_comments(mut self) -> Self {
        self.config = self.config.skip_html_comments();
        self
    }

    /// Skip lines inside mkdocstrings blocks
    #[must_use]
    pub fn skip_mkdocstrings(mut self) -> Self {
        self.config = self.config.skip_mkdocstrings();
        self
    }

    /// Skip lines inside ESM (ECMAScript Module) blocks
    #[must_use]
    pub fn skip_esm_blocks(mut self) -> Self {
        self.config = self.config.skip_esm_blocks();
        self
    }

    /// Skip lines inside math blocks ($$ ... $$)
    #[must_use]
    pub fn skip_math_blocks(mut self) -> Self {
        self.config = self.config.skip_math_blocks();
        self
    }

    /// Skip lines inside Quarto div blocks (::: ... :::)
    #[must_use]
    pub fn skip_quarto_divs(mut self) -> Self {
        self.config = self.config.skip_quarto_divs();
        self
    }

    /// Skip lines containing or inside JSX expressions (MDX: {expression})
    #[must_use]
    pub fn skip_jsx_expressions(mut self) -> Self {
        self.config = self.config.skip_jsx_expressions();
        self
    }

    /// Skip lines inside MDX comments ({/* ... */})
    #[must_use]
    pub fn skip_mdx_comments(mut self) -> Self {
        self.config = self.config.skip_mdx_comments();
        self
    }

    /// Skip lines inside MkDocs admonitions (!!! or ???)
    #[must_use]
    pub fn skip_admonitions(mut self) -> Self {
        self.config = self.config.skip_admonitions();
        self
    }

    /// Skip lines inside MkDocs content tabs (=== "Tab")
    #[must_use]
    pub fn skip_content_tabs(mut self) -> Self {
        self.config = self.config.skip_content_tabs();
        self
    }

    /// Skip lines inside HTML blocks with markdown attribute (MkDocs grid cards, etc.)
    #[must_use]
    pub fn skip_mkdocs_html_markdown(mut self) -> Self {
        self.config = self.config.skip_mkdocs_html_markdown();
        self
    }

    /// Skip lines inside any MkDocs container (admonitions, content tabs, or markdown HTML divs)
    ///
    /// This is a convenience method that enables `skip_admonitions`,
    /// `skip_content_tabs`, and `skip_mkdocs_html_markdown`. MkDocs containers use
    /// 4-space indented content which may need special handling to preserve structure.
    #[must_use]
    pub fn skip_mkdocs_containers(mut self) -> Self {
        self.config = self.config.skip_mkdocs_containers();
        self
    }

    /// Skip lines inside definition lists (:  definition)
    #[must_use]
    pub fn skip_definition_lists(mut self) -> Self {
        self.config = self.config.skip_definition_lists();
        self
    }

    /// Skip lines inside Obsidian comments (%%...%%)
    #[must_use]
    pub fn skip_obsidian_comments(mut self) -> Self {
        self.config = self.config.skip_obsidian_comments();
        self
    }

    /// Skip lines inside PyMdown Blocks (/// ... ///)
    #[must_use]
    pub fn skip_pymdown_blocks(mut self) -> Self {
        self.config = self.config.skip_pymdown_blocks();
        self
    }

    /// Skip lines inside kramdown extension blocks ({::comment}...{:/comment}, etc.)
    #[must_use]
    pub fn skip_kramdown_extension_blocks(mut self) -> Self {
        self.config = self.config.skip_kramdown_extension_blocks();
        self
    }

    /// Skip lines that are div markers (::: opening or closing)
    ///
    /// Unlike `skip_quarto_divs` which skips ALL lines inside a div block,
    /// this only skips the `:::` marker lines themselves.
    #[must_use]
    pub fn skip_div_markers(mut self) -> Self {
        self.config = self.config.skip_div_markers();
        self
    }
}

impl<'a> IntoIterator for FilteredLinesBuilder<'a> {
    type Item = FilteredLine<'a>;
    type IntoIter = FilteredLinesIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        FilteredLinesIter::new(self.ctx, self.config)
    }
}

impl<'a> FilteredLinesExt for LintContext<'a> {
    fn filtered_lines(&self) -> FilteredLinesBuilder<'_> {
        FilteredLinesBuilder::new(self)
    }

    fn content_lines(&self) -> FilteredLinesIter<'_> {
        FilteredLinesIter::new(self, LineFilterConfig::new().skip_front_matter())
    }
}

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

    #[test]
    fn test_filtered_line_structure() {
        let content = "# Title\n\nContent";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);

        let line = ctx.content_lines().next().unwrap();
        assert_eq!(line.line_num, 1);
        assert_eq!(line.content, "# Title");
        assert!(!line.line_info.in_front_matter);
    }

    #[test]
    fn test_skip_front_matter_yaml() {
        let content = "---\ntitle: Test\nurl: http://example.com\n---\n\n# Content\n\nMore content";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);

        let lines: Vec<_> = ctx.content_lines().collect();
        // After front matter (lines 1-4), we have: empty line, "# Content", empty line, "More content"
        assert_eq!(lines.len(), 4);
        assert_eq!(lines[0].line_num, 5); // First line after front matter
        assert_eq!(lines[0].content, "");
        assert_eq!(lines[1].line_num, 6);
        assert_eq!(lines[1].content, "# Content");
        assert_eq!(lines[2].line_num, 7);
        assert_eq!(lines[2].content, "");
        assert_eq!(lines[3].line_num, 8);
        assert_eq!(lines[3].content, "More content");
    }

    #[test]
    fn test_skip_front_matter_toml() {
        let content = "+++\ntitle = \"Test\"\nurl = \"http://example.com\"\n+++\n\n# Content";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);

        let lines: Vec<_> = ctx.content_lines().collect();
        assert_eq!(lines.len(), 2); // Empty line + "# Content"
        assert_eq!(lines[0].line_num, 5);
        assert_eq!(lines[1].line_num, 6);
        assert_eq!(lines[1].content, "# Content");
    }

    #[test]
    fn test_skip_front_matter_json() {
        let content = "{\n\"title\": \"Test\",\n\"url\": \"http://example.com\"\n}\n\n# Content";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);

        let lines: Vec<_> = ctx.content_lines().collect();
        assert_eq!(lines.len(), 2); // Empty line + "# Content"
        assert_eq!(lines[0].line_num, 5);
        assert_eq!(lines[1].line_num, 6);
        assert_eq!(lines[1].content, "# Content");
    }

    #[test]
    fn test_skip_code_blocks() {
        let content = "# Title\n\n```rust\nlet x = 1;\nlet y = 2;\n```\n\nContent";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);

        let lines: Vec<_> = ctx.filtered_lines().skip_code_blocks().into_iter().collect();

        // Should have: "# Title", empty line, "```rust" fence, "```" fence, empty line, "Content"
        // Wait, actually code blocks include the fences. Let me check the line_info
        // Looking at the implementation, in_code_block is true for lines INSIDE code blocks
        // The fences themselves are not marked as in_code_block
        assert!(lines.iter().any(|l| l.content == "# Title"));
        assert!(lines.iter().any(|l| l.content == "Content"));
        // The actual code lines should be filtered out
        assert!(!lines.iter().any(|l| l.content == "let x = 1;"));
        assert!(!lines.iter().any(|l| l.content == "let y = 2;"));
    }

    #[test]
    fn test_no_filters() {
        let content = "---\ntitle: Test\n---\n\n# Content";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);

        // With no filters, all lines should be included
        let lines: Vec<_> = ctx.filtered_lines().into_iter().collect();
        assert_eq!(lines.len(), ctx.lines.len());
    }

    #[test]
    fn test_multiple_filters() {
        let content = "---\ntitle: Test\n---\n\n# Title\n\n```rust\ncode\n```\n\nContent";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);

        let lines: Vec<_> = ctx
            .filtered_lines()
            .skip_front_matter()
            .skip_code_blocks()
            .into_iter()
            .collect();

        // Should skip front matter (lines 1-3) and code block content (line 8)
        assert!(lines.iter().any(|l| l.content == "# Title"));
        assert!(lines.iter().any(|l| l.content == "Content"));
        assert!(!lines.iter().any(|l| l.content == "title: Test"));
        assert!(!lines.iter().any(|l| l.content == "code"));
    }

    #[test]
    fn test_line_numbering_is_1_indexed() {
        let content = "First\nSecond\nThird";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);

        let lines: Vec<_> = ctx.content_lines().collect();
        assert_eq!(lines[0].line_num, 1);
        assert_eq!(lines[0].content, "First");
        assert_eq!(lines[1].line_num, 2);
        assert_eq!(lines[1].content, "Second");
        assert_eq!(lines[2].line_num, 3);
        assert_eq!(lines[2].content, "Third");
    }

    #[test]
    fn test_content_lines_convenience_method() {
        let content = "---\nfoo: bar\n---\n\nContent";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);

        // content_lines() should automatically skip front matter
        let lines: Vec<_> = ctx.content_lines().collect();
        assert!(!lines.iter().any(|l| l.content.contains("foo")));
        assert!(lines.iter().any(|l| l.content == "Content"));
    }

    #[test]
    fn test_empty_document() {
        let content = "";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);

        let lines: Vec<_> = ctx.content_lines().collect();
        assert_eq!(lines.len(), 0);
    }

    #[test]
    fn test_only_front_matter() {
        let content = "---\ntitle: Test\n---";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);

        let lines: Vec<_> = ctx.content_lines().collect();
        assert_eq!(
            lines.len(),
            0,
            "Document with only front matter should have no content lines"
        );
    }

    #[test]
    fn test_builder_pattern_ergonomics() {
        let content = "# Title\n\n```\ncode\n```\n\nContent";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);

        // Test that builder pattern works smoothly
        let _lines: Vec<_> = ctx
            .filtered_lines()
            .skip_front_matter()
            .skip_code_blocks()
            .skip_html_blocks()
            .into_iter()
            .collect();

        // If this compiles and runs, the builder pattern is working
    }

    #[test]
    fn test_filtered_line_access_to_line_info() {
        let content = "# Title\n\nContent";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);

        for line in ctx.content_lines() {
            // Should be able to access line_info fields
            assert!(!line.line_info.in_front_matter);
            assert!(!line.line_info.in_code_block);
        }
    }

    #[test]
    fn test_skip_mkdocstrings() {
        let content = r#"# API Documentation

::: mymodule.MyClass
    options:
      show_root_heading: true
      show_source: false

Some regular content here.

::: mymodule.function
    options:
      show_signature: true

More content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::MkDocs, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_mkdocstrings().into_iter().collect();

        // Verify lines OUTSIDE mkdocstrings blocks are INCLUDED
        assert!(
            lines.iter().any(|l| l.content.contains("# API Documentation")),
            "Should include lines outside mkdocstrings blocks"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Some regular content")),
            "Should include content between mkdocstrings blocks"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("More content")),
            "Should include content after mkdocstrings blocks"
        );

        // Verify lines INSIDE mkdocstrings blocks are EXCLUDED
        assert!(
            !lines.iter().any(|l| l.content.contains("::: mymodule")),
            "Should exclude mkdocstrings marker lines"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("show_root_heading")),
            "Should exclude mkdocstrings option lines"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("show_signature")),
            "Should exclude all mkdocstrings option lines"
        );

        // Verify line numbers are preserved (1-indexed)
        assert_eq!(lines[0].line_num, 1, "First line should be line 1");
    }

    #[test]
    fn test_skip_esm_blocks() {
        // MDX 2.0+ allows ESM imports/exports anywhere in the document
        let content = r#"import {Chart} from './components.js'
import {Table} from './table.js'
export const year = 2023

# Last year's snowfall

Content about snowfall data.

import {Footer} from './footer.js'

More content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::MDX, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_esm_blocks().into_iter().collect();

        // Verify lines OUTSIDE ESM blocks are INCLUDED
        assert!(
            lines.iter().any(|l| l.content.contains("# Last year's snowfall")),
            "Should include markdown headings"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Content about snowfall")),
            "Should include markdown content"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("More content")),
            "Should include content after ESM blocks"
        );

        // Verify ALL ESM blocks are EXCLUDED (MDX 2.0+ allows imports anywhere)
        assert!(
            !lines.iter().any(|l| l.content.contains("import {Chart}")),
            "Should exclude import statements at top of file"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("import {Table}")),
            "Should exclude all import statements at top of file"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("export const year")),
            "Should exclude export statements at top of file"
        );
        // MDX 2.0+ allows imports anywhere - they should ALL be excluded
        assert!(
            !lines.iter().any(|l| l.content.contains("import {Footer}")),
            "Should exclude import statements even after markdown content (MDX 2.0+ ESM anywhere)"
        );

        // Verify line numbers are preserved
        let heading_line = lines
            .iter()
            .find(|l| l.content.contains("# Last year's snowfall"))
            .unwrap();
        assert_eq!(heading_line.line_num, 5, "Heading should be on line 5");
    }

    #[test]
    fn test_all_filters_combined() {
        let content = r#"---
title: Test
---

# Title

```
code
```

<!-- HTML comment here -->

::: mymodule.Class
    options:
      show_root_heading: true

<div>
HTML block
</div>

Content"#;
        let ctx = LintContext::new(content, MarkdownFlavor::MkDocs, None);

        let lines: Vec<_> = ctx
            .filtered_lines()
            .skip_front_matter()
            .skip_code_blocks()
            .skip_html_blocks()
            .skip_html_comments()
            .skip_mkdocstrings()
            .into_iter()
            .collect();

        // Verify markdown content is INCLUDED
        assert!(
            lines.iter().any(|l| l.content == "# Title"),
            "Should include markdown headings"
        );
        assert!(
            lines.iter().any(|l| l.content == "Content"),
            "Should include markdown content"
        );

        // Verify all filtered content is EXCLUDED
        assert!(
            !lines.iter().any(|l| l.content == "title: Test"),
            "Should exclude front matter"
        );
        assert!(
            !lines.iter().any(|l| l.content == "code"),
            "Should exclude code block content"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("HTML comment")),
            "Should exclude HTML comments"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("::: mymodule")),
            "Should exclude mkdocstrings blocks"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("show_root_heading")),
            "Should exclude mkdocstrings options"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("HTML block")),
            "Should exclude HTML blocks"
        );
    }

    #[test]
    fn test_skip_math_blocks() {
        let content = r#"# Heading

Some regular text.

$$
A = \left[
\begin{array}{c}
1 \\
-D
\end{array}
\right]
$$

More content after math."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_math_blocks().into_iter().collect();

        // Verify lines OUTSIDE math blocks are INCLUDED
        assert!(
            lines.iter().any(|l| l.content.contains("# Heading")),
            "Should include markdown headings"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Some regular text")),
            "Should include regular text before math block"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("More content after math")),
            "Should include content after math block"
        );

        // Verify lines INSIDE math blocks are EXCLUDED
        assert!(
            !lines.iter().any(|l| l.content == "$$"),
            "Should exclude math block delimiters"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("\\left[")),
            "Should exclude LaTeX content inside math block"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("-D")),
            "Should exclude content that looks like list items inside math block"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("\\begin{array}")),
            "Should exclude LaTeX array content"
        );
    }

    #[test]
    fn test_math_blocks_not_confused_with_code_blocks() {
        let content = r#"# Title

```python
# This $$ is inside a code block
x = 1
```

$$
y = 2
$$

Regular text."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);

        // Check that the $$ inside code block doesn't start a math block
        let lines: Vec<_> = ctx.filtered_lines().skip_math_blocks().into_iter().collect();

        // The $$ inside the code block should NOT trigger math block detection
        // So when we skip math blocks, the code block content is still there (until we also skip code blocks)
        assert!(
            lines.iter().any(|l| l.content.contains("# This $$")),
            "Code block content with $$ should not be detected as math block"
        );

        // But the real math block content should be excluded
        assert!(
            !lines.iter().any(|l| l.content == "y = 2"),
            "Actual math block content should be excluded"
        );
    }

    #[test]
    fn test_skip_quarto_divs() {
        let content = r#"# Heading

::: {.callout-note}
This is a callout note.
With multiple lines.
:::

Regular text outside.

::: {.bordered}
Content inside bordered div.
:::

More content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Quarto, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_quarto_divs().into_iter().collect();

        // Verify lines OUTSIDE Quarto divs are INCLUDED
        assert!(
            lines.iter().any(|l| l.content.contains("# Heading")),
            "Should include markdown headings"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Regular text outside")),
            "Should include content between divs"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("More content")),
            "Should include content after divs"
        );

        // Verify lines INSIDE Quarto divs are EXCLUDED
        assert!(
            !lines.iter().any(|l| l.content.contains("::: {.callout-note}")),
            "Should exclude callout div markers"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("This is a callout note")),
            "Should exclude callout content"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("Content inside bordered")),
            "Should exclude bordered div content"
        );
    }

    #[test]
    fn test_skip_jsx_expressions() {
        let content = r#"# MDX Document

Here is some content with {myVariable} inline.

{items.map(item => (
  <Item key={item.id} />
))}

Regular paragraph after expression.

{/* This should NOT be skipped by jsx_expressions filter */}
{/* MDX comments have their own filter */}

More content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::MDX, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_jsx_expressions().into_iter().collect();

        // Verify lines OUTSIDE JSX expressions are INCLUDED
        assert!(
            lines.iter().any(|l| l.content.contains("# MDX Document")),
            "Should include markdown headings"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Regular paragraph")),
            "Should include regular paragraphs"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("More content")),
            "Should include content after expressions"
        );

        // Verify lines with JSX expressions are EXCLUDED
        assert!(
            !lines.iter().any(|l| l.content.contains("{myVariable}")),
            "Should exclude lines with inline JSX expressions"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("items.map")),
            "Should exclude multi-line JSX expression content"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("<Item key")),
            "Should exclude JSX inside expressions"
        );
    }

    #[test]
    fn test_skip_quarto_divs_nested() {
        let content = r#"# Title

::: {.outer}
Outer content.

::: {.inner}
Inner content.
:::

Back to outer.
:::

Outside text."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Quarto, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_quarto_divs().into_iter().collect();

        // Should include content outside all divs
        assert!(
            lines.iter().any(|l| l.content.contains("# Title")),
            "Should include heading"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Outside text")),
            "Should include text after divs"
        );

        // Should exclude all div content
        assert!(
            !lines.iter().any(|l| l.content.contains("Outer content")),
            "Should exclude outer div content"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("Inner content")),
            "Should exclude inner div content"
        );
    }

    #[test]
    fn test_skip_quarto_divs_not_in_standard_flavor() {
        let content = r#"::: {.callout-note}
This should NOT be skipped in standard flavor.
:::"#;
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_quarto_divs().into_iter().collect();

        // In standard flavor, Quarto divs are not detected, so nothing is skipped
        assert!(
            lines.iter().any(|l| l.content.contains("This should NOT be skipped")),
            "Standard flavor should not detect Quarto divs"
        );
    }

    #[test]
    fn test_skip_mdx_comments() {
        let content = r#"# MDX Document

{/* This is an MDX comment */}

Regular content here.

{/*
  Multi-line
  MDX comment
*/}

More content after comment."#;
        let ctx = LintContext::new(content, MarkdownFlavor::MDX, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_mdx_comments().into_iter().collect();

        // Verify lines OUTSIDE MDX comments are INCLUDED
        assert!(
            lines.iter().any(|l| l.content.contains("# MDX Document")),
            "Should include markdown headings"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Regular content")),
            "Should include regular content"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("More content")),
            "Should include content after comments"
        );

        // Verify lines with MDX comments are EXCLUDED
        assert!(
            !lines.iter().any(|l| l.content.contains("{/* This is")),
            "Should exclude single-line MDX comments"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("Multi-line")),
            "Should exclude multi-line MDX comment content"
        );
    }

    #[test]
    fn test_jsx_expressions_with_nested_braces() {
        // Test that nested braces are handled correctly
        let content = r#"# Document

{props.style || {color: "red", background: "blue"}}

Regular content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::MDX, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_jsx_expressions().into_iter().collect();

        // Verify nested braces don't break detection
        assert!(
            !lines.iter().any(|l| l.content.contains("props.style")),
            "Should exclude JSX expression with nested braces"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Regular content")),
            "Should include content after nested expression"
        );
    }

    #[test]
    fn test_jsx_and_mdx_comments_combined() {
        // Test both filters together
        let content = r#"# Title

{variable}

{/* comment */}

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::MDX, None);
        let lines: Vec<_> = ctx
            .filtered_lines()
            .skip_jsx_expressions()
            .skip_mdx_comments()
            .into_iter()
            .collect();

        assert!(
            lines.iter().any(|l| l.content.contains("# Title")),
            "Should include heading"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Content")),
            "Should include regular content"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("{variable}")),
            "Should exclude JSX expression"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("{/* comment */")),
            "Should exclude MDX comment"
        );
    }

    #[test]
    fn test_jsx_expressions_not_detected_in_standard_flavor() {
        // JSX expressions should only be detected in MDX flavor
        let content = r#"# Document

{this is not JSX in standard markdown}

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_jsx_expressions().into_iter().collect();

        // In standard markdown, braces are just text - nothing should be filtered
        assert!(
            lines.iter().any(|l| l.content.contains("{this is not JSX")),
            "Should NOT exclude brace content in standard markdown"
        );
    }

    // ==================== Obsidian Comment Tests ====================

    #[test]
    fn test_skip_obsidian_comments_simple_inline() {
        // Simple inline comment: text %%hidden%% text
        let content = r#"# Heading

This is visible %%this is hidden%% and visible again.

More content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        // All lines should be included - inline comments don't hide entire lines
        assert!(
            lines.iter().any(|l| l.content.contains("# Heading")),
            "Should include heading"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("This is visible")),
            "Should include line with inline comment"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("More content")),
            "Should include content after comment"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_multiline_block() {
        // Multi-line comment block
        let content = r#"# Heading

%%
This is a multi-line
comment block
%%

Content after."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        // Should include content outside the comment block
        assert!(
            lines.iter().any(|l| l.content.contains("# Heading")),
            "Should include heading"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Content after")),
            "Should include content after comment block"
        );

        // Lines inside the comment block should be excluded
        assert!(
            !lines.iter().any(|l| l.content.contains("This is a multi-line")),
            "Should exclude multi-line comment content"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("comment block")),
            "Should exclude multi-line comment content"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_in_code_block() {
        // %% inside code blocks should NOT be treated as comments
        let content = r#"# Heading

```
%% This is NOT a comment
It's inside a code block
%%
```

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx
            .filtered_lines()
            .skip_obsidian_comments()
            .skip_code_blocks()
            .into_iter()
            .collect();

        // The code block content should be excluded by skip_code_blocks, not by obsidian comments
        assert!(
            lines.iter().any(|l| l.content.contains("# Heading")),
            "Should include heading"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Content")),
            "Should include content after code block"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_in_html_comment() {
        // %% inside HTML comments should NOT be treated as Obsidian comments
        let content = r#"# Heading

<!-- %% This is inside HTML comment %% -->

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx
            .filtered_lines()
            .skip_obsidian_comments()
            .skip_html_comments()
            .into_iter()
            .collect();

        assert!(
            lines.iter().any(|l| l.content.contains("# Heading")),
            "Should include heading"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Content")),
            "Should include content"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_empty() {
        // Empty comment: %%%%
        let content = r#"# Heading

%%%% empty comment

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        // Empty comments should be handled gracefully
        assert!(
            lines.iter().any(|l| l.content.contains("# Heading")),
            "Should include heading"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_unclosed() {
        // Unclosed comment extends to end of document
        let content = r#"# Heading

%% starts but never ends
This should be hidden
Until end of document"#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        // Should include content before the unclosed comment
        assert!(
            lines.iter().any(|l| l.content.contains("# Heading")),
            "Should include heading before unclosed comment"
        );

        // Content after the %% should be excluded
        assert!(
            !lines.iter().any(|l| l.content.contains("This should be hidden")),
            "Should exclude content in unclosed comment"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("Until end of document")),
            "Should exclude content until end of document"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_multiple_on_same_line() {
        // Multiple comments on same line
        let content = r#"# Heading

First %%hidden1%% middle %%hidden2%% last

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        // Line should still be included (inline comments)
        assert!(
            lines.iter().any(|l| l.content.contains("First")),
            "Should include line with multiple inline comments"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("middle")),
            "Should include visible text between comments"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_at_start_of_line() {
        // Comment at start of line
        let content = r#"# Heading

%%comment at start%%

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        assert!(
            lines.iter().any(|l| l.content.contains("# Heading")),
            "Should include heading"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Content")),
            "Should include content"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_at_end_of_line() {
        // Comment at end of line
        let content = r#"# Heading

Some text %%comment at end%%

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        assert!(
            lines.iter().any(|l| l.content.contains("Some text")),
            "Should include text before comment"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_with_markdown_inside() {
        // Comments containing special markdown
        let content = r#"# Heading

%%
# hidden heading
[hidden link](url)
**hidden bold**
%%

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        assert!(
            !lines.iter().any(|l| l.content.contains("# hidden heading")),
            "Should exclude heading inside comment"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("[hidden link]")),
            "Should exclude link inside comment"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("**hidden bold**")),
            "Should exclude bold inside comment"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_with_unicode() {
        // Unicode content inside comments
        let content = r#"# Heading

%%日本語コメント%%

%%Комментарий%%

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        // Lines with only comments should be handled properly
        assert!(
            lines.iter().any(|l| l.content.contains("# Heading")),
            "Should include heading"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Content")),
            "Should include content"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_triple_percent() {
        // Odd number of percent signs: %%%
        let content = r#"# Heading

%%% odd percent

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        // Should handle gracefully - the %%% starts a comment, single % is content
        assert!(
            lines.iter().any(|l| l.content.contains("# Heading")),
            "Should include heading"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_not_in_standard_flavor() {
        // Obsidian comments should NOT be detected in Standard flavor
        let content = r#"# Heading

%%this is not hidden in standard%%

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        // In Standard flavor, %% is just text - nothing should be filtered
        assert!(
            lines.iter().any(|l| l.content.contains("%%this is not hidden")),
            "Should NOT hide %% content in Standard flavor"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_integration_with_other_filters() {
        // Test combining with frontmatter and code block filters
        let content = r#"---
title: Test
---

# Heading

```
code
```

%%hidden comment%%

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx
            .filtered_lines()
            .skip_front_matter()
            .skip_code_blocks()
            .skip_obsidian_comments()
            .into_iter()
            .collect();

        // Should skip frontmatter, code blocks, and Obsidian comments
        assert!(
            !lines.iter().any(|l| l.content.contains("title: Test")),
            "Should skip frontmatter"
        );
        assert!(
            !lines.iter().any(|l| l.content == "code"),
            "Should skip code block content"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("# Heading")),
            "Should include heading"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Content")),
            "Should include content"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_whole_line_only() {
        // Multi-line comment should only mark lines entirely within the comment
        let content = "start %%\nfully hidden\n%% end";
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        // First line starts before comment, should be included
        assert!(
            lines.iter().any(|l| l.content.contains("start")),
            "First line should be included (starts outside comment)"
        );
        // Middle line is entirely within comment, should be excluded
        assert!(
            !lines.iter().any(|l| l.content == "fully hidden"),
            "Middle line should be excluded (entirely within comment)"
        );
        // Last line ends after comment, should be included
        assert!(
            lines.iter().any(|l| l.content.contains("end")),
            "Last line should be included (ends outside comment)"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_in_inline_code() {
        // %% inside inline code spans should NOT be treated as comments
        let content = r#"# Heading

The syntax is `%%comment%%` in Obsidian.

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        // The line with code span should be included
        assert!(
            lines.iter().any(|l| l.content.contains("The syntax is")),
            "Should include line with %% in code span"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("in Obsidian")),
            "Should include text after code span"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_in_inline_code_multi_backtick() {
        // %% inside inline code spans with multiple backticks should NOT be treated as comments
        let content = r#"# Heading

The syntax is ``%%comment%%`` in Obsidian.

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        assert!(
            lines.iter().any(|l| l.content.contains("The syntax is")),
            "Should include line with %% in multi-backtick code span"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Content")),
            "Should include content after code span"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_consecutive_blocks() {
        // Multiple consecutive comment blocks
        let content = r#"# Heading

%%comment 1%%

%%comment 2%%

Content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        assert!(
            lines.iter().any(|l| l.content.contains("# Heading")),
            "Should include heading"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Content")),
            "Should include content after comments"
        );
    }

    #[test]
    fn test_skip_obsidian_comments_spanning_many_lines() {
        // Comment block spanning many lines
        let content = r#"# Title

%%
Line 1 of comment
Line 2 of comment
Line 3 of comment
Line 4 of comment
Line 5 of comment
%%

After comment."#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_obsidian_comments().into_iter().collect();

        // All lines inside the comment should be excluded
        for i in 1..=5 {
            assert!(
                !lines
                    .iter()
                    .any(|l| l.content.contains(&format!("Line {i} of comment"))),
                "Should exclude line {i} of comment"
            );
        }

        assert!(
            lines.iter().any(|l| l.content.contains("# Title")),
            "Should include title"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("After comment")),
            "Should include content after comment"
        );
    }

    #[test]
    fn test_obsidian_comment_line_info_field() {
        // Verify the in_obsidian_comment field is set correctly
        let content = "visible\n%%\nhidden\n%%\nvisible";
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);

        // Line 0: visible - should NOT be in comment
        assert!(
            !ctx.lines[0].in_obsidian_comment,
            "Line 0 should not be marked as in_obsidian_comment"
        );

        // Line 2: hidden - should be in comment
        assert!(
            ctx.lines[2].in_obsidian_comment,
            "Line 2 (hidden) should be marked as in_obsidian_comment"
        );

        // Line 4: visible - should NOT be in comment
        assert!(
            !ctx.lines[4].in_obsidian_comment,
            "Line 4 should not be marked as in_obsidian_comment"
        );
    }

    // ==================== PyMdown Blocks Filter Tests ====================

    #[test]
    fn test_skip_pymdown_blocks_basic() {
        // Basic PyMdown block (caption)
        let content = r#"# Heading

/// caption
Table caption here.
///

Content after."#;
        let ctx = LintContext::new(content, MarkdownFlavor::MkDocs, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_pymdown_blocks().into_iter().collect();

        // Should include heading and content after
        assert!(
            lines.iter().any(|l| l.content.contains("# Heading")),
            "Should include heading"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Content after")),
            "Should include content after block"
        );

        // Should NOT include content inside the block
        assert!(
            !lines.iter().any(|l| l.content.contains("Table caption")),
            "Should exclude content inside block"
        );
    }

    #[test]
    fn test_skip_pymdown_blocks_details() {
        // Details block with summary
        let content = r#"# Heading

/// details | Click to expand
    open: True
Hidden content here.
More hidden content.
///

Visible content."#;
        let ctx = LintContext::new(content, MarkdownFlavor::MkDocs, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_pymdown_blocks().into_iter().collect();

        assert!(
            !lines.iter().any(|l| l.content.contains("Hidden content")),
            "Should exclude hidden content"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("open: True")),
            "Should exclude YAML options"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("Visible content")),
            "Should include visible content"
        );
    }

    #[test]
    fn test_skip_pymdown_blocks_nested() {
        // Nested blocks
        let content = r#"# Title

/// details | Outer
Outer content.

  /// caption
  Inner caption.
  ///

More outer content.
///

After all blocks."#;
        let ctx = LintContext::new(content, MarkdownFlavor::MkDocs, None);
        let lines: Vec<_> = ctx.filtered_lines().skip_pymdown_blocks().into_iter().collect();

        assert!(
            !lines.iter().any(|l| l.content.contains("Outer content")),
            "Should exclude outer block content"
        );
        assert!(
            !lines.iter().any(|l| l.content.contains("Inner caption")),
            "Should exclude inner block content"
        );
        assert!(
            lines.iter().any(|l| l.content.contains("After all blocks")),
            "Should include content after all blocks"
        );
    }

    #[test]
    fn test_pymdown_block_line_info_field() {
        // Verify the in_pymdown_block field is set correctly
        let content = "visible\n/// caption\nhidden\n///\nvisible";
        let ctx = LintContext::new(content, MarkdownFlavor::MkDocs, None);

        // Line 0: visible - should NOT be in block
        assert!(
            !ctx.lines[0].in_pymdown_block,
            "Line 0 should not be marked as in_pymdown_block"
        );

        // Line 1: /// caption - should be in block
        assert!(
            ctx.lines[1].in_pymdown_block,
            "Line 1 (/// caption) should be marked as in_pymdown_block"
        );

        // Line 2: hidden - should be in block
        assert!(
            ctx.lines[2].in_pymdown_block,
            "Line 2 (hidden) should be marked as in_pymdown_block"
        );

        // Line 3: /// - closing should still be in block range
        assert!(
            ctx.lines[3].in_pymdown_block,
            "Line 3 (closing ///) should be marked as in_pymdown_block"
        );

        // Line 4: visible - should NOT be in block
        assert!(
            !ctx.lines[4].in_pymdown_block,
            "Line 4 should not be marked as in_pymdown_block"
        );
    }

    #[test]
    fn test_pymdown_blocks_only_for_mkdocs_flavor() {
        // PyMdown blocks should only be detected for MkDocs flavor
        let content = "/// caption\nCaption text\n///";

        // Test with MkDocs flavor - should detect block
        let ctx_mkdocs = LintContext::new(content, MarkdownFlavor::MkDocs, None);
        assert!(
            ctx_mkdocs.lines[1].in_pymdown_block,
            "MkDocs flavor should detect pymdown blocks"
        );

        // Test with Standard flavor - should NOT detect block
        let ctx_standard = LintContext::new(content, MarkdownFlavor::Standard, None);
        assert!(
            !ctx_standard.lines[1].in_pymdown_block,
            "Standard flavor should NOT detect pymdown blocks"
        );
    }
}