mirror of https://github.com/vuejs/vitepress
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.3 KiB
49 lines
1.3 KiB
import type { MarkdownItAsync } from 'markdown-it-async'
|
|
|
|
export interface Options {
|
|
codeCopyButtonTitle: string
|
|
}
|
|
|
|
export function preWrapperPlugin(md: MarkdownItAsync, options: Options) {
|
|
const fence = md.renderer.rules.fence!
|
|
md.renderer.rules.fence = (...args) => {
|
|
const [tokens, idx] = args
|
|
const token = tokens[idx]
|
|
|
|
// remove title from info
|
|
token.info = token.info.replace(/\[.*\]/, '')
|
|
|
|
const active = / active( |$)/.test(token.info) ? ' active' : ''
|
|
token.info = token.info.replace(/ active$/, '').replace(/ active /, ' ')
|
|
|
|
const lang = extractLang(token.info)
|
|
|
|
return (
|
|
`<div class="language-${lang}${active}">` +
|
|
`<button title="${options.codeCopyButtonTitle}" class="copy"></button>` +
|
|
`<span class="lang">${lang}</span>` +
|
|
fence(...args) +
|
|
'</div>'
|
|
)
|
|
}
|
|
}
|
|
|
|
export function extractTitle(info: string, html = false) {
|
|
if (html) {
|
|
return (
|
|
info.replace(/<!--[^]*?-->/g, '').match(/data-title="(.*?)"/)?.[1] || ''
|
|
)
|
|
}
|
|
return info.match(/\[(.*)\]/)?.[1] || extractLang(info) || 'txt'
|
|
}
|
|
|
|
function extractLang(info: string) {
|
|
return info
|
|
.trim()
|
|
.replace(/=(\d*)/, '')
|
|
.replace(/:(no-)?line-numbers({| |$|=\d*).*/, '')
|
|
.replace(/(-vue|{| ).*$/, '')
|
|
.replace(/^vue-html$/, 'template')
|
|
.replace(/^ansi$/, '')
|
|
}
|