Skip to content

Commit

Permalink
fix: code snippets with {monaco-write} should not trigger slide reload
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Nov 18, 2024
1 parent 3ba948c commit 51aba9f
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/slidev/node/syntax/transform/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ function findRegion(lines: Array<string>, regionName: string) {
return null
}

const reMonacoWrite = /^\{monaco-write\}/

/**
* format: ">>> /path/to/file.extension#region language meta..."
* where #region, language and meta are optional
Expand All @@ -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) {
Expand All @@ -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 `<Monaco writable=${JSON.stringify(filepath)} code-lz="${encoded}" lang="${lang}" v-bind="${meta}" />`
}
else {
watchFiles[src] ??= new Set()
watchFiles[src].add(slide.index)
}

return `\`\`\`${lang} ${meta}\n${content}\n\`\`\``
},
Expand Down

0 comments on commit 51aba9f

Please sign in to comment.