From ca74a8816b71e5b37154457b53e721ceb2202d6d Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sat, 1 May 2021 08:42:20 +0900 Subject: [PATCH 1/5] Add `markdown.marp.outlineExtension` setting --- package.json | 5 +++++ src/extension.ts | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 2f33928b..ef6dc2c4 100644 --- a/package.json +++ b/package.json @@ -127,6 +127,11 @@ "KaTeX (https://katex.org/): The default library in Marp Core" ] }, + "markdown.marp.outlineExtension": { + "type": "boolean", + "default": true, + "description": "Enables the outline extension for Marp Markdown. If enabled, VS Code's outline view will reflect slide splitters, and you can fold regions of the slide content in the editor." + }, "markdown.marp.themes": { "type": "array", "default": [], diff --git a/src/extension.ts b/src/extension.ts index e5ebaa04..70a65483 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -11,12 +11,13 @@ import customTheme from './plugins/custom-theme' import lineNumber from './plugins/line-number' import outline from './plugins/outline' import themes from './themes' -import { detectMarpFromMarkdown } from './utils' +import { detectMarpFromMarkdown, marpConfiguration } from './utils' const shouldRefreshConfs = [ 'markdown.marp.breaks', 'markdown.marp.enableHtml', 'markdown.marp.mathTypesetting', + 'markdown.marp.outlineExtension', 'markdown.marp.themes', 'markdown.preview.breaks', ] @@ -49,9 +50,12 @@ export function extendMarkdownIt(md: any) { const marp = new Marp(marpCoreOptionForPreview(md.options)) .use(customTheme) - .use(outline) .use(lineNumber) + if (marpConfiguration().get('outlineExtension') ?? true) { + marp.use(outline) + } + // Load custom themes Promise.all( themes.loadStyles(baseFolder).map((p) => From 562e69aab505dfdd0d4756a7e09f9fafabaf236e Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sat, 1 May 2021 08:50:57 +0900 Subject: [PATCH 2/5] Don't change the order of loading Marp plugins --- src/extension.ts | 7 ++++--- src/plugins/outline.ts | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 70a65483..dd373fd7 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -9,7 +9,7 @@ import diagnostics from './diagnostics/' import { marpCoreOptionForPreview, clearMarpCoreOptionCache } from './option' import customTheme from './plugins/custom-theme' import lineNumber from './plugins/line-number' -import outline from './plugins/outline' +import outline, { rule as outlineRule } from './plugins/outline' import themes from './themes' import { detectMarpFromMarkdown, marpConfiguration } from './utils' @@ -50,10 +50,11 @@ export function extendMarkdownIt(md: any) { const marp = new Marp(marpCoreOptionForPreview(md.options)) .use(customTheme) + .use(outline) .use(lineNumber) - if (marpConfiguration().get('outlineExtension') ?? true) { - marp.use(outline) + if (!(marpConfiguration().get('outlineExtension') ?? true)) { + marp.markdown.disable(outlineRule) } // Load custom themes diff --git a/src/plugins/outline.ts b/src/plugins/outline.ts index 600b8a9c..17badaef 100644 --- a/src/plugins/outline.ts +++ b/src/plugins/outline.ts @@ -1,5 +1,7 @@ +export const rule = 'marp_vscode_outline' + export default function marpVSCodeOutline(instance) { - instance.core.ruler.push('marp_vscode_outline', (state) => { + instance.core.ruler.push(rule, (state) => { if (state.inlineMode) return const tokens: any[] = [] From 70a4acdf3135014f5b05a0655435d230dce43ead Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sat, 1 May 2021 21:07:47 +0900 Subject: [PATCH 3/5] Add test for markdown.marp.outlineExtension --- src/__mocks__/vscode.ts | 1 + src/extension.test.ts | 62 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/__mocks__/vscode.ts b/src/__mocks__/vscode.ts index ab3c1a5f..e5ca7a07 100644 --- a/src/__mocks__/vscode.ts +++ b/src/__mocks__/vscode.ts @@ -6,6 +6,7 @@ const defaultConf: MockedConf = { 'markdown.marp.chromePath': '', 'markdown.marp.enableHtml': false, 'markdown.marp.exportType': 'pdf', + 'markdown.marp.outlineExtension': true, 'window.zoomLevel': 0, } diff --git a/src/extension.test.ts b/src/extension.test.ts index 697d9545..f1948a13 100644 --- a/src/extension.test.ts +++ b/src/extension.test.ts @@ -287,5 +287,67 @@ describe('#extendMarkdownIt', () => { ).not.toContain('@custom theme') }) }) + + describe('markdown.marp.outlineExtension', () => { + const markdown = dedent` + --- + marp: true + --- + + 1 + + --- + + 2 + ` + + it('adds hidden heading token marked as zero-level if enabled', () => { + setConfiguration({ 'markdown.marp.outlineExtension': true }) + + const parsed = md().parse(markdown) + const hiddenHeadings = parsed.filter( + (t) => t.type === 'heading_open' && t.level === 0 && t.hidden + ) + + expect(hiddenHeadings).toHaveLength(2) + expect(hiddenHeadings[0].map[0]).toBe(0) + expect(hiddenHeadings[1].map[0]).toBe(6) + + // headingDivider directive + const headingDivider = md().parse(dedent` + --- + marp: true + headingDivider: 2 + --- + + # 1 + + ## 2 + + ### 3 + + ## 4 + `) + const hiddenHeadingDividers = headingDivider.filter( + (t) => t.type === 'heading_open' && t.level === 0 && t.hidden + ) + + expect(hiddenHeadingDividers).toHaveLength(3) + expect(hiddenHeadingDividers[0].map[0]).toBe(0) + expect(hiddenHeadingDividers[1].map[0]).toBe(7) + expect(hiddenHeadingDividers[2].map[0]).toBe(11) + }) + + it('does not add zero-level heading token if disabled', () => { + setConfiguration({ 'markdown.marp.outlineExtension': false }) + + const parsed = md().parse(markdown) + const hiddenHeadings = parsed.filter( + (t) => t.type === 'heading_open' && t.level === 0 && t.hidden + ) + + expect(hiddenHeadings).toHaveLength(0) + }) + }) }) }) From 5b249837fe5a31acaac245ad9a1888e6b29bc899 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sat, 1 May 2021 21:30:48 +0900 Subject: [PATCH 4/5] [ci skip] Update README.md --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2f9fe29a..2ddc5971 100644 --- a/README.md +++ b/README.md @@ -113,9 +113,13 @@ Markdown preview will reload updated theme CSS automatically when you edited the Use custom theme

-### Outline view for each slide +### Outline extension -We extend [a native outline view](https://code.visualstudio.com/docs/languages/markdown#_outline-view) to support slide pages in Marp Markdown. +When Marp Markdown is enabled, you can use the extended [outline view](https://code.visualstudio.com/docs/languages/markdown#_outline-view) like following. They are enabled by default but you may disable by `markdown.marp.outlineExtension` preference. + +#### Outline view for each slide + +We extend the outline view to support slide pages in Marp Markdown.

Outline view for each slide @@ -123,7 +127,7 @@ We extend [a native outline view](https://code.visualstudio.com/docs/languages/m > ℹ️ Please choose `Sort By: Position` from context menu of its panel if you see incorrect slide order. -### Slide folding in editor +#### Slide folding in editor You can fold the content of slide in editor while editing Marp Markdown. From dddeaa8ec882f8748f72a1bb7f96c798b6fca4ba Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sat, 1 May 2021 21:43:47 +0900 Subject: [PATCH 5/5] [ci skip] Update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de7339eb..6d04e2ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ ### Added -- Set up automated publication into Open VSX ([#211](https://github.com/marp-team/marp-vscode/issues/211), [#218](https://github.com/marp-team/marp-vscode/pull/218)) +- Set up automated publication into [Open VSX](https://open-vsx.org/extension/marp-team/marp-vscode) ([#211](https://github.com/marp-team/marp-vscode/issues/211), [#218](https://github.com/marp-team/marp-vscode/pull/218)) +- `markdown.marp.outlineExtension` preference to enable or disable the outline extension ([#212](https://github.com/marp-team/marp-vscode/issues/212), [#221](https://github.com/marp-team/marp-vscode/pull/221)) ### Changed