From 0cd87b1bafa6158ded0bf741553816f3d9b43a89 Mon Sep 17 00:00:00 2001 From: mehm8128 <83744975+mehm8128@users.noreply.github.com> Date: Sat, 16 Sep 2023 18:26:38 +0900 Subject: [PATCH] fix: customizing the starting line number even if globally set (#2941) --- src/node/markdown/plugins/highlight.ts | 7 ++++++- src/node/markdown/plugins/lineNumbers.ts | 2 +- src/node/markdown/plugins/preWrapper.ts | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/node/markdown/plugins/highlight.ts b/src/node/markdown/plugins/highlight.ts index 07396b8f..e0ddfdb1 100644 --- a/src/node/markdown/plugins/highlight.ts +++ b/src/node/markdown/plugins/highlight.ts @@ -87,13 +87,18 @@ export async function highlight( const styleRE = /]*(style=".*?")/ const preRE = /^/ const vueRE = /-vue$/ + const lineNoStartRE = /=(\d*)/ const lineNoRE = /:(no-)?line-numbers(=\d*)?$/ const mustacheRE = /\{\{.*?\}\}/g return (str: string, lang: string, attrs: string) => { const vPre = vueRE.test(lang) ? '' : 'v-pre' lang = - lang.replace(lineNoRE, '').replace(vueRE, '').toLowerCase() || defaultLang + lang + .replace(lineNoStartRE, '') + .replace(lineNoRE, '') + .replace(vueRE, '') + .toLowerCase() || defaultLang if (lang) { const langLoaded = highlighter.getLoadedLanguages().includes(lang as any) diff --git a/src/node/markdown/plugins/lineNumbers.ts b/src/node/markdown/plugins/lineNumbers.ts index b12e53d6..15fff082 100644 --- a/src/node/markdown/plugins/lineNumbers.ts +++ b/src/node/markdown/plugins/lineNumbers.ts @@ -19,7 +19,7 @@ export const lineNumberPlugin = (md: MarkdownIt, enable = false) => { } let startLineNumber = 1 - const matchStartLineNumber = info.match(/:line-numbers=(\d*)/) + const matchStartLineNumber = info.match(/=(\d*)/) if (matchStartLineNumber && matchStartLineNumber[1]) { startLineNumber = parseInt(matchStartLineNumber[1]) } diff --git a/src/node/markdown/plugins/preWrapper.ts b/src/node/markdown/plugins/preWrapper.ts index 4c9215d1..9e79a4e1 100644 --- a/src/node/markdown/plugins/preWrapper.ts +++ b/src/node/markdown/plugins/preWrapper.ts @@ -40,6 +40,7 @@ export function extractTitle(info: string, html = false) { function extractLang(info: string) { return info .trim() + .replace(/=(\d*)/, '') .replace(/:(no-)?line-numbers({| |$|=\d*).*/, '') .replace(/(-vue|{| ).*$/, '') .replace(/^vue-html$/, 'template')