From 0f7886e433966fdb1de34a1ada3be130ac66ee49 Mon Sep 17 00:00:00 2001 From: Michael Overmeyer Date: Thu, 7 Jan 2021 21:40:43 -0500 Subject: [PATCH] Don't treat ATX headings with following thematic breaks as setext headings --- src/test/suite/toc.test.ts | 18 ++++++++++++++++++ src/toc.ts | 11 ++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/test/suite/toc.test.ts b/src/test/suite/toc.test.ts index f625584d..75423d55 100644 --- a/src/test/suite/toc.test.ts +++ b/src/test/suite/toc.test.ts @@ -296,6 +296,24 @@ suite("TOC.", () => { new Selection(16, 15, 16, 15)).then(done, done); }); + test("ATX Heading followed by thematic break doesn't get parsed as a setext heading", done => { + testCommand('markdown.extension.toc.create', {}, + [ + '# H1', + '---', + '', + '', + ], + new Selection(3, 0, 3, 0), + [ + '# H1', + '---', + '', + '- [H1](#h1)', + ], + new Selection(3, 10, 3, 10)).then(done, done); + }); + test("Non-Latin symbols (Option `toc.slugifyMode: github`)", done => { testCommand('markdown.extension.toc.create', { diff --git a/src/toc.ts b/src/toc.ts index 23c9b6e0..5948eac6 100644 --- a/src/toc.ts +++ b/src/toc.ts @@ -319,6 +319,12 @@ function onWillSave(e: TextDocumentWillSaveEvent) { } } +function isAtxHeading(lineText: String): Boolean { + return lineText.trim().startsWith('#') + && !lineText.startsWith(' ') //// The opening `#` character may be indented 0-3 spaces + && lineText.includes('# ') +} + /** * Updates `tocConfig` and `docConfig`. * @param editor The editor, from which we detect `docConfig`. @@ -370,6 +376,7 @@ export function buildToc(doc: TextDocument): IHeading[] { //// Transform setext headings to ATX headings if ( i < arr.length - 1 + && !isAtxHeading(lineText) //// #879 && lineText.match(/^ {0,3}\S.*$/) && lineText.replace(/[ -]/g, '').length > 0 //// #629 && arr[i + 1].match(/^ {0,3}(=+|-{2,}) *$/) @@ -387,9 +394,7 @@ export function buildToc(doc: TextDocument): IHeading[] { const toc = lines.map((lineText, index) => { if ( - lineText.trim().startsWith('#') - && !lineText.startsWith(' ') //// The opening `#` character may be indented 0-3 spaces - && lineText.includes('# ') + isAtxHeading(lineText) && !lineText.includes('< omit in toc >') ) { lineText = lineText.replace(/^ +/, '');