Skip to content

Commit

Permalink
feat(git-changelog): add authors for specific page (#273)
Browse files Browse the repository at this point in the history
* fix: contributors order
* feat: add authors for specific page
* fix: clean code
* fix: validate `pageData.value.frontmatter.authors` is string[]
  • Loading branch information
northword authored Jul 21, 2024
1 parent 59543b2 commit 6a97f60
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,24 @@ interface Locale {

The Git-based page histories plugin provides accessibility support by default. You can override accessible labels (aria-label) via [Configuration](#configuration) in the same way as [Internationalization](#internationalization), see [Locales Options](#locales-options) for a description of what labels can be configured for accessibility.

## Adding Contributors for Specific Pages

In certain situations, Git records may miss some contributors. To address this issue, we provide a Front Matter key that allows you to supplement author information for specific pages.

You can add missing contributor information in the Front Matter of the Markdown file using the following format:

```md
---
authors: ['author1', 'author2']
---

<!-- body -->
```

These contributors will be merged with the authors retrieved from Git.

Please note that the contributors specified here will not go through the `mapAuthorsByNameAlias` data mapping of the [Vite plugin `mapAuthors` option](./configure-vite-plugins.md#option-mapauthors-map-contributors-information). Therefore, you need to use the `name` attribute values from the `mapAuthors` array for each author. Otherwise, the contributor will be considered an independent author.

## More customizations?

It is possible though.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,24 @@ interface Locale {

基于 Git 的页面历史插件默认提供了无障碍的支持,你可以通过 [配置](#配置选项) 来对无障碍的文案进行复写,使用方法和 [国际化](#国际化) 一样,有关无障碍有哪些文案可以配置,请参阅 [国际化字段选项](#国际化字段选项)

## 为特定页面增补贡献者

在某些情况下,Git 记录中可能会遗漏部分贡献者信息。为了解决这个问题,我们提供了一个 Front Matter 键,允许你为特定页面补充作者信息。

你可以在 Markdown 文件的 Front Matter 中添加缺失的贡献者信息,格式如下:

```md
---
authors: ['author1', 'author2']
---

<!-- body-->
```

这些贡献者信息将会与从 Git 中获取到的作者信息合并显示。

主要注意的是,此处指定的贡献者将不会经过 [Vite 插件 `mapAuthors` 选项](./configure-vite-plugins.md#选项-mapcontributors-为贡献者添加数据映射)`mapAuthorsByNameAlias` 数据映射,因此你需要填写 `mapAuthors` 数组中每个作者信息 `name` 属性的值,否则,该贡献者将视为一个独立的作者。

## 更多自定义能力?

可以的,没问题。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type Ref, computed, ref, toValue } from 'vue'
import type { PageData } from 'vitepress'
import changelog from 'virtual:nolebase-git-changelog'
import type { Changelog, Commit, CommitAuthor } from '../../types'
import { isStringArray } from '../utils'

export interface AuthorInfo extends CommitAuthor {
commitsCount: number
Expand Down Expand Up @@ -31,7 +32,11 @@ export function useChangelog(pageData: Ref<PageData>) {
const authors = computed(() => {
const uniq = new Map<string, AuthorInfo>()

commits.value.map(c => c.authors)
const authorsFromFrontMatter: string[] = isStringArray(pageData.value.frontmatter.authors)
? pageData.value.frontmatter.authors
: [];

[...commits.value.map(c => c.authors), ...authorsFromFrontMatter]
.flat()
.map((name) => {
if (!uniq.has(name)) {
Expand All @@ -48,12 +53,14 @@ export function useChangelog(pageData: Ref<PageData>) {
})

return Array.from(uniq.values())
.sort((a, b) => a.commitsCount - b.commitsCount)
.sort((a, b) => b.commitsCount - a.commitsCount)
.map((a) => {
return {
...a,
...gitChangelog.value.authors.find(item => item.name === a.name),

...gitChangelog.value.authors.find(item => item.name === a.name) ?? {
// a avatarUrl fallback for authors in frontmatter
avatarUrl: `https://gravatar.com/avatar/${a.name}?d=retro`,
},
}
})
})
Expand Down
4 changes: 4 additions & 0 deletions packages/vitepress-plugin-git-changelog/src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export function formatDistanceToNowFromValue(value: Date, localeName = 'enUS') {
}
}

export function isStringArray(value: any): value is string[] {
return Array.isArray(value) && value.every(item => typeof item === 'string')
}

/**
* Hashes a string using SHA-256
*
Expand Down

0 comments on commit 6a97f60

Please sign in to comment.