(doc: TextDocument)
| 337 | } |
| 338 | |
| 339 | export function buildToc(doc: TextDocument) { |
| 340 | const replacer = (foundStr: string) => foundStr.replace(/[^\r\n]/g, ''); |
| 341 | let lines = doc.getText() |
| 342 | .replace(REGEX_FENCED_CODE_BLOCK, replacer) //// Remove fenced code blocks (and #603, #675) |
| 343 | .replace(/<!-- omit in (toc|TOC) -->/g, '< omit in toc >') //// Escape magic comment |
| 344 | .replace(/<!--[\W\w]+?-->/g, replacer) //// Remove comments |
| 345 | .replace(/^---[\W\w]+?(\r?\n)---/, replacer) //// Remove YAML front matter |
| 346 | .split(/\r?\n/g); |
| 347 | |
| 348 | //// Some special cases that we need to look at multiple lines to decide |
| 349 | lines.forEach((lineText, i, arr) => { |
| 350 | //// Transform setext headings to ATX headings |
| 351 | if ( |
| 352 | i < arr.length - 1 |
| 353 | && lineText.match(/^ {0,3}\S.*$/) |
| 354 | && lineText.replace(/[ -]/g, '').length > 0 //// #629 |
| 355 | && arr[i + 1].match(/^ {0,3}(=+|-{2,}) *$/) |
| 356 | ) { |
| 357 | arr[i] = (arr[i + 1].includes('=') ? '# ' : '## ') + lineText; |
| 358 | } |
| 359 | //// Ignore headings following `<!-- omit in toc -->` |
| 360 | if ( |
| 361 | i > 0 |
| 362 | && arr[i - 1] === '< omit in toc >' |
| 363 | ) { |
| 364 | arr[i] = ''; |
| 365 | } |
| 366 | }); |
| 367 | |
| 368 | const toc = lines.map((lineText, index) => { |
| 369 | if ( |
| 370 | lineText.trim().startsWith('#') |
| 371 | && !lineText.startsWith(' ') //// The opening `#` character may be indented 0-3 spaces |
| 372 | && lineText.includes('# ') |
| 373 | && !lineText.includes('< omit in toc >') |
| 374 | ) { |
| 375 | lineText = lineText.replace(/^ +/, ''); |
| 376 | const matches = /^(#+) (.*)/.exec(lineText); |
| 377 | const entry = { |
| 378 | level: matches[1].length, |
| 379 | text: matches[2].replace(/#+$/, '').trim(), |
| 380 | lineNum: index, |
| 381 | }; |
| 382 | return entry; |
| 383 | } else { |
| 384 | return null; |
| 385 | } |
| 386 | }).filter(entry => entry !== null); |
| 387 | |
| 388 | return toc; |
| 389 | } |
| 390 | |
| 391 | class TocCodeLensProvider implements CodeLensProvider { |
| 392 | public provideCodeLenses(document: TextDocument, _: CancellationToken): |
no outgoing calls
no test coverage detected