diff --git a/package.json b/package.json index 9bd2ccfe..3a27368b 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "onLanguage:markdown", "onLanguage:rmd", "onLanguage:quarto", + "onLanguage:typst", "workspaceContains:README.md" ], "main": "./dist/node/main.js", @@ -73,25 +74,25 @@ "commands": [ { "command": "markdown.extension.toc.create", - "enablement": "editorLangId =~ /^markdown$|^rmd$|^quarto$/", + "enablement": "editorLangId =~ /^markdown$|^rmd$|^quarto$|^typst$/", "title": "%command.toc.create.title%", "category": "Markdown All in One" }, { "command": "markdown.extension.toc.update", - "enablement": "editorLangId =~ /^markdown$|^rmd$|^quarto$/", + "enablement": "editorLangId =~ /^markdown$|^rmd$|^quarto|^typst$/", "title": "%command.toc.update.title%", "category": "Markdown All in One" }, { "command": "markdown.extension.toc.addSecNumbers", - "enablement": "editorLangId =~ /^markdown$|^rmd$|^quarto$/", + "enablement": "editorLangId =~ /^markdown$|^rmd$|^quarto$|^typst$/", "title": "%command.toc.addSecNumbers.title%", "category": "Markdown All in One" }, { "command": "markdown.extension.toc.removeSecNumbers", - "enablement": "editorLangId =~ /^markdown$|^rmd$|^quarto$/", + "enablement": "editorLangId =~ /^markdown$|^rmd$|^quarto$|^typst$/", "title": "%command.toc.removeSecNumbers.title%", "category": "Markdown All in One" }, @@ -625,7 +626,8 @@ "items": { "enum": [ "rmd", - "quarto" + "quarto", + "typst" ] }, "description": "%config.extraLangIds.description%" diff --git a/src/test/suite/integration/toc.test.ts b/src/test/suite/integration/toc.test.ts index af9d3eac..c78ea26d 100644 --- a/src/test/suite/integration/toc.test.ts +++ b/src/test/suite/integration/toc.test.ts @@ -650,19 +650,19 @@ suite("TOC.", () => { '---', 'title: test', '---', - '# 1. Heading 1', - '## 1.1. Heading 1.1', - ' 2. Heading 2', + '# 1 Heading 1', + '## 1.1 Heading 1.1', + ' 2 Heading 2', '===', '```markdown', '# _Heading 3', '```', - '## 2.1. Heading 2.1', + '## 2.1 Heading 2.1', '## _Heading 2.2 ', '', - '## 2.2. Heading 2.2', + '## 2.2 Heading 2.2', ], new Selection(0, 0, 0, 0)); }); @@ -692,19 +692,19 @@ suite("TOC.", () => { '---', 'title: test', '---', - '# 1. Heading 1', - '## 1.1. Heading 1.1', + '# 1 Heading 1', + '## 1.1 Heading 1.1', '2. Not Heading', '===', '```markdown', '# _Heading 3', '```', - '## 1.2. Heading 1.2', + '## 1.2 Heading 1.2', '## _Heading 2.2 ', '', - '## 1.3. Heading 1.3', + '## 1.3 Heading 1.3', ], new Selection(0, 0, 0, 0)); }); @@ -762,9 +762,9 @@ suite("TOC.", () => { new Selection(0, 0, 0, 0), [ '# Heading ', - '## 1. Heading 1', - '## 2. Heading 2', - '## 3. Heading 3', + '## 1 Heading 1', + '## 2 Heading 2', + '## 3 Heading 3', ], new Selection(0, 0, 0, 0)); }); @@ -781,9 +781,9 @@ suite("TOC.", () => { new Selection(0, 0, 0, 0), [ '# Heading', - '## 1. Heading 1', - '## 2. Heading 2', - '## 3. Heading 3', + '## 1 Heading 1', + '## 2 Heading 2', + '## 3 Heading 3', ], new Selection(0, 0, 0, 0) ); diff --git a/src/toc.ts b/src/toc.ts index 355e6040..eb533489 100644 --- a/src/toc.ts +++ b/src/toc.ts @@ -148,12 +148,13 @@ function addSectionNumbers() { secNumbers[level - 1] += 1; secNumbers.fill(0, level); - const secNumStr = [...Array(level - startDepth + 1).keys()].map(num => `${secNumbers[num + startDepth - 1]}.`).join(''); + // const secNumStr = [...Array(level - startDepth + 1).keys()].map(num => `${secNumbers[num + startDepth - 1]}.`).join(''); + const secNumStr = [...Array(level - startDepth + 1).keys()].map(num => `${secNumbers[num + startDepth - 1]}`).join('.'); const lineText = doc.lineAt(lineNum).text; - const newText = lineText.includes('#') - ? lineText.replace(/^(\s{0,3}#+ +)((?:\d{1,9}\.)* )?(.*)/, (_, g1, _g2, g3) => `${g1}${secNumStr} ${g3}`) - : lineText.replace(/^(\s{0,3})((?:\d{1,9}\.)* )?(.*)/, (_, g1, _g2, g3) => `${g1}${secNumStr} ${g3}`); + const newText = lineText.includes('=') || lineText.includes('#') + ? lineText.replace(/^(\s{0,3}[=#]+ +)((?:\d{1,9}\.?)* )?(.*)/, (_, g1, _g2, g3) => `${g1}${secNumStr} ${g3}`) + : lineText.replace(/^(\s{0,3})((?:\d{1,9}\.?)* )?(.*)/, (_, g1, _g2, g3) => `${g1}${secNumStr} ${g3}`); edit.replace(doc.uri, doc.lineAt(lineNum).range, newText); }); @@ -171,9 +172,9 @@ function removeSectionNumbers() { toc.forEach(entry => { const lineNum = entry.lineIndex; const lineText = doc.lineAt(lineNum).text; - const newText = lineText.includes('#') - ? lineText.replace(/^(\s{0,3}#+ +)((?:\d{1,9}\.)* )?(.*)/, (_, g1, _g2, g3) => `${g1}${g3}`) - : lineText.replace(/^(\s{0,3})((?:\d{1,9}\.)* )?(.*)/, (_, g1, _g2, g3) => `${g1}${g3}`); + const newText = lineText.includes('=') || lineText.includes('#') + ? lineText.replace(/^(\s{0,3}[=#]+ +)((?:\d{1,9}\.?)* )?(.*)/, (_, g1, _g2, g3) => `${g1}${g3}`) + : lineText.replace(/^(\s{0,3})((?:\d{1,9}\.?)* )?(.*)/, (_, g1, _g2, g3) => `${g1}${g3}`); edit.replace(doc.uri, doc.lineAt(lineNum).range, newText); }); @@ -184,7 +185,7 @@ function onWillSave(e: TextDocumentWillSaveEvent): void { if (!tocConfig.updateOnSave) { return; } - if (e.document.languageId === 'markdown') { + if (isMdDocument(e.document)) { e.waitUntil(updateToc()); } } @@ -583,16 +584,14 @@ export function getAllRootHeading(doc: TextDocument, respectMagicCommentOmit: bo for (let i: number = 0; i < lines.length; i++) { const crtLineText = lines[i]; + // + var ishead = /^(?:\/\/|) {0,3}[#=]{1,6}(?: |\t|$)/.test(crtLineText); + // Skip non-ATX heading lines. - if ( - // - !/^ {0,3}#{1,6}(?: |\t|$)/.test(crtLineText) - ) { - continue; - } + if (!ishead) continue; // Extract heading info. - const matches = /^ {0,3}(#{1,6})(.*)$/.exec(crtLineText)!; + const matches = /^(?:\/\/|) {0,3}([#=]{1,6})(.*)$/.exec(crtLineText)!; const entry: IHeadingBase = { level: matches[1].length as MarkdownSpec.MarkdownHeadingLevel, rawContent: matches[2].replace(/^[ \t]+/, '').replace(/[ \t]+#+[ \t]*$/, ''), @@ -642,7 +641,6 @@ export function getAllRootHeading(doc: TextDocument, respectMagicCommentOmit: bo toc.push(entry); } - return toc; }