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