Skip to content

Commit f974381

Browse files
Zhengqbbbbrc-dd
andauthored
feat(shiki): support ansi code highlight (#1878)
Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
1 parent 9f06342 commit f974381

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

src/node/markdown/plugins/highlight.ts

+17-31
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export async function highlight(
8888

8989
if (lang) {
9090
const langLoaded = highlighter.getLoadedLanguages().includes(lang as any)
91-
if (!langLoaded) {
91+
if (!langLoaded && lang !== 'ansi') {
9292
console.warn(
9393
c.yellow(
9494
`The language '${lang}' is not loaded, falling back to '${
@@ -127,42 +127,28 @@ export async function highlight(
127127
return s
128128
}
129129

130-
if (hasSingleTheme) {
130+
str = removeMustache(str)
131+
132+
const codeToHtml = (theme: IThemeRegistration) => {
131133
return cleanup(
132134
restoreMustache(
133-
highlighter.codeToHtml(removeMustache(str), {
134-
lang,
135-
lineOptions,
136-
theme: getThemeName(theme)
137-
})
135+
lang === 'ansi'
136+
? highlighter.ansiToHtml(str, {
137+
lineOptions,
138+
theme: getThemeName(theme)
139+
})
140+
: highlighter.codeToHtml(str, {
141+
lang,
142+
lineOptions,
143+
theme: getThemeName(theme)
144+
})
138145
)
139146
)
140147
}
141148

142-
const dark = addClass(
143-
cleanup(
144-
highlighter.codeToHtml(str, {
145-
lang,
146-
lineOptions,
147-
theme: getThemeName(theme.dark)
148-
})
149-
),
150-
'vp-code-dark',
151-
'pre'
152-
)
153-
154-
const light = addClass(
155-
cleanup(
156-
highlighter.codeToHtml(str, {
157-
lang,
158-
lineOptions,
159-
theme: getThemeName(theme.light)
160-
})
161-
),
162-
'vp-code-light',
163-
'pre'
164-
)
165-
149+
if (hasSingleTheme) return codeToHtml(theme)
150+
const dark = addClass(codeToHtml(theme.dark), 'vp-code-dark', 'pre')
151+
const light = addClass(codeToHtml(theme.light), 'vp-code-light', 'pre')
166152
return dark + light
167153
}
168154
}

0 commit comments

Comments
 (0)