fix: properly apply dark/light classes in code blocks (#1546)

pull/1547/head
Divyansh Singh 2 years ago committed by GitHub
parent d9fc0b26a3
commit 178895f067
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,7 +6,6 @@ import {
createRangeProcessor, createRangeProcessor,
getHighlighter, getHighlighter,
Processor, Processor,
addClass,
defineProcessor defineProcessor
} from 'shiki-processor' } from 'shiki-processor'
import type { ThemeOptions } from '../markdown' import type { ThemeOptions } from '../markdown'
@ -69,7 +68,8 @@ export async function highlight(
processors processors
}) })
const styleRE = /<pre .* (style=".*")><code>/ const classRE = /<pre[^>]*class="(.*?)"/
const styleRE = /<pre[^>]*(style=".*?")/
const preRE = /^<pre(.*?)>/ const preRE = /^<pre(.*?)>/
const vueRE = /-vue$/ const vueRE = /-vue$/
@ -93,29 +93,21 @@ export async function highlight(
) )
} }
const dark = addClass( const dark = cleanup(
cleanup(
highlighter.codeToHtml(str, { highlighter.codeToHtml(str, {
lang, lang,
lineOptions, lineOptions,
theme: getThemeName(theme.dark) theme: getThemeName(theme.dark)
}) })
), ).replace(classRE, (_, cls) => _.replace(cls, 'vp-code-dark'))
'vp-code-dark',
'pre'
)
const light = addClass( const light = cleanup(
cleanup(
highlighter.codeToHtml(str, { highlighter.codeToHtml(str, {
lang, lang,
lineOptions, lineOptions,
theme: getThemeName(theme.light) theme: getThemeName(theme.light)
}) })
), ).replace(classRE, (_, cls) => _.replace(cls, 'vp-code-light'))
'vp-code-light',
'pre'
)
return dark + light return dark + light
} }

Loading…
Cancel
Save