Skip to content

Commit

Permalink
perf: move the <pre> extra attribute to preWrapper (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo authored May 28, 2024
1 parent 97354a2 commit d0bc473
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 50 deletions.
13 changes: 12 additions & 1 deletion plugins/plugin-shiki/src/node/markdown/preWrapperPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import type { Markdown } from 'vuepress/markdown'
import type { PreWrapperOptions } from '../types.js'
import { resolveAttr, resolveLanguage } from '../utils.js'

const PRE_ATTRS_REGEXP = /<pre([\s\S]*?class="[^"]*")([^>]*)>/

export const preWrapperPlugin = (
md: Markdown,
{ preWrapper = true }: PreWrapperOptions = {},
Expand Down Expand Up @@ -46,7 +48,16 @@ export const preWrapperPlugin = (
}

const title = resolveAttr(info, 'title') || lang
let attrs = ''

// before: maybe `v-pre class="shiki *"`
// after: style="*" tab-index="*"
result = result.replace(PRE_ATTRS_REGEXP, (_, before, after) => {
attrs = after.trim()
// Keep `v-pre class="*"`, remove the rest.
return `<pre${before.trimEnd()}>`
})

return `<div class="${languageClass}" data-ext="${lang}" data-title="${title}">${result}</div>`
return `<div class="${languageClass}" data-ext="${lang}" data-title="${title}" ${attrs}>${result}</div>`
}
}
Loading

0 comments on commit d0bc473

Please sign in to comment.