Skip to content

Commit

Permalink
feat: internationalize code block copy button title
Browse files Browse the repository at this point in the history
  • Loading branch information
NozomuIkuta committed May 2, 2024
1 parent 9b2dd50 commit dc5fcd1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/node/markdown/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ export interface MarkdownOptions extends Options {
* @see https://vitepress.dev/guide/markdown#github-flavored-alerts
*/
gfmAlerts?: boolean
/**
* @default 'Copy Code'
*/
codeCopyButtonTitle?: string
}

export type MarkdownRenderer = MarkdownIt
Expand All @@ -196,6 +200,7 @@ export const createMarkdownRenderer = async (
logger: Pick<Logger, 'warn'> = console
): Promise<MarkdownRenderer> => {
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 md = MarkdownIt({
Expand All @@ -215,7 +220,7 @@ export const createMarkdownRenderer = async (
// custom plugins
md.use(componentPlugin, { ...options.component })
.use(highlightLinePlugin)
.use(preWrapperPlugin, { hasSingleTheme })
.use(preWrapperPlugin, { codeCopyButtonTitle, hasSingleTheme })
.use(snippetPlugin, srcDir)
.use(containerPlugin, { hasSingleTheme }, options.container)
.use(imagePlugin, options.image)
Expand Down
4 changes: 3 additions & 1 deletion src/node/markdown/plugins/preWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type MarkdownIt from 'markdown-it'

export interface Options {
codeCopyButtonTitle: string
hasSingleTheme: boolean
}

Expand All @@ -17,10 +18,11 @@ export function preWrapperPlugin(md: MarkdownIt, options: Options) {
token.info = token.info.replace(/ active$/, '').replace(/ active /, ' ')

const lang = extractLang(token.info)
const title = options.codeCopyButtonTitle
const rawCode = fence(...args)
return `<div class="language-${lang}${getAdaptiveThemeMarker(
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>`
}
}

Expand Down

0 comments on commit dc5fcd1

Please sign in to comment.