vue-markdown-shiki
Version:
A Vue 3 component library that provides integration with Shiki and Markdown-it.
49 lines (35 loc) • 1.2 kB
text/typescript
// Modified from https://github.com/egoist/markdown-it-highlight-lines
// Now this plugin is only used to normalize line attrs.
// The else part of line highlights logic is in './highlight.ts'.
import type MarkdownIt from 'markdown-it'
const RE = /{([\d,-]+)}/
export const highlightLinePlugin = (md: MarkdownIt) => {
const fence = md.renderer.rules.fence!
md.renderer.rules.fence = (...args) => {
const [tokens, idx] = args
const token = tokens[idx]
// 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: any = null
if (!attr) {
// 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]
}
if (!lines) {
lines = attr![0]
if (!lines || !/[\d,-]+/.test(lines)) {
return fence(...args)
}
}
token.info += ' ' + lines
return fence(...args)
}
}