fix: fix code line hightlighting

close #408
pull/421/head
Evan You 3 years ago
parent 383d8ffbba
commit 4c042b61e7

@ -68,14 +68,8 @@ export const createMarkdownRenderer = (
rel: 'noopener noreferrer', rel: 'noopener noreferrer',
...options.externalLinks ...options.externalLinks
}) })
.use(attrs, {
leftDelimiter: '{',
rightDelimiter: '}',
allowedAttributes: [],
...options.attrs
})
// 3rd party plugins // 3rd party plugins
.use(attrs, options.attrs)
.use(anchor, { .use(anchor, {
slugify, slugify,
permalink: anchor.permalink.ariaHidden({}), permalink: anchor.permalink.ariaHidden({}),

@ -1,7 +1,6 @@
// Modified from https://github.com/egoist/markdown-it-highlight-lines // Modified from https://github.com/egoist/markdown-it-highlight-lines
import MarkdownIt from 'markdown-it' import MarkdownIt from 'markdown-it'
const RE = /{([\d,-]+)}/
const wrapperRE = /^<pre .*?><code>/ const wrapperRE = /^<pre .*?><code>/
export const highlightLinePlugin = (md: MarkdownIt) => { export const highlightLinePlugin = (md: MarkdownIt) => {
@ -10,21 +9,24 @@ export const highlightLinePlugin = (md: MarkdownIt) => {
const [tokens, idx, options] = args const [tokens, idx, options] = args
const token = tokens[idx] const token = tokens[idx]
const rawInfo = token.info // due to use of markdown-it-attrs, the {0} syntax would have been converted
if (!rawInfo || !RE.test(rawInfo)) { // to attrs on the token
const attr = token.attrs && token.attrs[0]
if (!attr) {
return fence(...args) return fence(...args)
} }
const langName = rawInfo.replace(RE, '').trim() const lines = attr[0]
// ensure the next plugin get the correct lang. if (!lines || !/[\d,-]+/.test(lines)) {
token.info = langName return fence(...args)
}
const lineNumbers = RE.exec(rawInfo)![1] const lineNumbers = lines
.split(',') .split(',')
.map((v) => v.split('-').map((v) => parseInt(v, 10))) .map((v) => v.split('-').map((v) => parseInt(v, 10)))
const code = options.highlight const code = options.highlight
? options.highlight(token.content, langName, '') ? options.highlight(token.content, token.info, '')
: token.content : token.content
const rawCode = code.replace(wrapperRE, '') const rawCode = code.replace(wrapperRE, '')

Loading…
Cancel
Save