Skip to content

Commit

Permalink
feat(plugin-git): add transformContributors options
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Sep 12, 2024
1 parent 04801e4 commit d431dc4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
12 changes: 12 additions & 0 deletions docs/plugins/development/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions docs/zh/plugins/development/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ export default {

是否收集页面的贡献者。

### transformContributors

- 类型: `(contributors: GitContributor[]) => GitContributor[]`

- 详情:

贡献者信息的转换函数。

该函数接收一个贡献者信息数组,返回一个新的贡献者信息数组。

你可以使用该函数来过滤贡献者或排序贡献者。

## Frontmatter

### gitInclude
Expand Down
22 changes: 19 additions & 3 deletions plugins/development/plugin-git/src/node/gitPlugin.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
},

Expand Down

0 comments on commit d431dc4

Please sign in to comment.