From f9743816a55503c387b9c71793a92eb38817650d Mon Sep 17 00:00:00 2001 From: "Q.Ben Zheng" <40693636+Zhengqbbb@users.noreply.github.com> Date: Wed, 1 Feb 2023 20:11:40 +0800 Subject: [PATCH] feat(shiki): support `ansi` code highlight (#1878) Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> --- src/node/markdown/plugins/highlight.ts | 48 +++++++++----------------- 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/src/node/markdown/plugins/highlight.ts b/src/node/markdown/plugins/highlight.ts index cc031f40..53762d5d 100644 --- a/src/node/markdown/plugins/highlight.ts +++ b/src/node/markdown/plugins/highlight.ts @@ -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 } }