Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow disabling markdown-it-attrs #664

Merged
merged 2 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/config/app-configs.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ interface MarkdownOptions extends MarkdownIt.Options {
leftDelimiter?: string
rightDelimiter?: string
allowedAttributes?: string[]
disable?: boolean
}

// markdown-it-table-of-contents cplugin options
Expand Down
19 changes: 12 additions & 7 deletions src/node/markdown/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface MarkdownOptions extends MarkdownIt.Options {
leftDelimiter?: string
rightDelimiter?: string
allowedAttributes?: string[]
disable?: boolean
}
theme?: Theme
// https://github.com/Oktavilla/markdown-it-table-of-contents
Expand Down Expand Up @@ -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,
includeLevel: [2, 3],
Expand Down
23 changes: 19 additions & 4 deletions src/node/markdown/plugins/highlightLines.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -12,13 +13,27 @@ export const highlightLinePlugin = (md: MarkdownIt) => {
// 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
brc-dd marked this conversation as resolved.
Show resolved Hide resolved

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
Expand Down