mirror of
https://github.com/pengzhanbo/vuepress-theme-plume.git
synced 2026-04-23 10:58:13 +08:00
41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import {
|
|
getLocalePaths,
|
|
getRootLangPath,
|
|
isPlainObject,
|
|
} from '@vuepress/helper'
|
|
import { ensureLeadingSlash, resolveLocalePath } from 'vuepress/shared'
|
|
import type { App, LocaleConfig } from 'vuepress'
|
|
import { copyCodeButtonLocales } from './copyCodeButtonLocales.js'
|
|
import type {
|
|
CopyCodeLocaleOptions,
|
|
CopyCodeOptions,
|
|
} from '../types.js'
|
|
|
|
export function createCopyCodeButtonRender(app: App, options?: boolean | CopyCodeOptions): ((filePathRelative: string) => string) | null {
|
|
if (options === false)
|
|
return null
|
|
|
|
const { className = 'copy', locales: userLocales = {} }
|
|
= isPlainObject(options) ? options : {}
|
|
|
|
const root = getRootLangPath(app)
|
|
const locales: LocaleConfig<CopyCodeLocaleOptions> = {
|
|
// fallback locale
|
|
'/': userLocales['/'] || copyCodeButtonLocales[root],
|
|
}
|
|
|
|
getLocalePaths(app).forEach((path) => {
|
|
locales[path]
|
|
= userLocales[path] || copyCodeButtonLocales[path === '/' ? root : path]
|
|
})
|
|
|
|
return (filePathRelative) => {
|
|
const relativePath = ensureLeadingSlash(filePathRelative ?? '')
|
|
const localePath = resolveLocalePath(locales, relativePath)
|
|
|
|
const { title, copied } = locales[localePath] ?? {}
|
|
|
|
return `<button class="${className}" title="${title ?? 'Copy code'}" data-copied="${copied ?? 'Copied'}"></button>`
|
|
}
|
|
}
|