Skip to content

Commit 00eec7a

Browse files
authored
chore: use transformer to clean-up highlight result (#3343)
1 parent 8800195 commit 00eec7a

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

Diff for: src/node/markdown/plugins/highlight.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,20 @@ export async function highlight(
7979
transformerNotationHighlight(),
8080
transformerNotationErrorLevel(),
8181
{
82+
name: 'vitepress:add-class',
8283
pre(node) {
8384
addClassToHast(node, 'vp-code')
8485
}
86+
},
87+
{
88+
name: 'vitepress:clean-up',
89+
pre(node) {
90+
delete node.properties.tabindex
91+
delete node.properties.style
92+
}
8593
}
8694
]
8795

88-
const styleRE = /<pre[^>]*(style=".*?")/
89-
const preRE = /^<pre(.*?)>/
9096
const vueRE = /-vue$/
9197
const lineNoStartRE = /=(\d*)/
9298
const lineNoRE = /:(no-)?line-numbers(=\d*)?$/
@@ -116,16 +122,6 @@ export async function highlight(
116122
}
117123

118124
const lineOptions = attrsToLines(attrs)
119-
const cleanup = (str: string) => {
120-
return str
121-
.replace(
122-
preRE,
123-
(_, attributes) =>
124-
`<pre ${vPre}${attributes.replace(' tabindex="0"', '')}>`
125-
)
126-
.replace(styleRE, (_, style) => _.replace(style, ''))
127-
}
128-
129125
const mustaches = new Map<string, string>()
130126

131127
const removeMustache = (s: string) => {
@@ -161,6 +157,12 @@ export async function highlight(
161157
transformers: [
162158
...transformers,
163159
transformerCompactLineOptions(lineOptions),
160+
{
161+
name: 'vitepress:v-pre',
162+
pre(node) {
163+
if (vPre) node.properties['v-pre'] = ''
164+
}
165+
},
164166
...userTransformers
165167
],
166168
meta: {
@@ -174,6 +176,6 @@ export async function highlight(
174176
})
175177
})
176178

177-
return fillEmptyHighlightedLine(cleanup(restoreMustache(highlighted)))
179+
return fillEmptyHighlightedLine(restoreMustache(highlighted))
178180
}
179181
}

0 commit comments

Comments
 (0)