blz-core 1.5.5

Core library for fast local llms.txt search
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
//! Markdown parsing using tree-sitter for structured content analysis.
//!
//! This module provides robust markdown parsing capabilities using tree-sitter,
//! which enables precise syntax analysis and structured extraction of headings,
//! content blocks, and table of contents information.
//!
//! ## Features
//!
//! - **Hierarchical Structure**: Builds nested heading structures matching document organization
//! - **Error Resilience**: Continues parsing even with malformed markdown syntax  
//! - **Diagnostics**: Reports issues found during parsing for quality assurance
//! - **Performance**: Efficiently handles large documents (< 150ms per MB)
//! - **Unicode Support**: Full Unicode support including complex scripts and emoji
//!
//! ## Architecture
//!
//! The parser uses tree-sitter for tokenization and syntax analysis, then builds
//! structured representations:
//!
//! 1. **Tokenization**: tree-sitter parses markdown into a syntax tree
//! 2. **Structure Extraction**: Traverse tree to identify headings and content blocks
//! 3. **Hierarchy Building**: Construct nested TOC and heading block structures
//! 4. **Validation**: Generate diagnostics for quality issues
//!
//! ## Examples
//!
//! ### Basic parsing:
//!
//! ```rust
//! use blz_core::{MarkdownParser, Result};
//!
//! let mut parser = MarkdownParser::new()?;
//! let result = parser.parse(r#"
//! # Getting Started
//!
//! Welcome to the documentation.
//!
//! ## Installation
//!
//! Run the following command:
//! cargo install blz
//!
//! ## Usage
//!
//! Basic usage example.
//! "#)?;
//!
//! println!("Found {} heading blocks", result.heading_blocks.len());
//! println!("TOC has {} entries", result.toc.len());
//! println!("Total lines: {}", result.line_count);
//!
//! for diagnostic in &result.diagnostics {
//!     match diagnostic.severity {
//!         blz_core::DiagnosticSeverity::Warn => {
//!             println!("Warning: {}", diagnostic.message);
//!         }
//!         blz_core::DiagnosticSeverity::Error => {
//!             println!("Error: {}", diagnostic.message);
//!         }
//!         blz_core::DiagnosticSeverity::Info => {
//!             println!("Info: {}", diagnostic.message);
//!         }
//!     }
//! }
//! # Ok::<(), blz_core::Error>(())
//! ```
//!
//! ### Working with structured results:
//!
//! ```rust
//! use blz_core::{MarkdownParser, Result};
//!
//! let mut parser = MarkdownParser::new()?;
//! let result = parser.parse("# Main\n\nMain content\n\n## Sub\n\nSub content here.")?;
//!
//! // Examine heading blocks
//! for block in &result.heading_blocks {
//!     println!("Section: {} (lines {}-{})",
//!         block.path.join(" > "),
//!         block.start_line,
//!         block.end_line);
//! }
//!
//! // Examine table of contents
//! fn print_toc(entries: &[blz_core::TocEntry], indent: usize) {
//!     for entry in entries {
//!         println!("{}{} ({})",
//!             "  ".repeat(indent),
//!             entry.heading_path.last().unwrap_or(&"Unknown".to_string()),
//!             entry.lines);
//!         print_toc(&entry.children, indent + 1);
//!     }
//! }
//! print_toc(&result.toc, 0);
//! # Ok::<(), blz_core::Error>(())
//! ```
//!
//! ## Performance Characteristics
//!
//! - **Parse Time**: < 150ms per MB of markdown content
//! - **Memory Usage**: ~2x source document size during parsing
//! - **Large Documents**: Efficiently handles documents up to 100MB
//! - **Complex Structure**: Handles deeply nested headings (tested up to 50 levels)
//!
//! ## Error Handling
//!
//! The parser is designed to be resilient to malformed input:
//!
//! - **Syntax Errors**: tree-sitter handles most malformed markdown gracefully
//! - **Missing Headings**: Creates a default "Document" block for content without structure
//! - **Encoding Issues**: Handles various text encodings and invalid UTF-8 sequences
//! - **Memory Limits**: Prevents excessive memory usage on pathological inputs
//!
//! ## Thread Safety
//!
//! `MarkdownParser` is **not** thread-safe due to internal mutable state in tree-sitter.
//! Create separate parser instances for concurrent parsing:
//!
//! ```rust
//! use blz_core::{MarkdownParser, Result};
//! use std::thread;
//!
//! fn parse_concurrently(documents: Vec<String>) -> Vec<Result<blz_core::ParseResult>> {
//!     documents
//!         .into_iter()
//!         .map(|doc| {
//!             thread::spawn(move || {
//!                 let mut parser = MarkdownParser::new()?;
//!                 parser.parse(&doc)
//!             })
//!         })
//!         .collect::<Vec<_>>()
//!         .into_iter()
//!         .map(|handle| handle.join().unwrap())
//!         .collect()
//! }
//! ```

use crate::{
    Diagnostic, DiagnosticSeverity, Error, HeadingBlock, Result, TocEntry, heading::path_variants,
};
use base64::{Engine, engine::general_purpose::STANDARD as B64};
use sha2::{Digest, Sha256};
/// Lines per window used when falling back to windowed segmentation
const FALLBACK_WINDOW_LINES: usize = 200;
use std::collections::VecDeque;
use tree_sitter::{Node, Parser, TreeCursor};

/// A tree-sitter based markdown parser.
///
/// Provides structured parsing of markdown documents with heading hierarchy extraction,
/// content block identification, and diagnostic reporting. The parser is designed to be
/// resilient to malformed input while providing detailed structural information.
///
/// ## Parsing Strategy
///
/// The parser uses tree-sitter's markdown grammar to:
/// 1. Build a complete syntax tree of the document
/// 2. Walk the tree to identify heading nodes and their levels  
/// 3. Extract content blocks between headings
/// 4. Build hierarchical table of contents structure
/// 5. Generate diagnostics for quality issues
///
/// ## Reusability
///
/// Parser instances can be reused for multiple documents, but are not thread-safe.
/// The internal tree-sitter parser maintains mutable state across parse operations.
///
/// ## Memory Management
///
/// The parser automatically manages memory for syntax trees and intermediate structures.
/// Large documents may temporarily use significant memory during parsing, but this is
/// released after the `parse()` method returns.
pub struct MarkdownParser {
    /// The underlying tree-sitter parser instance.
    ///
    /// Configured specifically for markdown parsing with the tree-sitter-md grammar.
    /// This parser maintains internal state and is not thread-safe.
    parser: Parser,
}

impl MarkdownParser {
    /// Create a new markdown parser instance.
    ///
    /// Initializes the tree-sitter parser with the markdown grammar. This operation
    /// may fail if the tree-sitter language cannot be loaded properly.
    ///
    /// # Returns
    ///
    /// Returns a new parser instance ready for use.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The tree-sitter markdown language cannot be loaded
    /// - The parser cannot be initialized with the markdown grammar
    /// - System resources are insufficient for parser creation
    ///
    /// # Examples
    ///
    /// ```rust
    /// use blz_core::{MarkdownParser, Result};
    ///
    /// // Create a new parser
    /// let mut parser = MarkdownParser::new()?;
    ///
    /// // Parser is now ready to parse markdown content
    /// let result = parser.parse("# Hello World\n\nContent here.")?;
    /// assert!(!result.heading_blocks.is_empty());
    /// # Ok::<(), blz_core::Error>(())
    /// ```
    ///
    /// ## Resource Usage
    ///
    /// Creating a parser allocates approximately 1-2MB of memory for the grammar
    /// and internal structures. This overhead is amortized across multiple parse
    /// operations.
    pub fn new() -> Result<Self> {
        let mut parser = Parser::new();
        parser
            .set_language(&tree_sitter_md::LANGUAGE.into())
            .map_err(|e| Error::Parse(format!("Failed to set language: {e}")))?;

        Ok(Self { parser })
    }

