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 3 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 leftDelimiter?: string
rightDelimiter?: string rightDelimiter?: string
allowedAttributes?: string[] allowedAttributes?: string[]
disable?: boolean
} }
// markdown-it-toc-done-right plugin options // markdown-it-toc-done-right plugin options

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

@ -1,6 +1,7 @@
// 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) => {
@ -9,17 +10,35 @@ export const highlightLinePlugin = (md: MarkdownIt) => {
const [tokens, idx, options] = args const [tokens, idx, options] = args
const token = tokens[idx] const token = tokens[idx]
// due to use of markdown-it-attrs, the {0} syntax would have been converted // due to use of markdown-it-attrs, the {0} syntax would have been
// to attrs on the token // converted to attrs on the token
const attr = token.attrs && token.attrs[0] const attr = token.attrs && token.attrs[0]
let lines = null
if (!attr) { if (!attr) {
// markdown-it-attrs maybe disabled
const rawInfo = token.info
if (!rawInfo || !RE.test(rawInfo)) {
return fence(...args) return fence(...args)
} }
const lines = attr[0] 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)) { if (!lines || !/[\d,-]+/.test(lines)) {
return fence(...args) return fence(...args)
} }
}
const lineNumbers = lines const lineNumbers = lines
.split(',') .split(',')
@ -30,6 +49,7 @@ export const highlightLinePlugin = (md: MarkdownIt) => {
: token.content : token.content
const rawCode = code.replace(wrapperRE, '') const rawCode = code.replace(wrapperRE, '')
const highlightLinesCode = rawCode const highlightLinesCode = rawCode
.split('\n') .split('\n')
.map((split, index) => { .map((split, index) => {

Loading…
Cancel
Save