From 2c9006caf7d6509276cdb11b993d43cd34125bc5 Mon Sep 17 00:00:00 2001 From: pengzhanbo Date: Mon, 12 May 2025 09:06:44 +0800 Subject: [PATCH] fix(theme): fix git-plugin execute error when `git: false` (#591) --- docs/.vuepress/theme.ts | 2 -- theme/src/node/plugins/git.ts | 42 ++++++++++++++++++++++++++ theme/src/node/plugins/setupPlugins.ts | 25 ++------------- 3 files changed, 44 insertions(+), 25 deletions(-) create mode 100644 theme/src/node/plugins/git.ts diff --git a/docs/.vuepress/theme.ts b/docs/.vuepress/theme.ts index 5e2b56b9..38a893ce 100644 --- a/docs/.vuepress/theme.ts +++ b/docs/.vuepress/theme.ts @@ -73,6 +73,4 @@ export const theme: Theme = plumeTheme({ content: 'vuepress-theme-plume', }, }, - - // plugins: { git: true }, }) diff --git a/theme/src/node/plugins/git.ts b/theme/src/node/plugins/git.ts new file mode 100644 index 00000000..87f506d3 --- /dev/null +++ b/theme/src/node/plugins/git.ts @@ -0,0 +1,42 @@ +import type { App, PluginConfig } from 'vuepress' +import type { ThemeBuiltinPlugins } from '../../shared/index.js' +import { isPlainObject } from '@vuepress/helper' +import { gitPlugin as rawGitPlugin } from '@vuepress/plugin-git' +import { getThemeConfig } from '../loadConfig/index.js' + +export function gitPlugin(app: App, pluginOptions: ThemeBuiltinPlugins): PluginConfig { + const options = getThemeConfig() + + const git = pluginOptions.git ?? app.env.isBuild + + if (!git) { + // disable all git features + return [rawGitPlugin({ + createdTime: false, + updatedTime: false, + contributors: false, + changelog: false, + })] + } + + const excludes = ['home', 'friends', 'page', 'custom', false] + const changelogOptions = isPlainObject(options.changelog) ? options.changelog : {} + + return [rawGitPlugin({ + updatedTime: options.lastUpdated !== false, + contributors: isPlainObject(options.contributors) || options.contributors === true + ? { + avatar: true, + ...options.contributors === true ? {} : options.contributors, + } + : false, + changelog: options.changelog && options.docsRepo + ? { repoUrl: options.docsRepo, ...changelogOptions } + : options.changelog, + filter(page) { + if (page.frontmatter.home || excludes.includes(page.frontmatter.pageLayout as string)) + return false + return true + }, + })] +} diff --git a/theme/src/node/plugins/setupPlugins.ts b/theme/src/node/plugins/setupPlugins.ts index 6a6c080b..82c27e1e 100644 --- a/theme/src/node/plugins/setupPlugins.ts +++ b/theme/src/node/plugins/setupPlugins.ts @@ -8,7 +8,6 @@ import { isPlainObject } from '@vuepress/helper' import { cachePlugin } from '@vuepress/plugin-cache' import { commentPlugin } from '@vuepress/plugin-comment' import { docsearchPlugin } from '@vuepress/plugin-docsearch' -import { gitPlugin } from '@vuepress/plugin-git' import { nprogressPlugin } from '@vuepress/plugin-nprogress' import { photoSwipePlugin } from '@vuepress/plugin-photo-swipe' import { readingTimePlugin } from '@vuepress/plugin-reading-time' @@ -18,6 +17,7 @@ import { watermarkPlugin } from '@vuepress/plugin-watermark' import { replaceAssetsPlugin } from 'vuepress-plugin-replace-assets' import { getThemeConfig } from '../loadConfig/index.js' import { codePlugins } from './code.js' +import { gitPlugin } from './git.js' import { markdownPlugins } from './markdown.js' export function setupPlugins( @@ -103,28 +103,7 @@ export function setupPlugins( * 2. 贡献者列表 * 3. 更新日志 */ - if (pluginOptions.git ?? isProd) { - const excludes = ['home', 'friends', 'page', 'custom', false] - const changelogOptions = isPlainObject(options.changelog) ? options.changelog : {} - plugins.push(gitPlugin({ - createdTime: false, - updatedTime: options.lastUpdated !== false, - contributors: isPlainObject(options.contributors) || options.contributors === true - ? { - avatar: true, - ...options.contributors === true ? {} : options.contributors, - } - : false, - changelog: options.changelog && options.docsRepo - ? { repoUrl: options.docsRepo, ...changelogOptions } - : options.changelog, - filter(page) { - if (page.frontmatter.home || excludes.includes(page.frontmatter.pageLayout as string)) - return false - return true - }, - })) - } + plugins.push(...gitPlugin(app, pluginOptions)) /** * 资源替换