    /// Parse markdown text into structured components.
    ///
    /// Performs complete analysis of the markdown document, extracting heading hierarchy,
    /// content blocks, table of contents, and generating diagnostics for any issues found.
    ///
    /// # Arguments
    ///
    /// * `text` - The markdown content to parse (UTF-8 string)
    ///
    /// # Returns
    ///
    /// Returns a [`ParseResult`] containing:
    /// - Structured heading blocks with content and line ranges
    /// - Hierarchical table of contents
    /// - Diagnostic messages for any issues found
    /// - Line count and other metadata
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The text cannot be parsed by tree-sitter (very rare)
    /// - Memory is exhausted during parsing of extremely large documents
    /// - Internal parsing structures cannot be built
    ///
    /// Note: Most malformed markdown will not cause errors but will generate diagnostics.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use blz_core::{MarkdownParser, Result};
    ///
    /// let mut parser = MarkdownParser::new()?;
    ///
    /// // Parse simple markdown
    /// let result = parser.parse(r#"
    /// # Introduction
    ///
    /// This is an introduction section.
    ///
    /// ## Getting Started
    ///
    /// Here's how to get started:
    ///
    /// 1. First step
    /// 2. Second step
    ///
    /// ### Prerequisites
    ///
    /// You'll need these tools.
    /// "#)?;
    ///
    /// // Check the results
    /// // The parser creates one block per heading with content until the next heading
    /// assert!(result.heading_blocks.len() >= 2); // At least Introduction and Getting Started
    /// assert!(!result.toc.is_empty());
    /// // Line count represents total lines in the document
    /// assert!(result.line_count > 0);
    ///
    /// // Look for any parsing issues
    /// for diagnostic in &result.diagnostics {
    ///     println!("{:?}: {}", diagnostic.severity, diagnostic.message);
    /// }
    /// # Ok::<(), blz_core::Error>(())
    /// ```
    ///
    /// ## Performance Guidelines
    ///
    /// - Documents up to 1MB: Parse in under 50ms
    /// - Documents up to 10MB: Parse in under 500ms
    /// - Very large documents: Consider streaming or chunking for better UX
    ///
    /// ## Memory Usage
    ///
    /// Memory usage during parsing is approximately:
    /// - Small documents (< 100KB): ~2x document size
    /// - Large documents (> 1MB): ~1.5x document size  
    /// - Peak usage occurs during tree traversal and structure building
    pub fn parse(&mut self, text: &str) -> Result<ParseResult> {
        let tree = self
            .parser
            .parse(text, None)
            .ok_or_else(|| Error::Parse("Failed to parse markdown".into()))?;

        let root = tree.root_node();
        let mut diagnostics = Vec::new();
        let mut heading_blocks = Vec::new();
        let mut toc = Vec::new();

        if root.has_error() {
            diagnostics.push(Diagnostic {
                severity: DiagnosticSeverity::Warn,
                message: "Parse tree contains errors, using fallback parsing".into(),
                line: None,
            });
        }

        let mut cursor = root.walk();
        Self::extract_headings(&mut cursor, text, &mut heading_blocks, &mut toc);

        if heading_blocks.is_empty() {
            diagnostics.push(Diagnostic {
                severity: DiagnosticSeverity::Warn,
                message: "No headings found in document".into(),
                line: Some(1),
            });

            // Hybrid fallback: windowed segmentation for unstructured content
            // Splits the document into fixed-size windows to improve search fidelity
            let total_lines = text.lines().count();
            if total_lines <= FALLBACK_WINDOW_LINES {
                let path = vec!["Document".into()];
                let variants = path_variants(&path);
                heading_blocks.push(HeadingBlock {
                    path,
                    display_path: variants.display_segments,
                    normalized_tokens: variants.tokens,
                    content: text.to_string(),
                    start_line: 1,
                    end_line: total_lines,
                });
            } else {
                let mut start = 1usize;
                let mut current = String::new();
                let mut count = 0usize;
                for line in text.lines() {
                    if count > 0 {
                        current.push('\n');
                    }
                    current.push_str(line);
                    count += 1;
                    if count == FALLBACK_WINDOW_LINES {
                        let end_line = start + count - 1;
                        let path = vec!["Document".into()];
                        let variants = path_variants(&path);
                        heading_blocks.push(HeadingBlock {
                            path,
                            display_path: variants.display_segments,
                            normalized_tokens: variants.tokens,
                            content: std::mem::take(&mut current),
                            start_line: start,
                            end_line,
                        });
                        start = end_line + 1;
                        count = 0;
                    }
                }
                if !current.is_empty() {
                    let end_line = start + count - 1;
                    let path = vec!["Document".into()];
                    let variants = path_variants(&path);
                    heading_blocks.push(HeadingBlock {
                        path,
                        display_path: variants.display_segments,
                        normalized_tokens: variants.tokens,
                        content: current,
                        start_line: start,
                        end_line,
                    });
                }
            }
        }

        let line_count = text.lines().count();

        Ok(ParseResult {
            heading_blocks,
            toc,
            diagnostics,
            line_count,
        })
    }

    fn extract_headings(
        cursor: &mut TreeCursor,
        text: &str,
        blocks: &mut Vec<HeadingBlock>,
        toc: &mut Vec<TocEntry>,
    ) {
        // Collect all heading information first
        #[derive(Debug)]
        struct HeadingInfo {
            level: usize,
            text: String,
            byte_start: usize,
            line_start: usize,
        }

        let mut headings = Vec::new();

        // First pass: collect all headings with their positions
        Self::walk_tree(cursor, text, |node| {
            if node.kind() == "atx_heading" {
                let level = Self::get_heading_level(node, text);
                let heading_text = Self::get_heading_text(node, text);
                let line_start = node.start_position().row;

                headings.push(HeadingInfo {
                    level,
                    text: heading_text,
                    byte_start: node.byte_range().start,
                    line_start,
                });
            }
        });

        // If no headings, create a single document block
        if headings.is_empty() {
            return;
        }

        // Ensure headings are processed in source order
        headings.sort_by_key(|h| h.byte_start);

        // Second pass: build blocks by slicing between headings
        let mut current_path = Vec::new();
        let mut stack: VecDeque<usize> = VecDeque::new();
        let mut baseline_level: Option<usize> = None;

        for i in 0..headings.len() {
            let heading = &headings[i];

            // Update path based on heading level
            let trimmed = heading.text.trim();
            if heading.level == 1 && trimmed.starts_with("404") {
                // Skip placeholder 404 pages so they do not capture subsequent sections.
                current_path.clear();
                stack.clear();
                continue;
            }

            if baseline_level.is_none_or(|level| heading.level < level) {
                baseline_level = Some(heading.level);
            }
            let baseline = baseline_level.unwrap_or(1);
            let effective_level = heading
                .level
                .saturating_sub(baseline.saturating_sub(1))
                .max(1);

            while stack.len() >= effective_level {
                stack.pop_back();
                current_path.pop();
            }
            current_path.push(heading.text.clone());
            stack.push_back(effective_level);

            // Determine content range
            let content_start = heading.byte_start;
            let content_end = if i + 1 < headings.len() {
                headings[i + 1].byte_start
            } else {
                text.len()
            };

            // Extract content slice
            let content = &text[content_start..content_end];

            // Calculate line numbers
            let start_line = heading.line_start + 1; // 1-based
            let end_line = if i + 1 < headings.len() {
                headings[i + 1].line_start // End at the line before next heading
            } else {
                text.lines().count()
            };

            let variants = path_variants(&current_path);
            let display_path = variants.display_segments.clone();
            let normalized_segments = variants.normalized_segments.clone();
            let normalized_tokens = variants.tokens.clone();

            // Create heading block
            blocks.push(HeadingBlock {
                path: current_path.clone(),
                display_path: display_path.clone(),
                normalized_tokens: normalized_tokens.clone(),
                content: content.to_string(),
                start_line,
                end_line,
            });

            // Compute stable content anchor for remapping across updates
            let anchor = Some(Self::compute_anchor(&current_path, &heading.text, content));

            // Create TOC entry
            let entry = TocEntry {
                heading_path: current_path.clone(),
                heading_path_display: Some(display_path),
                heading_path_normalized: Some(normalized_segments),
                lines: if end_line > start_line {
                    format!("{start_line}-{end_line}")
                } else {
                    format!("{start_line}")
                },
                anchor,
                children: Vec::new(),
            };

            Self::add_to_toc(toc, entry, stack.len());
        }
    }

