feat: internationalize code block copy button title

pull/3854/head
Nozomu Ikuta 1 year ago
parent 9b2dd504fa
commit dc5fcd15f7
No known key found for this signature in database

@ -185,6 +185,10 @@ export interface MarkdownOptions extends Options {
* @see https://vitepress.dev/guide/markdown#github-flavored-alerts * @see https://vitepress.dev/guide/markdown#github-flavored-alerts
*/ */
gfmAlerts?: boolean gfmAlerts?: boolean
/**
* @default 'Copy Code'
*/
codeCopyButtonTitle?: string
} }
export type MarkdownRenderer = MarkdownIt export type MarkdownRenderer = MarkdownIt
@ -196,6 +200,7 @@ export const createMarkdownRenderer = async (
logger: Pick<Logger, 'warn'> = console logger: Pick<Logger, 'warn'> = console
): Promise<MarkdownRenderer> => { ): Promise<MarkdownRenderer> => {
const theme = options.theme ?? { light: 'github-light', dark: 'github-dark' } const theme = options.theme ?? { light: 'github-light', dark: 'github-dark' }
const codeCopyButtonTitle = options.codeCopyButtonTitle || 'Copy Code'
const hasSingleTheme = typeof theme === 'string' || 'name' in theme const hasSingleTheme = typeof theme === 'string' || 'name' in theme
const md = MarkdownIt({ const md = MarkdownIt({
@ -215,7 +220,7 @@ export const createMarkdownRenderer = async (
// custom plugins // custom plugins
md.use(componentPlugin, { ...options.component }) md.use(componentPlugin, { ...options.component })
.use(highlightLinePlugin) .use(highlightLinePlugin)
.use(preWrapperPlugin, { hasSingleTheme }) .use(preWrapperPlugin, { codeCopyButtonTitle, hasSingleTheme })
.use(snippetPlugin, srcDir) .use(snippetPlugin, srcDir)
.use(containerPlugin, { hasSingleTheme }, options.container) .use(containerPlugin, { hasSingleTheme }, options.container)
.use(imagePlugin, options.image) .use(imagePlugin, options.image)

@ -1,6 +1,7 @@
import type MarkdownIt from 'markdown-it' import type MarkdownIt from 'markdown-it'
export interface Options { export interface Options {
codeCopyButtonTitle: string
hasSingleTheme: boolean hasSingleTheme: boolean
} }
@ -17,10 +18,11 @@ export function preWrapperPlugin(md: MarkdownIt, options: Options) {
token.info = token.info.replace(/ active$/, '').replace(/ active /, ' ') token.info = token.info.replace(/ active$/, '').replace(/ active /, ' ')
const lang = extractLang(token.info) const lang = extractLang(token.info)
const title = options.codeCopyButtonTitle
const rawCode = fence(...args) const rawCode = fence(...args)
return `<div class="language-${lang}${getAdaptiveThemeMarker( return `<div class="language-${lang}${getAdaptiveThemeMarker(
options options
)}${active}"><button title="Copy Code" class="copy"></button><span class="lang">${lang}</span>${rawCode}</div>` )}${active}"><button title="${title}" class="copy"></button><span class="lang">${lang}</span>${rawCode}</div>`
} }
} }

Loading…
Cancel
Save