@@ -23,15 +23,12 @@ export function findSections(text: string, languageId?: string): SectionMatch[]
2323
2424 // For Markdown/Quarto files, find code block ranges to exclude from parsing
2525 const codeBlocks : { start : number ; end : number } [ ] = [ ] ;
26- let lineStartIndices : number [ ] = [ ] ;
2726 if ( isMarkdownOrQuarto ) {
2827 const lines = text . split ( '\n' ) ;
2928 let inCodeBlock = false ;
3029 let codeBlockStart = 0 ;
31- let charIndex = 0 ;
32- lineStartIndices = [ ] ;
30+
3331 lines . forEach ( ( line , index ) => {
34- lineStartIndices . push ( charIndex ) ;
3532 if ( line . trim ( ) . startsWith ( '```' ) ) {
3633 if ( ! inCodeBlock ) {
3734 inCodeBlock = true ;
@@ -44,7 +41,6 @@ export function findSections(text: string, languageId?: string): SectionMatch[]
4441 } ) ;
4542 }
4643 }
47- charIndex += line . length + 1 ; // +1 for '\n'
4844 } ) ;
4945 // Edge case: unmatched opening code block at end of file
5046 if ( inCodeBlock ) {
@@ -55,18 +51,10 @@ export function findSections(text: string, languageId?: string): SectionMatch[]
5551 // Helper function to check if a match index is inside a code block
5652 const isInCodeBlock = ( matchIndex : number ) : boolean => {
5753 if ( ! isMarkdownOrQuarto ) return false ;
58- // Binary search to find line number for matchIndex
59- let left = 0 , right = lineStartIndices . length - 1 ;
60- let matchLineNumber = 0 ;
61- while ( left <= right ) {
62- const mid = Math . floor ( ( left + right ) / 2 ) ;
63- if ( lineStartIndices [ mid ] <= matchIndex ) {
64- matchLineNumber = mid ;
65- left = mid + 1 ;
66- } else {
67- right = mid - 1 ;
68- }
69- }
54+
55+ const lines = text . substring ( 0 , matchIndex ) . split ( '\n' ) ;
56+ const matchLineNumber = lines . length - 1 ;
57+
7058 return codeBlocks . some ( block =>
7159 matchLineNumber >= block . start && matchLineNumber <= block . end
7260 ) ;
0 commit comments