Skip to content

Commit f6ee4a0

Browse files
committed
fix(md): escape vue template in code snippent, close #95
1 parent a000f66 commit f6ee4a0

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

packages/slidev/node/plugins/fix.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function createFixPlugins(
1616
},
1717
},
1818
{
19-
name: 'slidev:vue-escape',
19+
name: 'slidev:vue-escape-post',
2020
enforce: 'post',
2121
transform(code, id) {
2222
if (id.endsWith('.md'))

packages/slidev/node/plugins/markdown-it-prism.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import Prism, { Grammar } from 'prismjs'
55
import loadLanguages from 'prismjs/components/'
66
import MarkdownIt from 'markdown-it'
7+
import { escapeVueInCode } from './markdown'
78

89
interface Options {
910
plugins: string[]
@@ -106,6 +107,7 @@ function selectLanguage(options: Options, lang: string): [string, Grammar | unde
106107
* (markdown-it’s langPrefix + lang). If Prism knows {@code lang}, {@code text} will be highlighted by it.
107108
*/
108109
function highlight(markdownit: MarkdownIt, options: Options, text: string, lang: string): string {
110+
text = escapeVueInCode(text)
109111
const [langToUse, prismLang] = selectLanguage(options, lang)
110112
const code = text
111113
.trimEnd()

packages/slidev/node/plugins/markdown-it-shiki.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import type { ShikiHighlighter, IShikiTheme, IThemeRegistration } from 'shiki'
1+
import type { Highlighter as ShikiHighlighter, IShikiTheme, IThemeRegistration } from 'shiki'
22
import type MarkdownIt from 'markdown-it'
33
import type { ShikiOptions } from '@slidev/types'
44
import { ShikiDarkModeThemes } from 'packages/types'
5+
import { escapeVueInCode } from './markdown'
56

67
function getThemeName(theme: IThemeRegistration) {
78
if (typeof theme === 'string')
@@ -48,6 +49,7 @@ const MarkdownItShiki: MarkdownIt.PluginWithOptions<ShikiOptions> = (markdownit,
4849
const { darkModeThemes } = resolveShikiOptions(options!)
4950

5051
markdownit.options.highlight = (code, lang) => {
52+
code = escapeVueInCode(code)
5153
if (darkModeThemes) {
5254
const dark = _highlighter
5355
.codeToHtml(code, lang || 'text', darkModeThemes.dark)

packages/slidev/node/plugins/markdown.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export function transformPageCSS(md: string, id: string) {
130130
/**
131131
* Transform Mermaid code blocks (render done on client side)
132132
*/
133-
export default function transformMermaid(md: string): string {
133+
export function transformMermaid(md: string): string {
134134
return md
135135
.replace(/^```mermaid\s*?({.*?})?\n([\s\S]+?)\n```/mg, (full, options = '', code = '') => {
136136
code = code.trim()
@@ -139,3 +139,7 @@ export default function transformMermaid(md: string): string {
139139
return `<Mermaid :code="'${encoded}'" v-bind="${options}" />`
140140
})
141141
}
142+
143+
export function escapeVueInCode(md: string) {
144+
return md.replace(/{({.*}})/g, '{\\$1')
145+
}

0 commit comments

Comments
 (0)