mirror of
https://github.com/pengzhanbo/vuepress-theme-plume.git
synced 2026-05-01 12:38:12 +08:00
29 lines
917 B
TypeScript
29 lines
917 B
TypeScript
import type { App } from 'vuepress'
|
|
import type { Markdown, MarkdownEnv } from 'vuepress/markdown'
|
|
import { createCopyCodeButtonRender } from './createCopyCodeButtonRender.js'
|
|
import type { CopyCodeOptions } from '../types.js'
|
|
|
|
/**
|
|
* This plugin should work after `preWrapperPlugin`,
|
|
* and if `preWrapper` is disabled, this plugin should not be called either.
|
|
*/
|
|
export function copyCodeButtonPlugin(md: Markdown, app: App, options?: boolean | CopyCodeOptions): void {
|
|
const render = createCopyCodeButtonRender(app, options)
|
|
|
|
if (!render)
|
|
return
|
|
|
|
const fence = md.renderer.rules.fence!
|
|
|
|
md.renderer.rules.fence = (...args) => {
|
|
const [, , , env] = args
|
|
|
|
const result = fence(...args)
|
|
const { filePathRelative } = env as MarkdownEnv
|
|
// resolve copy code button
|
|
const copyCodeButton = render(filePathRelative ?? '')
|
|
|
|
return result.replace('><pre', `>${copyCodeButton}<pre`)
|
|
}
|
|
}
|