From 51aba9f06ccdb74d349580dc0052cd0592d52255 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 18 Nov 2024 12:38:00 +0800 Subject: [PATCH] fix: code snippets with `{monaco-write}` should not trigger slide reload --- .../slidev/node/syntax/transform/snippet.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/slidev/node/syntax/transform/snippet.ts b/packages/slidev/node/syntax/transform/snippet.ts index 2e10d9fb61..7197bd310e 100644 --- a/packages/slidev/node/syntax/transform/snippet.ts +++ b/packages/slidev/node/syntax/transform/snippet.ts @@ -68,6 +68,8 @@ function findRegion(lines: Array, regionName: string) { return null } +const reMonacoWrite = /^\{monaco-write\}/ + /** * format: ">>> /path/to/file.extension#region language meta..." * where #region, language and meta are optional @@ -93,8 +95,9 @@ export function transformSnippet({ s, slide, options }: MarkdownTransformContext : path.resolve(dir, filepath), ) - watchFiles[src] ??= new Set() - watchFiles[src].add(slide.index) + meta = meta.trim() + lang = lang.trim() + lang = lang || path.extname(filepath).slice(1) const isAFile = fs.statSync(src).isFile() if (!fs.existsSync(src) || !isAFile) { @@ -119,17 +122,17 @@ export function transformSnippet({ s, slide, options }: MarkdownTransformContext } } - meta = meta.trim() - lang = lang.trim() - lang = lang || path.extname(filepath).slice(1) - - if (meta.match(/^\{monaco-write\}/)) { + if (meta.match(reMonacoWrite)) { monacoWriterWhitelist.add(filepath) lang = lang.trim() - meta = meta.replace(/^\{monaco-write\}/, '').trim() || '{}' + meta = meta.replace(reMonacoWrite, '').trim() || '{}' const encoded = lz.compressToBase64(content) return `` } + else { + watchFiles[src] ??= new Set() + watchFiles[src].add(slide.index) + } return `\`\`\`${lang} ${meta}\n${content}\n\`\`\`` },