Skip to content

Commit ed6ada7

Browse files
nozomuikutabrc-dd
andauthoredMay 2, 2024··
feat(build/i18n): support customizing copy code button's tooltip text (#3854)
Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
1 parent 728cb15 commit ed6ada7

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed
 

‎src/node/markdown/markdown.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ export interface MarkdownOptions extends Options {
111111
* Setup Shiki instance
112112
*/
113113
shikiSetup?: (shiki: Highlighter) => void | Promise<void>
114+
/**
115+
* The tooltip text for the copy button in code blocks
116+
* @default 'Copy Code'
117+
*/
118+
codeCopyButtonTitle?: string
114119

115120
/* ==================== Markdown It Plugins ==================== */
116121

@@ -195,6 +200,7 @@ export const createMarkdownRenderer = async (
195200
logger: Pick<Logger, 'warn'> = console
196201
): Promise<MarkdownRenderer> => {
197202
const theme = options.theme ?? { light: 'github-light', dark: 'github-dark' }
203+
const codeCopyButtonTitle = options.codeCopyButtonTitle || 'Copy Code'
198204
const hasSingleTheme = typeof theme === 'string' || 'name' in theme
199205

200206
const md = MarkdownIt({
@@ -214,7 +220,7 @@ export const createMarkdownRenderer = async (
214220
// custom plugins
215221
md.use(componentPlugin, { ...options.component })
216222
.use(highlightLinePlugin)
217-
.use(preWrapperPlugin, { hasSingleTheme })
223+
.use(preWrapperPlugin, { codeCopyButtonTitle, hasSingleTheme })
218224
.use(snippetPlugin, srcDir)
219225
.use(containerPlugin, { hasSingleTheme }, options.container)
220226
.use(imagePlugin, options.image)
@@ -229,7 +235,7 @@ export const createMarkdownRenderer = async (
229235
md.use(gitHubAlertsPlugin)
230236
}
231237

232-
// 3rd party plugins
238+
// third party plugins
233239
if (!options.attrs?.disable) {
234240
md.use(attrsPlugin, options.attrs)
235241
}

‎src/node/markdown/plugins/preWrapper.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type MarkdownIt from 'markdown-it'
22

33
export interface Options {
4+
codeCopyButtonTitle: string
45
hasSingleTheme: boolean
56
}
67

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

1920
const lang = extractLang(token.info)
20-
const rawCode = fence(...args)
21-
return `<div class="language-${lang}${getAdaptiveThemeMarker(
22-
options
23-
)}${active}"><button title="Copy Code" class="copy"></button><span class="lang">${lang}</span>${rawCode}</div>`
21+
22+
return (
23+
`<div class="language-${lang}${getAdaptiveThemeMarker(options)}${active}">` +
24+
`<button title="${options.codeCopyButtonTitle}" class="copy"></button>` +
25+
`<span class="lang">${lang}</span>` +
26+
fence(...args) +
27+
'</div>'
28+
)
2429
}
2530
}
2631

0 commit comments

Comments
 (0)
Please sign in to comment.