Skip to content

Commit 4045a8c

Browse files
committed
feat(plugin-git): collect page created time (close #45)
1 parent caa5491 commit 4045a8c

File tree

8 files changed

+81
-5
lines changed

8 files changed

+81
-5
lines changed

docs/reference/plugin/git.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

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

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

2121
## Options
2222

23+
### createdTime
24+
25+
- Type: `boolean`
26+
27+
- Default: `true`
28+
29+
- Details:
30+
31+
Whether to collect page created time or not.
32+
2333
### updatedTime
2434

2535
- Type: `boolean`
@@ -58,6 +68,14 @@ export default {
5868
}
5969
```
6070

71+
### git.createdTime
72+
73+
- Type: `number`
74+
75+
- Details:
76+
77+
Unix timestamp in milliseconds of the first commit of the page.
78+
6179
### git.updatedTime
6280

6381
- Type: `number`

docs/zh/reference/plugin/git.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

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

@@ -20,6 +20,16 @@
2020

2121
## 配置项
2222

23+
### createdTime
24+
25+
- 类型: `boolean`
26+
27+
- 默认值: `true`
28+
29+
- 详情:
30+
31+
是否收集页面的创建时间。
32+
2333
### updatedTime
2434

2535
- 类型: `boolean`
@@ -58,6 +68,14 @@ export default {
5868
}
5969
```
6070

71+
### git.createdTime
72+
73+
- 类型: `number`
74+
75+
- 详情:
76+
77+
页面第一次提交的 Unix 毫秒时间戳。
78+
6179
### git.updatedTime
6280

6381
- 类型: `number`

packages/@vuepress/plugin-git/src/index.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { Plugin } from '@vuepress/core'
2-
import { checkGitRepo, getContributors, getUpdatedTime } from './utils'
2+
import {
3+
checkGitRepo,
4+
getContributors,
5+
getCreatedTime,
6+
getUpdatedTime,
7+
} from './utils'
38
import type { GitData } from './types'
49

510
export * from './types'
@@ -9,6 +14,11 @@ export * from './utils'
914
* Options of @vuepress/plugin-git
1015
*/
1116
export interface GitPluginOptions {
17+
/**
18+
* Whether to get the created time of a page
19+
*/
20+
createdTime?: boolean
21+
1222
/**
1323
* Whether to get the updated time of a page
1424
*/
@@ -21,7 +31,7 @@ export interface GitPluginOptions {
2131
}
2232

2333
export const gitPlugin: Plugin<GitPluginOptions> = (
24-
{ updatedTime, contributors },
34+
{ createdTime, updatedTime, contributors },
2535
app
2636
) => {
2737
const cwd = app.dir.source()
@@ -37,6 +47,10 @@ export const gitPlugin: Plugin<GitPluginOptions> = (
3747
return { git }
3848
}
3949

50+
if (createdTime !== false) {
51+
git.createdTime = await getCreatedTime(page.filePathRelative, cwd)
52+
}
53+
4054
if (updatedTime !== false) {
4155
git.updatedTime = await getUpdatedTime(page.filePathRelative, cwd)
4256
}

packages/@vuepress/plugin-git/src/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ export interface GitPluginPageData {
33
}
44

55
export interface GitData {
6+
/**
7+
* Unix timestamp in milliseconds of the first commit
8+
*/
9+
createdTime?: number
10+
611
/**
712
* Unix timestamp in milliseconds of the last commit
813
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as execa from 'execa'
2+
3+
/**
4+
* Get unix timestamp in milliseconds of the first commit
5+
*/
6+
export const getCreatedTime = async (
7+
filePath: string,
8+
cwd: string
9+
): Promise<number> => {
10+
const { stdout } = await execa(
11+
'git',
12+
['--no-pager', 'log', '--diff-filter=A', '--format=%at', filePath],
13+
{
14+
cwd,
15+
}
16+
)
17+
18+
return Number.parseInt(stdout, 10) * 1000
19+
}

packages/@vuepress/plugin-git/src/utils/getUpdatedTime.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const getUpdatedTime = async (
99
): Promise<number> => {
1010
const { stdout } = await execa(
1111
'git',
12-
['log', '-1', '--format=%at', filePath],
12+
['--no-pager', 'log', '-1', '--format=%at', filePath],
1313
{
1414
cwd,
1515
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './checkGitRepo'
22
export * from './getContributors'
3+
export * from './getCreatedTime'
34
export * from './getUpdatedTime'

packages/@vuepress/theme-default/src/node/resolveGitPluginOptions.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const resolveGitPluginOptions = (
1919
}
2020

2121
return {
22+
createdTime: false,
2223
updatedTime: enableUpdatedTime,
2324
contributors: enableContributors,
2425
}

0 commit comments

Comments
 (0)