feat(shiki): support `ansi` code highlight (#1878)

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/1879/head
Q.Ben Zheng 2 years ago committed by GitHub
parent 9f063426e2
commit f9743816a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -88,7 +88,7 @@ export async function highlight(
if (lang) {
const langLoaded = highlighter.getLoadedLanguages().includes(lang as any)
if (!langLoaded) {
if (!langLoaded && lang !== 'ansi') {
console.warn(
c.yellow(
`The language '${lang}' is not loaded, falling back to '${
@ -127,42 +127,28 @@ export async function highlight(
return s
}
if (hasSingleTheme) {
str = removeMustache(str)
const codeToHtml = (theme: IThemeRegistration) => {
return cleanup(
restoreMustache(
highlighter.codeToHtml(removeMustache(str), {
lang,
lineOptions,
theme: getThemeName(theme)
})
lang === 'ansi'
? highlighter.ansiToHtml(str, {
lineOptions,
theme: getThemeName(theme)
})
: highlighter.codeToHtml(str, {
lang,
lineOptions,
theme: getThemeName(theme)
})
)
)
}
const dark = addClass(
cleanup(
highlighter.codeToHtml(str, {
lang,
lineOptions,
theme: getThemeName(theme.dark)
})
),
'vp-code-dark',
'pre'
)
const light = addClass(
cleanup(
highlighter.codeToHtml(str, {
lang,
lineOptions,
theme: getThemeName(theme.light)
})
),
'vp-code-light',
'pre'
)
if (hasSingleTheme) return codeToHtml(theme)
const dark = addClass(codeToHtml(theme.dark), 'vp-code-dark', 'pre')
const light = addClass(codeToHtml(theme.light), 'vp-code-light', 'pre')
return dark + light
}
}

Loading…
Cancel
Save