    fn compute_anchor(_path: &[String], heading_text: &str, _content: &str) -> String {
        let mut hasher = Sha256::new();
        // Normalize heading only for a stable, move-invariant anchor
        hasher.update(heading_text.trim().to_lowercase().as_bytes());
        let digest = hasher.finalize();
        let full = B64.encode(digest);
        // Truncate for brevity while remaining collision-resistant
        full[..22.min(full.len())].to_string()
    }

    fn walk_tree<F>(cursor: &mut TreeCursor, _text: &str, mut callback: F)
    where
        F: FnMut(Node),
    {
        loop {
            let node = cursor.node();
            callback(node);

            if cursor.goto_first_child() {
                continue;
            }

            if cursor.goto_next_sibling() {
                continue;
            }

            loop {
                if !cursor.goto_parent() {
                    return;
                }
                if cursor.goto_next_sibling() {
                    break;
                }
            }
        }
    }

    fn get_heading_level(node: Node, _text: &str) -> usize {
        for child in node.children(&mut node.walk()) {
            if child.kind() == "atx_h1_marker" {
                return 1;
            } else if child.kind() == "atx_h2_marker" {
                return 2;
            } else if child.kind() == "atx_h3_marker" {
                return 3;
            } else if child.kind() == "atx_h4_marker" {
                return 4;
            } else if child.kind() == "atx_h5_marker" {
                return 5;
            } else if child.kind() == "atx_h6_marker" {
                return 6;
            }
        }
        1
    }

    fn get_heading_text(node: Node, text: &str) -> String {
        for child in node.children(&mut node.walk()) {
            if child.kind().contains("heading") && child.kind().contains("content") {
                return text[child.byte_range()].trim().to_string();
            }
        }

        let full_text = &text[node.byte_range()];
        full_text.trim_start_matches('#').trim().to_string()
    }

    fn add_to_toc(toc: &mut Vec<TocEntry>, entry: TocEntry, depth: usize) {
        if depth == 1 {
            toc.push(entry);
        } else if let Some(parent) = toc.last_mut() {
            Self::add_to_toc_recursive(&mut parent.children, entry, depth - 1);
        }
    }

    fn add_to_toc_recursive(toc: &mut Vec<TocEntry>, entry: TocEntry, depth: usize) {
        if depth == 1 {
            toc.push(entry);
        } else if let Some(parent) = toc.last_mut() {
            Self::add_to_toc_recursive(&mut parent.children, entry, depth - 1);
        }
    }
}

/// The result of parsing a markdown document.
///
/// Contains all structured information extracted from the markdown, including heading
/// hierarchy, content blocks, table of contents, and any diagnostic messages generated
/// during parsing.
///
/// ## Usage Patterns
///
/// The parse result provides multiple ways to access the document structure:
///
/// - **Heading Blocks**: For content indexing and search
/// - **Table of Contents**: For navigation and structure display
/// - **Diagnostics**: For quality assurance and debugging
/// - **Line Count**: For validation and progress reporting
///
/// ## Examples
///
/// ### Processing heading blocks:
///
/// ```rust
/// use blz_core::{MarkdownParser, Result};
///
/// let mut parser = MarkdownParser::new()?;
/// let result = parser.parse("# Title\n\nContent\n\n## Subtitle\n\nMore content")?;
///
/// for block in &result.heading_blocks {
///     println!("Section: {}", block.path.join(" > "));
///     println!("  Lines {}-{}", block.start_line, block.end_line);
///     println!("  Content: {} chars", block.content.len());
/// }
/// # Ok::<(), blz_core::Error>(())
/// ```
///
/// ### Generating navigation from TOC:
///
/// ```rust
/// use blz_core::{MarkdownParser, TocEntry, Result};
///
/// fn generate_nav(entries: &[TocEntry], depth: usize) -> String {
///     entries
///         .iter()
///         .map(|entry| {
///             let indent = "  ".repeat(depth);
///             let default = "Untitled".to_string();
///             let title = entry.heading_path.last().unwrap_or(&default);
///             format!("{}* {} ({})\n{}",
///                 indent,
///                 title,
///                 entry.lines,
///                 generate_nav(&entry.children, depth + 1)
///             )
///         })
///         .collect()
/// }
///
/// let mut parser = MarkdownParser::new()?;
/// let result = parser.parse("# A\n\nContent A\n\n## A.1\n\nContent A.1\n\n### A.1.1\n\nContent A.1.1\n\n## A.2\n\nContent A.2")?;
/// let nav = generate_nav(&result.toc, 0);
/// println!("Navigation:\n{}", nav);
/// # Ok::<(), blz_core::Error>(())
/// ```
#[derive(Clone)]
pub struct ParseResult {
    /// Structured heading blocks extracted from the document.
    ///
    /// Each block represents a section of content under a specific heading hierarchy.
    /// Blocks are ordered by their appearance in the document and contain both the
    /// heading path and all content until the next same-level or higher-level heading.
    ///
    /// ## Content Organization
    ///
    /// - Content includes the heading itself and all text below it
    /// - Text continues until the next same-level or higher-level heading
    /// - Nested headings create separate blocks with extended paths
    /// - Documents without headings get a single "Document" block
    pub heading_blocks: Vec<HeadingBlock>,

    /// Hierarchical table of contents extracted from headings.
    ///
    /// Provides a nested structure that mirrors the heading hierarchy in the document.
    /// Each entry contains the full heading path and line range information.
    ///
    /// ## Structure
    ///
    /// - Top-level entries correspond to H1 headings
    /// - Child entries represent nested headings (H2, H3, etc.)
    /// - Empty when no headings are present in the document
    /// - Line ranges are 1-based and use "start-end" format
    pub toc: Vec<TocEntry>,

    /// Diagnostic messages generated during parsing.
    ///
    /// Contains warnings, errors, and informational messages about issues found
    /// during parsing. These help identify quality problems or processing decisions
    /// that users should be aware of.
    ///
    /// ## Common Diagnostics
    ///
    /// - Missing headings (document has content but no structure)
    /// - Parse tree errors (tree-sitter detected syntax issues)
    /// - Encoding problems (invalid UTF-8 sequences)
    /// - Structure warnings (very deep nesting, empty sections)
    pub diagnostics: Vec<Diagnostic>,

