feat: support interpolation inside code blocks

pull/875/head
Divyansh Singh 3 years ago
parent 7bfb463c37
commit b556c06c09

@ -5,23 +5,25 @@ export async function highlight(theme: ThemeOptions = 'material-palenight') {
const themes = typeof theme === 'string' ? [theme] : [theme.dark, theme.light] const themes = typeof theme === 'string' ? [theme] : [theme.dark, theme.light]
const highlighter = await getHighlighter({ themes }) const highlighter = await getHighlighter({ themes })
const preRE = /^<pre.*?>/ const preRE = /^<pre.*?>/
const vueRE = /-vue$/
return (str: string, lang: string) => { return (str: string, lang: string) => {
lang = lang || 'text' const vPre = vueRE.test(lang) ? '' : 'v-pre'
lang = lang.replace(vueRE, '')
if (typeof theme === 'string') { if (typeof theme === 'string') {
return highlighter return highlighter
.codeToHtml(str, { lang, theme }) .codeToHtml(str, { lang, theme })
.replace(preRE, '<pre v-pre>') .replace(preRE, `<pre ${vPre}>`)
} }
const dark = highlighter const dark = highlighter
.codeToHtml(str, { lang, theme: theme.dark }) .codeToHtml(str, { lang, theme: theme.dark })
.replace(preRE, '<pre v-pre class="vp-code-dark">') .replace(preRE, `<pre ${vPre} class="vp-code-dark">`)
const light = highlighter const light = highlighter
.codeToHtml(str, { lang, theme: theme.light }) .codeToHtml(str, { lang, theme: theme.light })
.replace(preRE, '<pre v-pre class="vp-code-light">') .replace(preRE, `<pre ${vPre} class="vp-code-light">`)
return dark + light return dark + light
} }

@ -13,7 +13,7 @@ export const preWrapperPlugin = (md: MarkdownIt) => {
const fence = md.renderer.rules.fence! const fence = md.renderer.rules.fence!
md.renderer.rules.fence = (...args) => { md.renderer.rules.fence = (...args) => {
const [tokens, idx] = args const [tokens, idx] = args
const lang = tokens[idx].info.trim() const lang = tokens[idx].info.trim().replace(/-vue$/, '')
const rawCode = fence(...args) const rawCode = fence(...args)
return `<div class="language-${lang}"><span class="lang">${ return `<div class="language-${lang}"><span class="lang">${
lang === 'vue-html' ? 'template' : lang lang === 'vue-html' ? 'template' : lang

Loading…
Cancel
Save