feat: allow disabling `markdown-it-attrs` (#662) (#664)

close #664

Co-authored-by: Kia King Ishii <kia.king.08@gmail.com>
pull/672/head
Divyansh Singh 2 years ago committed by GitHub
parent a0f81c9f9d
commit b79c66d79c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -119,6 +119,7 @@ interface MarkdownOptions extends MarkdownIt.Options {
leftDelimiter?: string
rightDelimiter?: string
allowedAttributes?: string[]
disable?: boolean
}
// markdown-it-toc-done-right plugin options

@ -29,6 +29,7 @@ export interface MarkdownOptions extends MarkdownIt.Options {
leftDelimiter?: string
rightDelimiter?: string
allowedAttributes?: string[]
disable?: boolean
}
theme?: Theme
// https://github.com/nagaozen/markdown-it-toc-done-right
@ -80,13 +81,17 @@ export const createMarkdownRenderer = async (
},
base
)
// 3rd party plugins
.use(attrs, options.attrs)
.use(anchor, {
slugify,
permalink: anchor.permalink.ariaHidden({}),
...options.anchor
})
// 3rd party plugins
if (!options.attrs?.disable) {
md.use(attrs, options.attrs)
}
md.use(anchor, {
slugify,
permalink: anchor.permalink.ariaHidden({}),
...options.anchor
})
.use(toc, {
slugify,
level: [2, 3],

@ -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) => {

Loading…
Cancel
Save