    /// Total number of lines in the source document.
    ///
    /// Used for validation, progress reporting, and ensuring line ranges in
    /// heading blocks and TOC entries are within bounds. This count includes
    /// empty lines and uses the same line numbering as other components (1-based).
    pub line_count: usize,
}

// Note: Default is not implemented as MarkdownParser::new() can fail.
// Use MarkdownParser::new() directly and handle the Result.

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::unnecessary_wraps,
    clippy::format_push_string,
    clippy::disallowed_macros
)]
mod tests {
    use super::*;
    use proptest::prelude::*;

    // Test fixtures and builders
    fn create_test_parser() -> MarkdownParser {
        MarkdownParser::new().expect("Failed to create parser")
    }

    #[test]
    fn test_anchor_stability_when_section_moves() {
        let mut parser = create_test_parser();

        let doc_v1 = "# Intro\n\nPrelude.\n\n## Section A\n\nAlpha content line 1.\nAlpha content line 2.\n\n## Section B\n\nBeta content.\n";

        let result_v1 = parser.parse(doc_v1).expect("parse v1");
        #[allow(clippy::items_after_statements)]
        fn find<'a>(entries: &'a [TocEntry], name: &str) -> Option<&'a TocEntry> {
            for e in entries {
                if e.heading_path.last().is_some_and(|h| h == name) {
                    return Some(e);
                }
                if let Some(found) = find(&e.children, name) {
                    return Some(found);
                }
            }
            None
        }
        let a_v1 = find(&result_v1.toc, "Section A").expect("section A in v1");
        let anchor_v1 = a_v1.anchor.clone().expect("anchor v1");
        let lines_v1 = a_v1.lines.clone();

        // Move Section A below B
        let doc_v2 = "# Intro\n\nPrelude.\n\n## Section B\n\nBeta content.\n\n## Section A\n\nAlpha content line 1.\nAlpha content line 2.\n";
        let result_v2 = parser.parse(doc_v2).expect("parse v2");
        let a_v2 = find(&result_v2.toc, "Section A").expect("section A in v2");
        let anchor_v2 = a_v2.anchor.clone().expect("anchor v2");
        let lines_v2 = a_v2.lines.clone();

        // Anchor should be stable even if lines changed
        assert_eq!(anchor_v1, anchor_v2, "anchor stable across moves");
        assert_ne!(lines_v1, lines_v2, "lines should reflect new position");
    }

    #[test]
    fn test_skips_placeholder_404_headings() -> Result<()> {
        let mut parser = create_test_parser();

        let doc = r"# 404

Check the URL.

## Actual Section

Real content lives here.

### Nested Detail

Additional context.

## Follow Up

More guidance.
";

        let result = parser.parse(doc)?;

        assert_eq!(
            result.toc.len(),
            2,
            "top-level entries should ignore 404 headings"
        );
        assert!(
            result.toc.iter().all(|entry| entry
                .heading_path
                .iter()
                .all(|component| !component.starts_with("404"))),
            "toc should not contain placeholder 404 entries"
        );
        assert_eq!(
            result.heading_blocks.len(),
            3,
            "children under 404 should remain accessible"
        );
        assert_eq!(result.heading_blocks[0].path[0], "Actual Section");

        Ok(())
    }

    fn simple_markdown() -> &'static str {
        r"# Main Heading

This is some content under the main heading.

## Sub Heading

More content here.

### Deep Heading

Even deeper content.

## Another Sub

Final content.
"
    }

    fn complex_markdown() -> &'static str {
        r#"# Getting Started

Welcome to our documentation!

## Installation

Run the following command:

```bash
npm install
```

### Requirements

- Node.js 16+
- npm 7+

## Usage

Here's how to use it:

1. First step
2. Second step

### Advanced Usage

For advanced users:

#### Configuration

Edit the config file:

```json
{
    "key": "value"
}
```

## Troubleshooting

Common issues:

- Issue 1
- Issue 2
"#
    }

    fn malformed_markdown() -> &'static str {
        r"# Broken Heading
## Missing content

### Unmatched brackets ][

