Skip to content

Commit db58af5

Browse files
authored
fix!: don't remove shiki styles from pre and remove unnecessary transformers (#4652)
BREAKING CHANGE: `vp-adaptive-theme` class is no longer added to code blocks when there is single theme. Theme authors supporting single code theme can use `.shiki:not(.shiki-themes)` as selector. Alternatively, it might be better to use the bg/fg variables set on the `.shiki` block to keep things generic. BREAKING CHANGE: `vp-code` class is no longer added to code blocks. Use `.shiki` or `pre.shiki` or `[class*='language-'] pre` instead. People not customizing their themes are not affected. But if you see weird stuff, you know what to change.
1 parent 531a7a1 commit db58af5

File tree

5 files changed

+16
-57
lines changed

5 files changed

+16
-57
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
.dark .vp-code span {
1+
.dark .shiki span {
22
color: var(--shiki-dark, inherit);
33
}
44

5-
html:not(.dark) .vp-code span {
5+
html:not(.dark) .shiki span {
66
color: var(--shiki-light, inherit);
77
}

src/node/markdown/markdown.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ export async function createMarkdownRenderer(
215215

216216
const theme = options.theme ?? { light: 'github-light', dark: 'github-dark' }
217217
const codeCopyButtonTitle = options.codeCopyButtonTitle || 'Copy Code'
218-
const hasSingleTheme = typeof theme === 'string' || 'name' in theme
219218

220219
let [highlight, dispose] = options.highlight
221220
? [options.highlight, () => {}]
@@ -237,9 +236,9 @@ export async function createMarkdownRenderer(
237236
// custom plugins
238237
md.use(componentPlugin, { ...options.component })
239238
.use(highlightLinePlugin)
240-
.use(preWrapperPlugin, { codeCopyButtonTitle, hasSingleTheme })
239+
.use(preWrapperPlugin, { codeCopyButtonTitle })
241240
.use(snippetPlugin, srcDir)
242-
.use(containerPlugin, { hasSingleTheme }, options.container)
241+
.use(containerPlugin, options.container)
243242
.use(imagePlugin, options.image)
244243
.use(
245244
linkPlugin,

src/node/markdown/plugins/containers.ts

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,17 @@ import container from 'markdown-it-container'
33
import type { RenderRule } from 'markdown-it/lib/renderer.mjs'
44
import type Token from 'markdown-it/lib/token.mjs'
55
import type { MarkdownEnv } from '../../shared'
6-
import {
7-
extractTitle,
8-
getAdaptiveThemeMarker,
9-
type Options
10-
} from './preWrapper'
6+
import { extractTitle } from './preWrapper'
117

128
export const containerPlugin = (
139
md: MarkdownItAsync,
14-
options: Options,
15-
containerOptions?: ContainerOptions
10+
options?: ContainerOptions
1611
) => {
17-
md.use(...createContainer('tip', containerOptions?.tipLabel || 'TIP', md))
18-
.use(...createContainer('info', containerOptions?.infoLabel || 'INFO', md))
19-
.use(
20-
...createContainer(
21-
'warning',
22-
containerOptions?.warningLabel || 'WARNING',
23-
md
24-
)
25-
)
26-
.use(
27-
...createContainer(
28-
'danger',
29-
containerOptions?.dangerLabel || 'DANGER',
30-
md
31-
)
32-
)
33-
.use(
34-
...createContainer(
35-
'details',
36-
containerOptions?.detailsLabel || 'Details',
37-
md
38-
)
39-
)
12+
md.use(...createContainer('tip', options?.tipLabel || 'TIP', md))
13+
.use(...createContainer('info', options?.infoLabel || 'INFO', md))
14+
.use(...createContainer('warning', options?.warningLabel || 'WARNING', md))
15+
.use(...createContainer('danger', options?.dangerLabel || 'DANGER', md))
16+
.use(...createContainer('details', options?.detailsLabel || 'Details', md))
4017
// explicitly escape Vue syntax
4118
.use(container, 'v-pre', {
4219
render: (tokens: Token[], idx: number) =>
@@ -46,7 +23,7 @@ export const containerPlugin = (
4623
render: (tokens: Token[], idx: number) =>
4724
tokens[idx].nesting === 1 ? `<div class="vp-raw">\n` : `</div>\n`
4825
})
49-
.use(...createCodeGroup(options, md))
26+
.use(...createCodeGroup(md))
5027
}
5128

5229
type ContainerArgs = [typeof container, string, { render: RenderRule }]
@@ -80,7 +57,7 @@ function createContainer(
8057
]
8158
}
8259

83-
function createCodeGroup(options: Options, md: MarkdownItAsync): ContainerArgs {
60+
function createCodeGroup(md: MarkdownItAsync): ContainerArgs {
8461
return [
8562
container,
8663
'code-group',
@@ -118,7 +95,7 @@ function createCodeGroup(options: Options, md: MarkdownItAsync): ContainerArgs {
11895
}
11996
}
12097

121-
return `<div class="vp-code-group${getAdaptiveThemeMarker(options)}"><div class="tabs">${tabs}</div><div class="blocks">\n`
98+
return `<div class="vp-code-group"><div class="tabs">${tabs}</div><div class="blocks">\n`
12299
}
123100
return `</div></div>\n`
124101
}

src/node/markdown/plugins/highlight.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,7 @@ export async function highlight(
8080
classActivePre: 'has-focused-lines'
8181
}),
8282
transformerNotationHighlight(),
83-
transformerNotationErrorLevel(),
84-
{
85-
name: 'vitepress:add-class',
86-
pre(node) {
87-
this.addClassToHast(node, 'vp-code')
88-
}
89-
},
90-
{
91-
name: 'vitepress:clean-up',
92-
pre(node) {
93-
delete node.properties.style
94-
}
95-
}
83+
transformerNotationErrorLevel()
9684
]
9785

9886
const vueRE = /-vue(?=:|$)/

src/node/markdown/plugins/preWrapper.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { MarkdownItAsync } from 'markdown-it-async'
22

33
export interface Options {
44
codeCopyButtonTitle: string
5-
hasSingleTheme: boolean
65
}
76

87
export function preWrapperPlugin(md: MarkdownItAsync, options: Options) {
@@ -20,7 +19,7 @@ export function preWrapperPlugin(md: MarkdownItAsync, options: Options) {
2019
const lang = extractLang(token.info)
2120

2221
return (
23-
`<div class="language-${lang}${getAdaptiveThemeMarker(options)}${active}">` +
22+
`<div class="language-${lang}${active}">` +
2423
`<button title="${options.codeCopyButtonTitle}" class="copy"></button>` +
2524
`<span class="lang">${lang}</span>` +
2625
fence(...args) +
@@ -29,10 +28,6 @@ export function preWrapperPlugin(md: MarkdownItAsync, options: Options) {
2928
}
3029
}
3130

32-
export function getAdaptiveThemeMarker(options: Options) {
33-
return options.hasSingleTheme ? '' : ' vp-adaptive-theme'
34-
}
35-
3631
export function extractTitle(info: string, html = false) {
3732
if (html) {
3833
return (

0 commit comments

Comments
 (0)