Skip to content

Commit 51aba9f

Browse files
committedNov 18, 2024
fix: code snippets with {monaco-write} should not trigger slide reload
1 parent 3ba948c commit 51aba9f

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed
 

‎packages/slidev/node/syntax/transform/snippet.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ function findRegion(lines: Array<string>, regionName: string) {
6868
return null
6969
}
7070

71+
const reMonacoWrite = /^\{monaco-write\}/
72+
7173
/**
7274
* format: ">>> /path/to/file.extension#region language meta..."
7375
* where #region, language and meta are optional
@@ -93,8 +95,9 @@ export function transformSnippet({ s, slide, options }: MarkdownTransformContext
9395
: path.resolve(dir, filepath),
9496
)
9597

96-
watchFiles[src] ??= new Set()
97-
watchFiles[src].add(slide.index)
98+
meta = meta.trim()
99+
lang = lang.trim()
100+
lang = lang || path.extname(filepath).slice(1)
98101

99102
const isAFile = fs.statSync(src).isFile()
100103
if (!fs.existsSync(src) || !isAFile) {
@@ -119,17 +122,17 @@ export function transformSnippet({ s, slide, options }: MarkdownTransformContext
119122
}
120123
}
121124

122-
meta = meta.trim()
123-
lang = lang.trim()
124-
lang = lang || path.extname(filepath).slice(1)
125-
126-
if (meta.match(/^\{monaco-write\}/)) {
125+
if (meta.match(reMonacoWrite)) {
127126
monacoWriterWhitelist.add(filepath)
128127
lang = lang.trim()
129-
meta = meta.replace(/^\{monaco-write\}/, '').trim() || '{}'
128+
meta = meta.replace(reMonacoWrite, '').trim() || '{}'
130129
const encoded = lz.compressToBase64(content)
131130
return `<Monaco writable=${JSON.stringify(filepath)} code-lz="${encoded}" lang="${lang}" v-bind="${meta}" />`
132131
}
132+
else {
133+
watchFiles[src] ??= new Set()
134+
watchFiles[src].add(slide.index)
135+
}
133136

134137
return `\`\`\`${lang} ${meta}\n${content}\n\`\`\``
135138
},

0 commit comments

Comments
 (0)
Please sign in to comment.