Skip to content

Commit b74872b

Browse files
authored
fix(plugin-shiki): fix incorrect highlight lines parsing, close #555 (#557)
1 parent 0eadb43 commit b74872b

File tree

6 files changed

+92
-42
lines changed

6 files changed

+92
-42
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Modified from https://github.com/egoist/markdown-it-highlight-lines
2+
// Now this plugin is only used to normalize line attrs.
3+
// The else part of line highlights logic is in './highlight.ts'.
4+
5+
import type { Markdown } from 'vuepress/markdown'
6+
7+
const HIGHLIGHT_LINES_REGEXP = /{([\d,-]+)}/
8+
9+
export const highlightLinePlugin = (md: Markdown): void => {
10+
const fence = md.renderer.rules.fence!
11+
md.renderer.rules.fence = (...args) => {
12+
const [tokens, idx] = args
13+
const token = tokens[idx]
14+
15+
// due to use of markdown-it-attrs, the {0} syntax would have been
16+
// converted to attrs on the token
17+
const attr = token.attrs?.[0]
18+
19+
let lines: string | null = null
20+
21+
if (!attr) {
22+
// markdown-it-attrs maybe disabled
23+
const rawInfo = token.info
24+
25+
if (!rawInfo || !HIGHLIGHT_LINES_REGEXP.test(rawInfo)) {
26+
return fence(...args)
27+
}
28+
29+
const langName = rawInfo.replace(HIGHLIGHT_LINES_REGEXP, '').trim()
30+
31+
// ensure the next plugin get the correct lang
32+
token.info = langName
33+
34+
lines = HIGHLIGHT_LINES_REGEXP.exec(rawInfo)![1]
35+
}
36+
37+
if (!lines) {
38+
lines = attr![0]
39+
40+
if (!lines || !/[\d,-]+/.test(lines)) {
41+
return fence(...args)
42+
}
43+
}
44+
45+
token.info += ` ${lines}`
46+
return fence(...args)
47+
}
48+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './highlighter/index.js'
22
export * from './preWrapperPlugin.js'
3+
export * from './highlightLinesPlugin.js'

plugins/markdown/plugin-shiki/src/node/shikiPlugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { MarkdownItPreWrapperOptions } from './markdown/index.js'
1717
import {
1818
createShikiHighlighter,
1919
getHighLightFunction,
20+
highlightLinePlugin,
2021
preWrapperPlugin,
2122
} from './markdown/index.js'
2223
import type { ShikiPluginOptions } from './options.js'
@@ -112,7 +113,7 @@ export const shikiPlugin = (options: ShikiPluginOptions = {}): Plugin => {
112113
loadLang,
113114
markdownFilePathGetter,
114115
)
115-
116+
md.use(highlightLinePlugin)
116117
md.use<MarkdownItPreWrapperOptions>(preWrapperPlugin, { preWrapper })
117118
if (preWrapper) {
118119
md.use<MarkdownItLineNumbersOptions>(lineNumbersPlugin, {

plugins/markdown/plugin-shiki/src/node/utils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ export const resolveLanguage = (info: string): string =>
4747
* @returns Line options array / 行选项数组
4848
*/
4949
export const attrsToLines = (attrs: string): TransformerCompactLineOption[] => {
50-
const attrsContent = attrs
51-
.replace(/^(?:\[.*?\])?.*?\{([\d,-]+)\}.*/, '$1')
52-
.trim()
50+
const attrsContent = attrs.replace(/^(?:\[.*?\])?.*?([\d,-]+).*/, '$1').trim()
5351
const result: number[] = []
5452

5553
if (!attrsContent) {

0 commit comments

Comments
 (0)