Skip to content

Commit f3f0147

Browse files
Ahacadantfu
andauthored
fix(md): escape vue template with character references, close #99 (#100)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent 103da55 commit f3f0147

File tree

5 files changed

+5
-16
lines changed

5 files changed

+5
-16
lines changed

demo/composable-vue/slides.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ watch(counter, count => {
202202
```html
203203
<template>
204204
<button @click="counter += 1">
205-
Counter is {\{ counter }}
205+
Counter is {{ counter }}
206206
</button>
207207
</template>
208208
```

packages/slidev/node/plugins/fix.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,5 @@ export function createFixPlugins(
1515
return code.replace(/__DEV__/g, DEV)
1616
},
1717
},
18-
{
19-
name: 'slidev:vue-escape-post',
20-
enforce: 'post',
21-
transform(code, id) {
22-
if (id.endsWith('.md'))
23-
return code.replace(/\\{/g, '{')
24-
},
25-
},
2618
]
2719
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ function selectLanguage(options: Options, lang: string): [string, Grammar | unde
107107
* (markdown-it’s langPrefix + lang). If Prism knows {@code lang}, {@code text} will be highlighted by it.
108108
*/
109109
function highlight(markdownit: MarkdownIt, options: Options, text: string, lang: string): string {
110-
text = escapeVueInCode(text)
111110
const [langToUse, prismLang] = selectLanguage(options, lang)
112111
const code = text
113112
.trimEnd()
@@ -120,7 +119,7 @@ function highlight(markdownit: MarkdownIt, options: Options, text: string, lang:
120119
const classAttribute = langToUse
121120
? ` class="slidev-code ${markdownit.options.langPrefix}${markdownit.utils.escapeHtml(langToUse)}"`
122121
: ''
123-
return `<pre${classAttribute}><code>${code}</code></pre>`
122+
return escapeVueInCode(`<pre${classAttribute}><code>${code}</code></pre>`)
124123
}
125124

126125
/**

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,17 @@ const MarkdownItShiki: MarkdownIt.PluginWithOptions<ShikiOptions> = (markdownit,
4949
const { darkModeThemes } = resolveShikiOptions(options!)
5050

5151
markdownit.options.highlight = (code, lang) => {
52-
code = escapeVueInCode(code)
5352
if (darkModeThemes) {
5453
const dark = _highlighter
5554
.codeToHtml(code, lang || 'text', darkModeThemes.dark)
5655
.replace('<pre class="shiki"', '<pre class="slidev-code shiki shiki-dark"')
5756
const light = _highlighter
5857
.codeToHtml(code, lang || 'text', darkModeThemes.light)
5958
.replace('<pre class="shiki"', '<pre class="slidev-code shiki shiki-light"')
60-
return `<pre class="shiki-container">${dark}${light}</pre>`
59+
return escapeVueInCode(`<pre class="shiki-container">${dark}${light}</pre>`)
6160
}
6261
else {
63-
return _highlighter.codeToHtml(code, lang || 'text')
64-
.replace('<pre class="shiki"', '<pre class="slidev-code shiki"')
62+
return escapeVueInCode(_highlighter.codeToHtml(code, lang || 'text').replace('<pre class="shiki"', '<pre class="slidev-code shiki"'))
6563
}
6664
}
6765
}

packages/slidev/node/plugins/markdown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,5 @@ export function transformMermaid(md: string): string {
141141
}
142142

143143
export function escapeVueInCode(md: string) {
144-
return md.replace(/{({.*}})/g, '{\\$1')
144+
return md.replace(/{{(.*)}}/g, '&lbrace;&lbrace;$1&rbrace;&rbrace;')
145145
}

0 commit comments

Comments
 (0)