Content with `unclosed code

> Broken quote
>> Nested broken quote

* List item
  * Nested without proper spacing
* Another item

```
Unclosed code block
"
    }

    #[test]
    fn test_parser_creation() {
        // Given: Creating a new parser
        // When: Parser is created
        let result = MarkdownParser::new();

        // Then: Should succeed
        assert!(result.is_ok());
    }

    #[test]
    fn test_parse_simple_markdown() -> Result<()> {
        // Given: Simple markdown with basic headings
        let mut parser = create_test_parser();
        let markdown = simple_markdown();

        // When: Parsing the markdown
        let result = parser.parse(markdown)?;

        // Then: Should extract headings and create TOC
        assert!(!result.heading_blocks.is_empty());
        assert!(!result.toc.is_empty());
        assert_eq!(result.line_count, markdown.lines().count());

        // Verify main heading is found
        let main_heading = result
            .heading_blocks
            .iter()
            .find(|block| block.path.contains(&"Main Heading".to_string()));
        assert!(main_heading.is_some());

        // Verify sub heading is found
        let sub_heading = result
            .heading_blocks
            .iter()
            .find(|block| block.path.contains(&"Sub Heading".to_string()));
        assert!(sub_heading.is_some());

        Ok(())
    }

    #[test]
    fn test_parse_complex_markdown_structure() -> Result<()> {
        // Given: Complex markdown with nested headings
        let mut parser = create_test_parser();
        let markdown = complex_markdown();

        // When: Parsing the markdown
        let result = parser.parse(markdown)?;

        // Then: Should handle nested structure correctly
        assert!(result.heading_blocks.len() >= 5); // Multiple headings

        // Check for specific headings at different levels
        let headings: Vec<_> = result
            .heading_blocks
            .iter()
            .flat_map(|block| &block.path)
            .collect();

        assert!(headings.iter().any(|h| h.contains("Getting Started")));
        assert!(headings.iter().any(|h| h.contains("Installation")));
        assert!(headings.iter().any(|h| h.contains("Requirements")));
        assert!(headings.iter().any(|h| h.contains("Configuration")));

        // Verify TOC structure
        assert!(!result.toc.is_empty());
        let top_level = &result.toc[0];
        assert!(
            top_level
                .heading_path
                .contains(&"Getting Started".to_string())
        );

        Ok(())
    }

    #[test]
    fn test_parse_malformed_markdown() -> Result<()> {
        // Given: Malformed markdown with various issues
        let mut parser = create_test_parser();
        let markdown = malformed_markdown();

        // When: Parsing the malformed markdown
        let result = parser.parse(markdown)?;

        // Then: Should handle errors gracefully with diagnostics
        assert!(!result.heading_blocks.is_empty()); // Should still extract some headings

        // Should have diagnostics about parsing issues if tree-sitter detected errors
        // Note: tree-sitter is quite robust, so it may not always generate errors

        Ok(())
    }

    #[test]
    fn test_parse_empty_document() -> Result<()> {
        // Given: Empty document
        let mut parser = create_test_parser();
        let empty = "";

        // When: Parsing empty document
        let result = parser.parse(empty)?;

        // Then: Should handle gracefully
        assert_eq!(result.line_count, 0);
        assert!(result.heading_blocks.len() <= 1); // May have default "Document" block
        assert!(
            result
                .diagnostics
                .iter()
                .any(|d| d.message.contains("No headings found")
                    || d.severity == DiagnosticSeverity::Warn)
        );

        Ok(())
    }

    #[test]
    fn test_parse_document_without_headings() -> Result<()> {
        // Given: Document with content but no headings
        let mut parser = create_test_parser();
        let no_headings = r"This is just plain text.

With multiple paragraphs.

And some more content.

But no headings at all.
";

        // When: Parsing document without headings
        let result = parser.parse(no_headings)?;

        // Then: Should create default document block
        assert_eq!(result.heading_blocks.len(), 1);
        let block = &result.heading_blocks[0];
        assert_eq!(block.path, vec!["Document".to_string()]);
        assert_eq!(block.content.trim(), no_headings.trim());

        // Should have diagnostic warning
        assert!(
            result
                .diagnostics
                .iter()
                .any(|d| d.message.contains("No headings found"))
        );

        Ok(())
    }

    #[test]
    fn test_windowed_segmentation_for_large_unstructured() -> Result<()> {
        // Given: Unstructured content larger than fallback window size
        let mut parser = create_test_parser();
        let total = FALLBACK_WINDOW_LINES * 2 + 25; // two full windows + remainder
        let doc = (1..=total)
            .map(|i| format!("line {i}"))
            .collect::<Vec<_>>()
            .join("\n");

        // When: Parsing the unstructured document
        let result = parser.parse(&doc)?;

        // Then: Should split into windows of size FALLBACK_WINDOW_LINES
        assert_eq!(result.heading_blocks.len(), 3);
        for b in &result.heading_blocks {
            assert_eq!(b.path, vec!["Document".to_string()]);
            assert!(b.start_line >= 1);
            assert!(b.end_line <= total);
        }
        assert_eq!(result.heading_blocks.last().unwrap().end_line, total);

        Ok(())
    }

    #[test]
    fn test_heading_level_detection() -> Result<()> {
        // Given: Markdown with various heading levels
        let mut parser = create_test_parser();
        let multilevel = r"# Level 1

## Level 2

### Level 3

#### Level 4

##### Level 5

###### Level 6
";

        // When: Parsing multilevel headings
        let result = parser.parse(multilevel)?;

        // Then: Should correctly identify all levels
        assert!(result.heading_blocks.len() >= 6);

        // Verify heading paths reflect nesting
        let paths: Vec<_> = result
            .heading_blocks
            .iter()
            .map(|block| block.path.len())
            .collect();

        // Should have headings at different nesting levels
        assert!(paths.contains(&1)); // Level 1
        assert!(paths.contains(&2)); // Level 2
        assert!(paths.iter().any(|&len| len >= 3)); // Deeper levels

        Ok(())
    }

    #[test]
    fn test_heading_text_extraction() -> Result<()> {
        // Given: Headings with various formatting
        let mut parser = create_test_parser();
        let formatted_headings = r"# **Bold Heading**

## _Italic Heading_

### `Code in Heading`

#### Heading with [Link](http://example.com)

##### Heading with **bold** and _italic_
";

        // When: Parsing formatted headings
        let result = parser.parse(formatted_headings)?;

        // Then: Should extract clean heading text
        let heading_texts: Vec<_> = result
            .heading_blocks
            .iter()
            .flat_map(|block| &block.path)
            .collect();

        // Should contain expected heading text (formatting may be preserved or stripped)
        assert!(heading_texts.iter().any(|h| h.contains("Bold Heading")));
        assert!(heading_texts.iter().any(|h| h.contains("Italic Heading")));
        assert!(heading_texts.iter().any(|h| h.contains("Code in Heading")));

        Ok(())
    }

    #[test]
    fn test_content_extraction() -> Result<()> {
        // Given: Markdown with content under headings
        let mut parser = create_test_parser();
        let content_markdown = r"# Section A

This is content for section A.
It spans multiple lines.

## Subsection A1

More specific content here.

# Section B

Different content for section B.
";

        // When: Parsing markdown
        let result = parser.parse(content_markdown)?;

        // Then: Should extract content correctly
        let section_a = result
            .heading_blocks
            .iter()
            .find(|block| block.path.contains(&"Section A".to_string()))
            .expect("Section A should be found");

        assert!(section_a.content.contains("This is content for section A"));
        assert!(section_a.content.contains("multiple lines"));

        let section_b = result
            .heading_blocks
            .iter()
            .find(|block| block.path.contains(&"Section B".to_string()))
            .expect("Section B should be found");

        assert!(
            section_b
                .content
                .contains("Different content for section B")
        );

        Ok(())
    }

    #[test]
    fn test_line_number_tracking() -> Result<()> {
        // Given: Markdown with known line structure
        let mut parser = create_test_parser();
        let numbered_content =
            "Line 1\n# Heading at line 2\nLine 3\nLine 4\n## Sub at line 5\nLine 6";

        // When: Parsing markdown
        let result = parser.parse(numbered_content)?;

        // Then: Should track line numbers correctly
        assert_eq!(result.line_count, 6);

        // Find the heading block and verify line numbers
        let heading_block = result
            .heading_blocks
            .iter()
            .find(|block| block.path.contains(&"Heading at line 2".to_string()));

        if let Some(block) = heading_block {
            // Line numbers are 1-based
            assert!(block.start_line >= 1);
            assert!(block.end_line <= result.line_count);
            assert!(block.start_line <= block.end_line);
        }

        Ok(())
    }

    #[test]
    fn test_toc_generation() -> Result<()> {
        // Given: Hierarchical markdown
        let mut parser = create_test_parser();
        let hierarchical = r"# Top Level

## First Sub
### Deep Sub 1
### Deep Sub 2

## Second Sub
### Another Deep
#### Very Deep

# Another Top
";

        // When: Parsing hierarchical markdown
        let result = parser.parse(hierarchical)?;

        // Then: Should generate proper TOC structure
        assert!(!result.toc.is_empty());

        // Should have top-level entries
        assert!(!result.toc.is_empty());

        // Check first top-level entry
        let first_top = &result.toc[0];
        assert!(first_top.heading_path.contains(&"Top Level".to_string()));

        // Should have children
        if !first_top.children.is_empty() {
            let first_sub = &first_top.children[0];
            assert!(first_sub.heading_path.len() >= 2); // Nested path
        }

        Ok(())
    }

    // Property-based tests
    proptest! {
        // Use more constrained inputs to avoid tree-sitter segfaults in CI
        // Tree-sitter can crash on certain malformed inputs, particularly with
        // arbitrary binary data or extreme edge cases. We still get good coverage
        // with ASCII-only content.
        #[test]
        fn test_parser_never_panics_on_arbitrary_input(
            content in prop::string::string_regex("[\\x20-\\x7E\\n\\r\\t]{0,500}").unwrap()
        ) {
            let mut parser = create_test_parser();

            // Should never panic, even with malformed input
            let result = parser.parse(&content);

            // Either succeeds or fails gracefully
            if let Ok(parse_result) = result {
                prop_assert!(parse_result.line_count == content.lines().count());
                prop_assert!(!parse_result.heading_blocks.is_empty()); // Always has at least default
            } else {
                // Graceful failure is acceptable
            }
        }

        #[test]
        fn test_line_count_accuracy(
            lines in prop::collection::vec(
                prop::string::string_regex("[\\x20-\\x7E]{0,100}").unwrap(),
                0..50
            )
        ) {
            let content = lines.join("\n");
            let mut parser = create_test_parser();
            // The actual line count is determined by the content, not the vector length
            // An empty string has 0 lines, non-empty content has at least 1 line
            let expected_lines = if content.is_empty() {
                0
            } else {
                // Count actual lines in the joined content
                // A non-empty string has at least 1 line, plus count of newlines
                content.lines().count()
            };

            if let Ok(result) = parser.parse(&content) {
                prop_assert_eq!(result.line_count, expected_lines);
            }
        }

        #[test]
        fn test_single_heading_parsing(heading_text in r"[a-zA-Z][a-zA-Z0-9 ]{2,30}") {
            let mut parser = create_test_parser();
            let markdown = format!("# {heading_text}");

            // Only test if heading text has actual content after trimming
            let trimmed = heading_text.trim();
            if trimmed.is_empty() || trimmed.len() < 2 {
                // Skip very short or empty headings as they may not parse reliably
                return Ok(());
            }

            if let Ok(result) = parser.parse(&markdown) {
                // Parser should always return at least one heading block (default "Document")
                prop_assert!(!result.heading_blocks.is_empty());

                // TOC generation depends on successful parsing - not all inputs may generate TOC
                if !result.toc.is_empty() {
                    let has_heading = result.heading_blocks.iter()
                        .any(|block| block.path.iter().any(|p| p.contains(trimmed)));
                    prop_assert!(has_heading);
                }
            }
        }

        #[test]
        fn test_heading_level_detection_consistency(
            levels in prop::collection::vec(1u8..=6, 1..10)
        ) {
            let mut parser = create_test_parser();

            // Generate markdown with specified heading levels
            let mut markdown = String::new();
            let mut expected_path_lens = Vec::new();

            for (i, level) in levels.iter().enumerate() {
                let heading_text = format!("Heading {}", i + 1);
                let heading_line = format!("{} {}\n\nContent for heading {}\n\n",
                                         "#".repeat(*level as usize),
                                         heading_text,
                                         i + 1);
                markdown.push_str(&heading_line);
                expected_path_lens.push(*level as usize);
            }

            if let Ok(result) = parser.parse(&markdown) {
                // Should have appropriate number of heading blocks
                prop_assert!(result.heading_blocks.len() >= levels.len().min(1));

                // Each heading should create appropriate nesting
                for (i, expected_depth) in expected_path_lens.iter().enumerate() {
                    if i < result.heading_blocks.len() {
                        let actual_depth = result.heading_blocks[i].path.len();
                        // Depth should be reasonable (may not exactly match due to nesting rules)
                        prop_assert!(actual_depth <= *expected_depth);
                        prop_assert!(actual_depth >= 1);
                    }
                }
            }
        }

        #[test]
        fn test_unicode_content_preservation(
            content in r"[\u{0080}-\u{FFFF}]{1,100}"
        ) {
            let mut parser = create_test_parser();
            let markdown = format!("# Unicode Test\n\n{content}");

            if let Ok(result) = parser.parse(&markdown) {
                // Unicode content should be preserved in heading blocks
                let has_unicode = result.heading_blocks.iter()
                    .any(|block| block.content.contains(&content));
                prop_assert!(has_unicode, "Unicode content should be preserved");

                // Line count should be accurate
                prop_assert_eq!(result.line_count, markdown.lines().count());
            }
        }

        #[test]
        fn test_mixed_line_endings(
            line_ending in prop_oneof![Just("\n"), Just("\r\n"), Just("\r")]
        ) {
            let mut parser = create_test_parser();
            let content_lines = ["# Main Heading",
                "",
                "This is content.",
                "",
                "## Sub Heading",
                "",
                "More content here."];

            let markdown = content_lines.join(line_ending);

            if let Ok(result) = parser.parse(&markdown) {
                // Should parse regardless of line ending style
                prop_assert!(!result.heading_blocks.is_empty());

                // Should find both headings
                let main_heading = result.heading_blocks.iter()
                    .any(|block| block.path.iter().any(|p| p.contains("Main Heading")));
                let sub_heading = result.heading_blocks.iter()
                    .any(|block| block.path.iter().any(|p| p.contains("Sub Heading")));

                prop_assert!(main_heading || sub_heading, "Should find at least one heading");
            }
        }

        #[test]
        fn test_deeply_nested_structure(depth in 1usize..20) {
            let mut parser = create_test_parser();
            let mut markdown = String::new();

            // Create deeply nested heading structure
            for level in 1..=depth.min(6) {
                let heading = format!("{} Level {} Heading\n\nContent at level {}.\n\n",
                                    "#".repeat(level), level, level);
                markdown.push_str(&heading);
            }

            if let Ok(result) = parser.parse(&markdown) {
                // Should handle deep nesting gracefully
                prop_assert!(!result.heading_blocks.is_empty());
                prop_assert!(!result.toc.is_empty());

                // Deepest heading should have appropriate path length
                if let Some(deepest) = result.heading_blocks.iter()
                    .max_by_key(|block| block.path.len()) {
                    prop_assert!(deepest.path.len() <= depth.min(6));
                }
            }
        }

        #[test]
        fn test_large_content_blocks(
            block_size in 100usize..5000,
            num_blocks in 1usize..10
        ) {
            let mut parser = create_test_parser();
            let mut markdown = String::new();

            for i in 0..num_blocks {
                markdown.push_str(&format!("# Heading {}\n\n", i + 1));

                // Add large content block
                let content_line = format!("This is line {i} of content. ");
                let large_content = content_line.repeat(block_size / content_line.len());
                markdown.push_str(&large_content);
                markdown.push_str("\n\n");
            }

            if let Ok(result) = parser.parse(&markdown) {
                // Should handle large content efficiently
                prop_assert_eq!(result.heading_blocks.len(), num_blocks);

                // Each block should have substantial content
                for block in &result.heading_blocks {
                    prop_assert!(block.content.len() > block_size / 2);
                }

                // Line count should be reasonable
                prop_assert!(result.line_count >= num_blocks * 3); // At least heading + 2 content lines per block
            }
        }

        #[test]
        fn test_markdown_syntax_edge_cases(
            syntax_char in prop_oneof![
                Just("*"), Just("_"), Just("`"), Just("~"),
                Just("["), Just("]"), Just("("), Just(")"),
                Just("!"), Just("#"), Just(">"), Just("-"),
                Just("+"), Just("="), Just("|"), Just("\\")
            ]
        ) {
            let mut parser = create_test_parser();

            // Create markdown with potentially problematic syntax
            let markdown = format!(
                "# Test Heading\n\nContent with {syntax_char} special {syntax_char} characters {syntax_char} here.\n\n## Another {syntax_char}\n\nMore {syntax_char} content."
            );

            if let Ok(result) = parser.parse(&markdown) {
                // Should parse without crashing
                prop_assert!(!result.heading_blocks.is_empty());

                // Should preserve the special characters in content
                let has_special_chars = result.heading_blocks.iter()
                    .any(|block| block.content.contains(syntax_char));
                prop_assert!(has_special_chars, "Special characters should be preserved");
            }
        }

        #[test]
        fn test_heading_with_formatting(
            format_type in prop_oneof![
                Just("**bold**"),
                Just("_italic_"),
                Just("`code`"),
                Just("[link](url)"),
                Just("~~strike~~")
            ],
            heading_text in r"[a-zA-Z ]{5,20}"
        ) {
            let mut parser = create_test_parser();
            let formatted_heading = format!("# {heading_text} {format_type}\n\nContent here.");

            if let Ok(result) = parser.parse(&formatted_heading) {
                // Should extract heading text (may or may not preserve formatting)
                prop_assert!(!result.heading_blocks.is_empty());

                let heading_found = result.heading_blocks.iter()
                    .any(|block| block.path.iter()
                        .any(|p| p.contains(heading_text.trim())));
                prop_assert!(heading_found, "Should find heading text");
            }
        }

        #[test]
        fn test_random_whitespace_patterns(
            spaces_before in 0usize..4,  // 4+ spaces makes it a code block
            spaces_after in 0usize..10,
            tabs_mixed in 0usize..5
        ) {
            let mut parser = create_test_parser();

            // Note: In Markdown, tabs or 4+ spaces before # make it a code block
            // We'll only test with valid heading formats
            let whitespace_prefix = " ".repeat(spaces_before);  // No tabs before #
            let whitespace_suffix = format!("{}{}",
                                          " ".repeat(spaces_after),
                                          "\t".repeat(tabs_mixed));

            let markdown = format!("{whitespace_prefix}# Test Heading{whitespace_suffix}\n\nContent here.");

            if let Ok(result) = parser.parse(&markdown) {
                // Should handle whitespace variations gracefully
                // With less than 4 spaces, it should be a valid heading
                prop_assert!(!result.heading_blocks.is_empty());

                // Should find the heading
                let found_heading = result.heading_blocks.iter()
                    .any(|block| block.path.iter()
                        .any(|p| p.contains("Test Heading")));
                prop_assert!(found_heading, "Should find heading with {} spaces before", spaces_before);
            }
        }

        #[test]
        fn test_content_with_code_blocks(
            language in prop_oneof![
                Just("rust"), Just("javascript"), Just("python"),
                Just("bash"), Just("json"), Just("")
            ],
            code_lines in prop::collection::vec(r"[a-zA-Z0-9 ]{0,50}", 1..10)
        ) {
            let mut parser = create_test_parser();

            let code_content = code_lines.join("\n");
            let markdown = format!(
                "# Code Example\n\nHere's some code:\n\n```{language}\n{code_content}\n```\n\n## After Code\n\nMore content."
            );

            if let Ok(result) = parser.parse(&markdown) {
                // Should handle code blocks properly
                prop_assert!(!result.heading_blocks.is_empty());

                // Code content should be preserved in blocks
                let has_code = result.heading_blocks.iter()
                    .any(|block| block.content.contains(&code_content));
                prop_assert!(has_code, "Code content should be preserved");

                // Should find both headings
                let headings: Vec<_> = result.heading_blocks.iter()
                    .flat_map(|block| &block.path)
                    .collect();
                let has_main = headings.iter().any(|h| h.contains("Code Example"));
                let has_after = headings.iter().any(|h| h.contains("After Code"));

                prop_assert!(has_main || has_after, "Should find at least one heading");
            }
        }
    }

    // Security-focused tests
    #[test]
    fn test_parser_handles_malicious_markdown() -> Result<()> {
        // Given: Various potentially malicious markdown inputs
        let malicious_inputs = vec![
            // Very long heading
            format!("# {}", "A".repeat(10000)),
            // Deeply nested structure
            (1..=100)
                .map(|i| format!("{} Level {}", "#".repeat(i % 6 + 1), i))
                .collect::<Vec<_>>()
                .join("\n"),
            // Unicode attacks
            "# \u{202e}reversed\u{202d} heading".to_string(),
            // Control characters
            "# Heading with \x00 null \x01 characters".to_string(),
            // Excessive nesting
            format!(
                "# Top\n{}",
                (2..=50)
                    .map(|i| format!("{} Level {}", "#".repeat(i), i))
                    .collect::<Vec<_>>()
                    .join("\n")
            ),
            // Mixed line endings
            "# Heading 1\r\n## Heading 2\n### Heading 3\r#### Heading 4".to_string(),
        ];

        let mut parser = create_test_parser();

        for malicious_input in malicious_inputs {
            // When: Parsing potentially malicious input
            let result = parser.parse(&malicious_input);

            // Then: Should handle safely without crashing
            if let Ok(parse_result) = result {
                // Should not crash and should produce reasonable output
                assert!(parse_result.line_count <= malicious_input.lines().count() + 1);
                assert!(!parse_result.heading_blocks.is_empty());
            } else {
                // Graceful failure is acceptable for extreme inputs
            }
        }

        Ok(())
    }

    #[test]
    fn test_parser_handles_unicode_content() -> Result<()> {
        // Given: Markdown with various Unicode content
        let unicode_markdown = r"# 日本語のヘッダー

これは日本語のコンテンツです。

## العنوان العربي

محتوى باللغة العربية.

### Заголовок на русском

Русский контент.

#### 🚀 Emoji Header 🎉

Content with emojis: 😀 🎈 🌟

##### Mixed: English 中文 العربية русский
";

        let mut parser = create_test_parser();

        // When: Parsing Unicode markdown
        let result = parser.parse(unicode_markdown)?;

        // Then: Should handle Unicode correctly
        assert!(!result.heading_blocks.is_empty());
        assert!(!result.toc.is_empty());

        // Check that Unicode text is preserved
        let all_paths: Vec<_> = result
            .heading_blocks
            .iter()
            .flat_map(|block| &block.path)
            .collect();

        assert!(all_paths.iter().any(|p| p.contains("日本語")));
        assert!(all_paths.iter().any(|p| p.contains("العربي")));
        assert!(all_paths.iter().any(|p| p.contains("русском")));
        assert!(all_paths.iter().any(|p| p.contains("🚀")));

        Ok(())
    }

    #[test]
    fn test_parser_memory_efficiency() -> Result<()> {
        // Given: Large document
        let large_doc = format!(
            "# Main\n\n{}\n\n## Sub\n\n{}",
            "Content line.\n".repeat(1000),
            "More content.\n".repeat(1000)
        );

        let mut parser = create_test_parser();

        // When: Parsing large document
        let result = parser.parse(&large_doc)?;

        // Then: Should handle efficiently
        assert!(!result.heading_blocks.is_empty());
        assert_eq!(result.line_count, large_doc.lines().count());

        // Verify content is captured
        let main_block = result
            .heading_blocks
            .iter()
            .find(|block| block.path.contains(&"Main".to_string()));
        assert!(main_block.is_some());

        Ok(())
    }

    #[test]
    fn test_parser_edge_cases() -> Result<()> {
        // Given: Various edge cases
        let edge_cases = vec![
            // Only whitespace
            "   \n\t\n   ",
            // Just headings, no content
            "# A\n## B\n### C\n#### D",
            // Headings with only symbols
            "# !!!\n## ???\n### ***",
            // Empty headings
            "#\n##\n###",
            // Headings with trailing spaces
            "# Heading   \n## Another    ",
            // Mixed heading styles (if tree-sitter supports them)
            "# ATX Style\nSetext Style\n============",
        ];

        let mut parser = create_test_parser();

        for edge_case in edge_cases {
            // When: Parsing edge case
            let result = parser.parse(edge_case);

            // Then: Should handle gracefully
            match result {
                Ok(parse_result) => {
                    assert!(parse_result.line_count == edge_case.lines().count());
                    assert!(!parse_result.heading_blocks.is_empty()); // Always has at least default
                },
                Err(e) => {
                    // Should be a reasonable error
                    assert!(e.to_string().contains("parse") || e.to_string().contains("Parse"));
                },
            }
        }

        Ok(())
    }

    #[test]
    fn test_diagnostic_generation() -> Result<()> {
        // Given: Markdown that should generate diagnostics
        let problematic_markdown = r"Some content without headings

More content here

And even more content
";

        let mut parser = create_test_parser();

        // When: Parsing markdown without headings
        let result = parser.parse(problematic_markdown)?;

        // Then: Should generate appropriate diagnostics
        assert!(!result.diagnostics.is_empty());

        let warning_diagnostic = result.diagnostics.iter().find(|d| {
            matches!(d.severity, DiagnosticSeverity::Warn) && d.message.contains("No headings")
        });
        assert!(warning_diagnostic.is_some());

        Ok(())
    }

    #[test]
    fn test_parser_consistency() -> Result<()> {
        // Given: Same markdown parsed multiple times
        let mut parser = create_test_parser();
        let markdown = simple_markdown();

        // When: Parsing the same content multiple times
        let result1 = parser.parse(markdown)?;
        let result2 = parser.parse(markdown)?;

        // Then: Results should be consistent
        assert_eq!(result1.heading_blocks.len(), result2.heading_blocks.len());
        assert_eq!(result1.toc.len(), result2.toc.len());
        assert_eq!(result1.line_count, result2.line_count);

        // Compare heading paths
        for (block1, block2) in result1
            .heading_blocks
            .iter()
            .zip(result2.heading_blocks.iter())
        {
            assert_eq!(block1.path, block2.path);
            assert_eq!(block1.start_line, block2.start_line);
            assert_eq!(block1.end_line, block2.end_line);
        }

        Ok(())
    }

    #[test]
    #[allow(clippy::similar_names)] // Test uses similar names for related test blocks
    fn test_heading_blocks_no_duplication() -> Result<()> {
        // Given: Markdown with sentinel markers to verify exact extraction
        let markdown = r"# First Heading
SENTINEL_FIRST_START
Content under first heading
with multiple lines
SENTINEL_FIRST_END

## First Sub
SENTINEL_SUB_START  
Content under first sub
SENTINEL_SUB_END

## Second Sub
SENTINEL_SUB2_START
Content under second sub
SENTINEL_SUB2_END

# Second Heading
SENTINEL_SECOND_START
Final content
SENTINEL_SECOND_END";

        let mut parser = create_test_parser();
        let result = parser.parse(markdown)?;

        // Verify correct number of blocks
        assert_eq!(
            result.heading_blocks.len(),
            4,
            "Should have 4 heading blocks"
        );

        // Verify no content duplication
        for block in &result.heading_blocks {
            // Each sentinel should appear exactly once
            let first_count = block.content.matches("SENTINEL_FIRST_START").count();
            let sub_count = block.content.matches("SENTINEL_SUB_START").count();
            let sub2_count = block.content.matches("SENTINEL_SUB2_START").count();
            let second_count = block.content.matches("SENTINEL_SECOND_START").count();

            // Each block should contain at most one sentinel section
            assert!(first_count <= 1, "First sentinel duplicated");
            assert!(sub_count <= 1, "Sub sentinel duplicated");
            assert!(sub2_count <= 1, "Sub2 sentinel duplicated");
            assert!(second_count <= 1, "Second sentinel duplicated");
        }

        // Verify each block contains its expected content
        let first_block = &result.heading_blocks[0];
        assert!(first_block.content.contains("SENTINEL_FIRST_START"));
        assert!(first_block.content.contains("SENTINEL_FIRST_END"));
        assert!(!first_block.content.contains("SENTINEL_SUB_START"));

        let sub_block = &result.heading_blocks[1];
        assert!(sub_block.content.contains("SENTINEL_SUB_START"));
        assert!(sub_block.content.contains("SENTINEL_SUB_END"));
        assert!(!sub_block.content.contains("SENTINEL_FIRST"));
        assert!(!sub_block.content.contains("SENTINEL_SUB2"));

        let sub2_block = &result.heading_blocks[2];
        assert!(sub2_block.content.contains("SENTINEL_SUB2_START"));
        assert!(sub2_block.content.contains("SENTINEL_SUB2_END"));
        assert!(!sub2_block.content.contains("SENTINEL_SUB_START"));
        assert!(!sub2_block.content.contains("SENTINEL_SECOND"));

        let second_block = &result.heading_blocks[3];
        assert!(second_block.content.contains("SENTINEL_SECOND_START"));
        assert!(second_block.content.contains("SENTINEL_SECOND_END"));
        assert!(!second_block.content.contains("SENTINEL_FIRST"));
        assert!(!second_block.content.contains("SENTINEL_SUB"));

        Ok(())
    }

    #[test]
    fn test_line_ranges_accuracy() -> Result<()> {
        // Given: Markdown with known line structure
        let markdown = "# Heading at Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n## Sub at Line 6\nLine 7\nLine 8\n# Another at Line 9\nLine 10";

        let mut parser = create_test_parser();
        let result = parser.parse(markdown)?;

        assert_eq!(result.line_count, 10, "Should have 10 lines total");
        assert_eq!(
            result.heading_blocks.len(),
            3,
            "Should have 3 heading blocks"
        );

        // First block: "Heading at Line 1" (lines 1-5)
        let first = &result.heading_blocks[0];
        assert_eq!(first.path, vec!["Heading at Line 1"]);
        assert_eq!(first.start_line, 1, "First heading starts at line 1");
        assert_eq!(first.end_line, 5, "First heading ends at line 5");

        // Second block: "Sub at Line 6" (lines 6-8)
        let second = &result.heading_blocks[1];
        assert_eq!(second.path, vec!["Heading at Line 1", "Sub at Line 6"]);
        assert_eq!(second.start_line, 6, "Sub heading starts at line 6");
        assert_eq!(second.end_line, 8, "Sub heading ends at line 8");

        // Third block: "Another at Line 9" (lines 9-10)
        let third = &result.heading_blocks[2];
        assert_eq!(third.path, vec!["Another at Line 9"]);
        assert_eq!(third.start_line, 9, "Another heading starts at line 9");
        assert_eq!(third.end_line, 10, "Another heading ends at line 10");

        Ok(())
    }

    #[test]
    fn test_unicode_mixed_headings_edge_cases() -> Result<()> {
        // Given: Markdown with unicode and various heading levels
        let markdown = r"# 🔥 Main Section
Content with emoji

## Ünïcödë Heading
Спецйальные символы

### Deep → Nested ← Section
More content here

#### Even Deeper
Nested content

##### Fifth Level
Very deep

###### Sixth Level  
Deepest level

### Back to Level 3
After deep nesting";

        let mut parser = create_test_parser();
        let result = parser.parse(markdown)?;

        // Should handle all heading levels
        assert!(
            result.heading_blocks.len() >= 7,
            "Should extract all heading levels"
        );

        // Verify unicode preservation
        assert!(result.heading_blocks[0].path[0].contains("🔥"));
        assert!(result.heading_blocks[1].path[1].contains("Ünïcödë"));

        // Verify proper nesting handling
        let deep_block = result
            .heading_blocks
            .iter()
            .find(|b| b.path.last().is_some_and(|p| p.contains("Fifth Level")))
            .expect("Should find Fifth Level heading");
        assert!(
            deep_block.path.len() >= 5,
            "Fifth level should be deeply nested"
        );

        // Verify back-tracking works (going from level 6 back to level 3)
        let back_block = result
            .heading_blocks
            .iter()
            .find(|b| b.path.last().is_some_and(|p| p.contains("Back to Level 3")))
            .expect("Should find Back to Level 3 heading");
        assert_eq!(
            back_block.path.len(),
            3,
            "Should be at level 3 after backtracking"
        );

        Ok(())
    }
}