diff --git a/theme/src/node/setupPages.ts b/theme/src/node/setupPages.ts index 566cbb9a..45526f0a 100644 --- a/theme/src/node/setupPages.ts +++ b/theme/src/node/setupPages.ts @@ -6,7 +6,7 @@ import type { PlumeThemeLocaleOptions, PlumeThemePageData, } from '../shared/index.js' -import { normalizePath } from './utils.js' +import { pathJoin } from './utils.js' export async function setupPage( app: App, @@ -18,7 +18,7 @@ export async function setupPage( const defaultBlog = localeOption.blog const link = blog?.link ? blog.link - : normalizePath(path.join('/', locale, defaultBlog?.link || '/blog/')) + : pathJoin('/', locale, defaultBlog?.link || '/blog/') const blogPage = await createPage(app, { path: link, frontmatter: { @@ -30,7 +30,7 @@ export async function setupPage( if (blog?.tags !== false || defaultBlog?.tags !== false) { const tagsPage = await createPage(app, { - path: normalizePath(path.join(link, 'tags/')), + path: pathJoin(link, 'tags/'), frontmatter: { lang: locale.replace(/^\/|\/$/g, '') || app.siteData.lang, type: 'blog-tags', @@ -41,7 +41,7 @@ export async function setupPage( if (blog?.archives !== false || defaultBlog?.archives !== false) { const archivesPage = await createPage(app, { - path: normalizePath(path.join(link, 'archives/')), + path: pathJoin(link, 'archives/'), frontmatter: { lang: locale.replace(/^\/|\/$/g, '') || app.siteData.lang, type: 'blog-archives', diff --git a/theme/src/node/theme.ts b/theme/src/node/theme.ts index 27a215ae..5308a2a7 100644 --- a/theme/src/node/theme.ts +++ b/theme/src/node/theme.ts @@ -1,5 +1,5 @@ import type { App, Page, Theme } from '@vuepress/core' -import { getDirname, path } from '@vuepress/utils' +import { fs, getDirname, path, templateRenderer } from '@vuepress/utils' import type { PlumeThemeOptions, PlumeThemePageData } from '../shared/index.js' import { mergeLocaleOptions } from './defaultOptions.js' import { setupPlugins } from './plugins.js' @@ -10,20 +10,38 @@ const name = 'vuepress-theme-plume' const resolve = (...args: string[]) => path.resolve(__dirname, '../', ...args) const templates = (url: string) => resolve('../templates', url) +function getThemePackage() { + let pkg = {} as any + try { + const content = fs.readFileSync(resolve('../package.json'), 'utf-8') + pkg = JSON.parse(content) + } + catch {} + return pkg +} + export function plumeTheme({ themePlugins = {}, ...localeOptions }: PlumeThemeOptions = {}): Theme { localeOptions = mergeLocaleOptions(localeOptions) + const pkg = getThemePackage() return (app: App) => { return { name, templateBuild: templates('build.html'), clientConfigFile: resolve('client/config.js'), plugins: setupPlugins(app, themePlugins, localeOptions), - onInitialized: async app => await setupPage(app, localeOptions), + onInitialized: app => setupPage(app, localeOptions), extendsPage: (page: Page) => extendsPageData(app, page, localeOptions), + templateBuildRenderer(template, context) { + template = template + .replace('{{ themeVersion }}', pkg.version || '') + .replace(/^\s+|\s+$/gm, '') + .replace(/\n/g, '') + return templateRenderer(template, context) + }, } } } diff --git a/theme/templates/build.html b/theme/templates/build.html index c000f8cc..c804a1d0 100644 --- a/theme/templates/build.html +++ b/theme/templates/build.html @@ -4,11 +4,12 @@ +