@@ -192,97 +192,99 @@ class SectionWordCountEditorPlugin implements PluginValue {
192192 return match ? match [ 1 ] . length : null ;
193193 } ;
194194
195+ // Start processing from the beginning of the first visible range
196+ const { from } = view . visibleRanges [ 0 ] ;
195197 const doc = view . state . doc ;
198+ const lineStart = doc . lineAt ( from ) ;
196199 const lineCount = doc . lines ;
197200 const sectionCounts : SectionCountData [ ] = [ ] ;
198201 const nested : SectionCountData [ ] = [ ] ;
199202
200- for ( const { from } of view . visibleRanges ) {
201- const lineStart = doc . lineAt ( from ) ;
202-
203- for ( let i = lineStart . number , len = lineCount ; i <= len ; i ++ ) {
204- let line : Line ;
205- if ( i === lineStart . number ) line = lineStart ;
206- else line = doc . line ( i ) ;
207-
208- const level = getHeaderLevel ( line ) ;
209- const prevHeading = nested . last ( ) ;
210- if ( level ) {
211- if ( ! prevHeading || level > prevHeading . level ) {
212- nested . push ( {
213- line : i ,
214- level,
215- self : 0 ,
216- total : 0 ,
217- pos : line . to ,
218- } ) ;
219- } else if ( prevHeading . level === level ) {
220- const nestedHeading = nested . pop ( ) ;
221- sectionCounts . push ( nestedHeading ) ;
222- nested . push ( {
223- line : i ,
224- level,
225- self : 0 ,
226- total : 0 ,
227- pos : line . to ,
228- } ) ;
229- } else if ( prevHeading . level > level ) {
230- // Traversing to lower level heading (eg. ### -> ##)
231- for ( let j = nested . length - 1 ; j >= 0 ; j -- ) {
232- const nestedHeading = nested [ j ] ;
233-
234- if ( level < nestedHeading . level ) {
235- // Continue traversing to lower level heading
236- const nestedHeading = nested . pop ( ) ;
237- sectionCounts . push ( nestedHeading ) ;
238- if ( j === 0 ) {
239- nested . push ( {
240- line : i ,
241- level,
242- self : 0 ,
243- total : 0 ,
244- pos : line . to ,
245- } ) ;
246- }
247- continue ;
248- }
249-
250- if ( level === nestedHeading . level ) {
251- // Stop because we found an equal level heading
252- const nestedHeading = nested . pop ( ) ;
253- sectionCounts . push ( nestedHeading ) ;
203+ for ( let i = lineStart . number ; i <= lineCount ; i ++ ) {
204+ let line : Line ;
205+ if ( i === lineStart . number ) line = lineStart ;
206+ else line = doc . line ( i ) ;
207+
208+ const level = getHeaderLevel ( line ) ;
209+ const prevHeading = nested . last ( ) ;
210+ if ( level ) {
211+ if ( ! prevHeading || level > prevHeading . level ) {
212+ // The first heading or moving to a higher level eg. ## -> ###
213+ nested . push ( {
214+ line : i ,
215+ level,
216+ self : 0 ,
217+ total : 0 ,
218+ pos : line . to ,
219+ } ) ;
220+ } else if ( prevHeading . level === level ) {
221+ // Same level as the previous heading
222+ const nestedHeading = nested . pop ( ) ;
223+ sectionCounts . push ( nestedHeading ) ;
224+ nested . push ( {
225+ line : i ,
226+ level,
227+ self : 0 ,
228+ total : 0 ,
229+ pos : line . to ,
230+ } ) ;
231+ } else if ( prevHeading . level > level ) {
232+ // Traversing to lower level heading (eg. ### -> ##)
233+ for ( let j = nested . length - 1 ; j >= 0 ; j -- ) {
234+ const nestedHeading = nested [ j ] ;
235+
236+ if ( level < nestedHeading . level ) {
237+ // Continue traversing to lower level heading
238+ const nestedHeading = nested . pop ( ) ;
239+ sectionCounts . push ( nestedHeading ) ;
240+ if ( j === 0 ) {
254241 nested . push ( {
255242 line : i ,
256243 level,
257244 self : 0 ,
258245 total : 0 ,
259246 pos : line . to ,
260247 } ) ;
261- break ;
262248 }
249+ continue ;
250+ }
263251
264- if ( level > nestedHeading . level ) {
265- // Stop because we found an higher level heading
266- nested . push ( {
267- line : i ,
268- level,
269- self : 0 ,
270- total : 0 ,
271- pos : line . to ,
272- } ) ;
273- break ;
274- }
252+ if ( level === nestedHeading . level ) {
253+ // Stop because we found an equal level heading
254+ const nestedHeading = nested . pop ( ) ;
255+ sectionCounts . push ( nestedHeading ) ;
256+ nested . push ( {
257+ line : i ,
258+ level,
259+ self : 0 ,
260+ total : 0 ,
261+ pos : line . to ,
262+ } ) ;
263+ break ;
275264 }
276- }
277- } else if ( nested . length ) {
278- const count = this . lineCounts [ i - 1 ] ;
279- for ( const heading of nested ) {
280- if ( heading === prevHeading ) {
281- heading . self += count ;
265+
266+ if ( level > nestedHeading . level ) {
267+ // Stop because we found an higher level heading
268+ nested . push ( {
269+ line : i ,
270+ level,
271+ self : 0 ,
272+ total : 0 ,
273+ pos : line . to ,
274+ } ) ;
275+ break ;
282276 }
283- heading . total += count ;
284277 }
285278 }
279+ } else if ( nested . length ) {
280+ // Not in a heading, so add the word count of the line to the headings containing this line
281+ const count = this . lineCounts [ i - 1 ] ;
282+ for ( const heading of nested ) {
283+ if ( heading === prevHeading ) {
284+ heading . self += count ;
285+ }
286+ heading . total += count ;
287+ }
286288 }
287289 }
288290
0 commit comments