Skip to content

Commit

Permalink
feat(plugin-git): collect page created time (close #45)
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Feb 5, 2021
1 parent caa5491 commit 4045a8c
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 5 deletions.
20 changes: 19 additions & 1 deletion docs/reference/plugin/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> @vuepress/plugin-git
This plugin will collect git information of your pages, including the updated time, the contributors, etc.
This plugin will collect git information of your pages, including the created and updated time, the contributors, etc.

The [lastUpdated](../default-theme/config.md#lastupdated) and [contributors](../default-theme/config.md#contributors) of default theme is powered by this plugin.

Expand All @@ -20,6 +20,16 @@ This plugin will significantly slow down the speed of data preparation, especial

## Options

### createdTime

- Type: `boolean`

- Default: `true`

- Details:

Whether to collect page created time or not.

### updatedTime

- Type: `boolean`
Expand Down Expand Up @@ -58,6 +68,14 @@ export default {
}
```

### git.createdTime

- Type: `number`

- Details:

Unix timestamp in milliseconds of the first commit of the page.

### git.updatedTime

- Type: `number`
Expand Down
20 changes: 19 additions & 1 deletion docs/zh/reference/plugin/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> @vuepress/plugin-git
该插件会收集你的页面的 Git 信息,包括更新时间、贡献者等。
该插件会收集你的页面的 Git 信息,包括创建和更新时间、贡献者等。

默认主题的 [lastUpdated](../default-theme/config.md#lastupdated)[contributors](../default-theme/config.md#contributors) 就是由该插件支持的。

Expand All @@ -20,6 +20,16 @@

## 配置项

### createdTime

- 类型: `boolean`

- 默认值: `true`

- 详情:

是否收集页面的创建时间。

### updatedTime

- 类型: `boolean`
Expand Down Expand Up @@ -58,6 +68,14 @@ export default {
}
```

### git.createdTime

- 类型: `number`

- 详情:

页面第一次提交的 Unix 毫秒时间戳。

### git.updatedTime

- 类型: `number`
Expand Down
18 changes: 16 additions & 2 deletions packages/@vuepress/plugin-git/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { Plugin } from '@vuepress/core'
import { checkGitRepo, getContributors, getUpdatedTime } from './utils'
import {
checkGitRepo,
getContributors,
getCreatedTime,
getUpdatedTime,
} from './utils'
import type { GitData } from './types'

export * from './types'
Expand All @@ -9,6 +14,11 @@ export * from './utils'
* Options of @vuepress/plugin-git
*/
export interface GitPluginOptions {
/**
* Whether to get the created time of a page
*/
createdTime?: boolean

/**
* Whether to get the updated time of a page
*/
Expand All @@ -21,7 +31,7 @@ export interface GitPluginOptions {
}

export const gitPlugin: Plugin<GitPluginOptions> = (
{ updatedTime, contributors },
{ createdTime, updatedTime, contributors },
app
) => {
const cwd = app.dir.source()
Expand All @@ -37,6 +47,10 @@ export const gitPlugin: Plugin<GitPluginOptions> = (
return { git }
}

if (createdTime !== false) {
git.createdTime = await getCreatedTime(page.filePathRelative, cwd)
}

if (updatedTime !== false) {
git.updatedTime = await getUpdatedTime(page.filePathRelative, cwd)
}
Expand Down
5 changes: 5 additions & 0 deletions packages/@vuepress/plugin-git/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ export interface GitPluginPageData {
}

export interface GitData {
/**
* Unix timestamp in milliseconds of the first commit
*/
createdTime?: number

/**
* Unix timestamp in milliseconds of the last commit
*/
Expand Down
19 changes: 19 additions & 0 deletions packages/@vuepress/plugin-git/src/utils/getCreatedTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as execa from 'execa'

/**
* Get unix timestamp in milliseconds of the first commit
*/
export const getCreatedTime = async (
filePath: string,
cwd: string
): Promise<number> => {
const { stdout } = await execa(
'git',
['--no-pager', 'log', '--diff-filter=A', '--format=%at', filePath],
{
cwd,
}
)

return Number.parseInt(stdout, 10) * 1000
}
2 changes: 1 addition & 1 deletion packages/@vuepress/plugin-git/src/utils/getUpdatedTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const getUpdatedTime = async (
): Promise<number> => {
const { stdout } = await execa(
'git',
['log', '-1', '--format=%at', filePath],
['--no-pager', 'log', '-1', '--format=%at', filePath],
{
cwd,
}
Expand Down
1 change: 1 addition & 0 deletions packages/@vuepress/plugin-git/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './checkGitRepo'
export * from './getContributors'
export * from './getCreatedTime'
export * from './getUpdatedTime'
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const resolveGitPluginOptions = (
}

return {
createdTime: false,
updatedTime: enableUpdatedTime,
contributors: enableContributors,
}
Expand Down

0 comments on commit 4045a8c

Please sign in to comment.