Skip to content

Commit 178895f

Browse files
authoredOct 27, 2022
fix: properly apply dark/light classes in code blocks (#1546)
1 parent d9fc0b2 commit 178895f

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed
 

‎src/node/markdown/plugins/highlight.ts

+16-24
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
createRangeProcessor,
77
getHighlighter,
88
Processor,
9-
addClass,
109
defineProcessor
1110
} from 'shiki-processor'
1211
import type { ThemeOptions } from '../markdown'
@@ -69,7 +68,8 @@ export async function highlight(
6968
processors
7069
})
7170

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

@@ -93,29 +93,21 @@ export async function highlight(
9393
)
9494
}
9595

96-
const dark = addClass(
97-
cleanup(
98-
highlighter.codeToHtml(str, {
99-
lang,
100-
lineOptions,
101-
theme: getThemeName(theme.dark)
102-
})
103-
),
104-
'vp-code-dark',
105-
'pre'
106-
)
96+
const dark = cleanup(
97+
highlighter.codeToHtml(str, {
98+
lang,
99+
lineOptions,
100+
theme: getThemeName(theme.dark)
101+
})
102+
).replace(classRE, (_, cls) => _.replace(cls, 'vp-code-dark'))
107103

108-
const light = addClass(
109-
cleanup(
110-
highlighter.codeToHtml(str, {
111-
lang,
112-
lineOptions,
113-
theme: getThemeName(theme.light)
114-
})
115-
),
116-
'vp-code-light',
117-
'pre'
118-
)
104+
const light = cleanup(
105+
highlighter.codeToHtml(str, {
106+
lang,
107+
lineOptions,
108+
theme: getThemeName(theme.light)
109+
})
110+
).replace(classRE, (_, cls) => _.replace(cls, 'vp-code-light'))
119111

120112
return dark + light
121113
}

0 commit comments

Comments
 (0)
Please sign in to comment.