Skip to content

fix: markdown-it-attrs behaviour in pre wrapper plugin #3519

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 14 additions & 3 deletions src/node/markdown/plugins/highlightLines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,22 @@ 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 highlightLineAttr
if (token.attrs) {
token.attrs = token.attrs.filter((x) => {
const isHighlightLineAttr = /^[\d,-]+$/.test(x[0])

if (isHighlightLineAttr) {
highlightLineAttr = x
}

return !isHighlightLineAttr
})
}

let lines = null

if (!attr) {
if (!highlightLineAttr) {
// markdown-it-attrs maybe disabled
const rawInfo = token.info

Expand All @@ -35,7 +46,7 @@ export const highlightLinePlugin = (md: MarkdownIt) => {
}

if (!lines) {
lines = attr![0]
lines = highlightLineAttr![0]

if (!lines || !/[\d,-]+/.test(lines)) {
return fence(...args)
Expand Down
19 changes: 14 additions & 5 deletions src/node/markdown/plugins/preWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@ export function preWrapperPlugin(md: MarkdownIt, options: Options) {
token.info = token.info.replace(/ active$/, '').replace(/ active /, ' ')

const lang = extractLang(token.info)

const classes = `language-${lang}${getAdaptiveThemeMarker(
options
)}${active}`
const classAttr = token.attrs && token.attrs.find((x) => x[0] === 'class')

if (classAttr != null) {
classAttr[1] = `${classes} ${classAttr[1]}`
} else {
token.attrJoin('class', classes)
}

const rawCode = fence(...args)
return (
`<div class="language-${lang}${getAdaptiveThemeMarker(options)}${active}">` +
`<div ${md.renderer.renderAttrs(token)}>` +
`<button title="${options.codeCopyButtonTitle}" class="copy"></button>` +
`<span class="lang">${lang}</span>` +
fence(...args) +
'</div>'
`<span class="lang">${lang}</span>${rawCode}</div>`
)
}
}
Expand Down