From d431dc460df18b386dbff9edff27374618edf150 Mon Sep 17 00:00:00 2001 From: Mister-Hope Date: Thu, 12 Sep 2024 19:06:49 +0800 Subject: [PATCH] feat(plugin-git): add `transformContributors` options --- docs/plugins/development/git.md | 12 ++++++++++ docs/zh/plugins/development/git.md | 12 ++++++++++ .../plugin-git/src/node/gitPlugin.ts | 22 ++++++++++++++++--- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/docs/plugins/development/git.md b/docs/plugins/development/git.md index 19de20935a..c578630055 100644 --- a/docs/plugins/development/git.md +++ b/docs/plugins/development/git.md @@ -68,6 +68,18 @@ This plugin will significantly slow down the speed of data preparation, especial Whether to collect page contributors or not. +### transformContributors + +- Type: `(contributors: GitContributor[]) => GitContributor[]` + +- Details: + + A function to transform the contributors. + + The input is the contributors collected by this plugin, and the output should be the transformed contributors. + + You can use it to filter out some contributors, or to sort contributors. + ## Frontmatter ### gitInclude diff --git a/docs/zh/plugins/development/git.md b/docs/zh/plugins/development/git.md index 6a5f123e9f..92962c8993 100644 --- a/docs/zh/plugins/development/git.md +++ b/docs/zh/plugins/development/git.md @@ -68,6 +68,18 @@ export default { 是否收集页面的贡献者。 +### transformContributors + +- 类型: `(contributors: GitContributor[]) => GitContributor[]` + +- 详情: + + 贡献者信息的转换函数。 + + 该函数接收一个贡献者信息数组,返回一个新的贡献者信息数组。 + + 你可以使用该函数来过滤贡献者或排序贡献者。 + ## Frontmatter ### gitInclude diff --git a/plugins/development/plugin-git/src/node/gitPlugin.ts b/plugins/development/plugin-git/src/node/gitPlugin.ts index a27aa0c32c..9fa16e37a7 100644 --- a/plugins/development/plugin-git/src/node/gitPlugin.ts +++ b/plugins/development/plugin-git/src/node/gitPlugin.ts @@ -1,6 +1,10 @@ import type { Page, Plugin } from 'vuepress/core' import { path } from 'vuepress/utils' -import type { GitPluginFrontmatter, GitPluginPageData } from './types.js' +import type { + GitContributor, + GitPluginFrontmatter, + GitPluginPageData, +} from './types.js' import { checkGitRepo, getContributors, @@ -26,10 +30,20 @@ export interface GitPluginOptions { * Whether to get the contributors of a page */ contributors?: boolean + + /** + * Functions to transform contributors, e.g. remove duplicates ones and sort them + */ + transformContributors?: (contributors: GitContributor[]) => GitContributor[] } export const gitPlugin = - ({ createdTime, updatedTime, contributors }: GitPluginOptions = {}): Plugin => + ({ + createdTime, + updatedTime, + contributors, + transformContributors, + }: GitPluginOptions = {}): Plugin => (app) => { const cwd = app.dir.source() const isGitRepoValid = checkGitRepo(cwd) @@ -62,7 +76,9 @@ export const gitPlugin = } if (contributors !== false) { - page.data.git.contributors = await getContributors(filePaths, cwd) + const result = await getContributors(filePaths, cwd) + + page.data.git.contributors = transformContributors?.(result) ?? result } },