Skip to content

Commit

Permalink
fix: customizing the starting line number even if globally set (#2941)
Browse files Browse the repository at this point in the history
  • Loading branch information
mehm8128 committed Sep 16, 2023
1 parent 76e9fe8 commit 0cd87b1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/node/markdown/plugins/highlight.ts
Expand Up @@ -87,13 +87,18 @@ export async function highlight(
const styleRE = /<pre[^>]*(style=".*?")/
const preRE = /^<pre(.*?)>/
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)
Expand Down
2 changes: 1 addition & 1 deletion src/node/markdown/plugins/lineNumbers.ts
Expand Up @@ -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])
}
Expand Down
1 change: 1 addition & 0 deletions src/node/markdown/plugins/preWrapper.ts
Expand Up @@ -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')
Expand Down

0 comments on commit 0cd87b1

Please sign in to comment.