From 614b15d6d1fc49dc668c06173404791987bc05b1 Mon Sep 17 00:00:00 2001 From: pi-dal Date: Tue, 28 Apr 2026 11:05:44 +0800 Subject: [PATCH 1/2] fix: ignore slide separators inside HTML comments --- packages/parser/src/core.ts | 51 +++++++++++++++++++++++++++++++++++-- test/parser.test.ts | 21 +++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/packages/parser/src/core.ts b/packages/parser/src/core.ts index f0b6ee16bd..a05565a47d 100644 --- a/packages/parser/src/core.ts +++ b/packages/parser/src/core.ts @@ -19,6 +19,31 @@ export interface SlidevParserOptions { preserveCR?: boolean } +function advanceHtmlCommentState(line: string, inHtmlComment: boolean) { + let cursor = 0 + + while (cursor < line.length) { + if (inHtmlComment) { + const end = line.indexOf('-->', cursor) + if (end < 0) + return true + inHtmlComment = false + cursor = end + 3 + } + else { + const start = line.indexOf('', start + 4) + if (end < 0) + return true + cursor = end + 3 + } + } + + return inHtmlComment +} + export function stringify(data: SlidevMarkdown) { return `${data.slides.map(stringifySlide).join('\n').trim()}\n` } @@ -200,6 +225,7 @@ export async function parse( let start = 0 let contentStart = 0 + let inHtmlComment = false async function slice(end: number) { if (start === end) @@ -247,7 +273,17 @@ export async function parse( } for (let i = 0; i < lines.length; i++) { - const line = lines[i].trimEnd() + const rawLine = lines[i] + const line = rawLine.trimEnd() + if (inHtmlComment) { + inHtmlComment = advanceHtmlCommentState(rawLine, true) + continue + } + + inHtmlComment = advanceHtmlCommentState(rawLine, false) + if (inHtmlComment) + continue + if (line.startsWith('---')) { await slice(i) @@ -296,6 +332,7 @@ export function parseSync( let start = 0 let contentStart = 0 + let inHtmlComment = false function slice(end: number) { if (start === end) @@ -315,7 +352,17 @@ export function parseSync( } for (let i = 0; i < lines.length; i++) { - const line = lines[i].trimEnd() + const rawLine = lines[i] + const line = rawLine.trimEnd() + if (inHtmlComment) { + inHtmlComment = advanceHtmlCommentState(rawLine, true) + continue + } + + inHtmlComment = advanceHtmlCommentState(rawLine, false) + if (inHtmlComment) + continue + if (line.startsWith('---')) { slice(i) diff --git a/test/parser.test.ts b/test/parser.test.ts index 027ff82a9a..954982095f 100644 --- a/test/parser.test.ts +++ b/test/parser.test.ts @@ -113,6 +113,27 @@ f .toEqual({ }) }) + it('ignores slide separators inside HTML comments', async () => { + const data = await parse(`--- +src: ./pages/one.md +--- + + + +--- +src: ./pages/three.md +--- +`, 'slides.md') + + expect(data.slides).toHaveLength(2) + expect(data.slides.map(slide => slide.frontmatter.src)) + .toEqual(['./pages/one.md', './pages/three.md']) + }) + async function parseWithExtension( src: string, transformRawLines: (lines: string[]) => void | Promise = () => {}, From de9382dbd9604a1fb941e23d1c012e3c3f2fffb9 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 7 May 2026 16:21:00 +0900 Subject: [PATCH 2/2] fix(parser): detect slide separator on lines that open HTML comments Reorders the parser loop so `---`/code-fence detection runs before the HTML-comment state is advanced. Previously a real separator like `---- + +b +`, 'file.md') + + expect(data.slides).toHaveLength(2) + expect(data.slides.map(s => s.content.trim())).toEqual(['a', 'hidden\n-->\n\nb']) + }) + async function parseWithExtension( src: string, transformRawLines: (lines: string[]) => void | Promise = () => {},