From 9708510cbd893f02916c95fabad5a8f07d52dbaf Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 10 Mar 2023 17:57:59 +0800 Subject: [PATCH] fix: fix line higlighting for empty lines --- src/node/markdown/plugins/highlight.ts | 33 +++++++++++++++----------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/node/markdown/plugins/highlight.ts b/src/node/markdown/plugins/highlight.ts index bcbfd414..4860ec79 100644 --- a/src/node/markdown/plugins/highlight.ts +++ b/src/node/markdown/plugins/highlight.ts @@ -136,23 +136,28 @@ export async function highlight( return s } + const fillEmptyHighlightedLine = (s: string) => { + return s.replace( + /()(<\/span>)/g, + '$1$2' + ) + } + str = removeMustache(str) const codeToHtml = (theme: IThemeRegistration) => { - return cleanup( - restoreMustache( - lang === 'ansi' - ? highlighter.ansiToHtml(str, { - lineOptions, - theme: getThemeName(theme) - }) - : highlighter.codeToHtml(str, { - lang, - lineOptions, - theme: getThemeName(theme) - }) - ) - ) + const res = + lang === 'ansi' + ? highlighter.ansiToHtml(str, { + lineOptions, + theme: getThemeName(theme) + }) + : highlighter.codeToHtml(str, { + lang, + lineOptions, + theme: getThemeName(theme) + }) + return fillEmptyHighlightedLine(cleanup(restoreMustache(res))) } if (hasSingleTheme) return codeToHtml(theme)