Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Oct 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 16 additions & 24 deletions src/node/markdown/plugins/highlight.ts
Expand Up @@ -6,7 +6,6 @@ import {
createRangeProcessor,
getHighlighter,
Processor,
addClass,
defineProcessor
} from 'shiki-processor'
import type { ThemeOptions } from '../markdown'
Expand Down Expand Up @@ -69,7 +68,8 @@ export async function highlight(
processors
})

const styleRE = /<pre .* (style=".*")><code>/
const classRE = /<pre[^>]*class="(.*?)"/
const styleRE = /<pre[^>]*(style=".*?")/
const preRE = /^<pre(.*?)>/
const vueRE = /-vue$/

Expand All @@ -93,29 +93,21 @@ export async function highlight(
)
}

const dark = addClass(
cleanup(
highlighter.codeToHtml(str, {
lang,
lineOptions,
theme: getThemeName(theme.dark)
})
),
'vp-code-dark',
'pre'
)
const dark = cleanup(
highlighter.codeToHtml(str, {
lang,
lineOptions,
theme: getThemeName(theme.dark)
})
).replace(classRE, (_, cls) => _.replace(cls, 'vp-code-dark'))

const light = addClass(
cleanup(
highlighter.codeToHtml(str, {
lang,
lineOptions,
theme: getThemeName(theme.light)
})
),
'vp-code-light',
'pre'
)
const light = cleanup(
highlighter.codeToHtml(str, {
lang,
lineOptions,
theme: getThemeName(theme.light)
})
).replace(classRE, (_, cls) => _.replace(cls, 'vp-code-light'))

return dark + light
}
Expand Down