Skip to content

Commit d9b290b

Browse files
authored
feat: generate the timestamp of last updated for each doc (close #258) (#282)
1 parent fd547ea commit d9b290b

File tree

6 files changed

+44
-4
lines changed

6 files changed

+44
-4
lines changed

docs/guide/custom-themes.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This is the value of `$site` of this very website:
2828
"base": "/",
2929
"pages": [
3030
{
31+
"lastModified": 1524027677,
3132
"path": "/",
3233
"title": "VuePress",
3334
"frontmatter": {}
@@ -43,7 +44,8 @@ This is the `$page` object for this page you are looking at:
4344

4445
``` json
4546
{
46-
"path": "/custom-themes.html",
47+
"lastModified": 1524847549,
48+
"path": "/guide/custom-themes.html",
4749
"title": "Custom Themes",
4850
"headers": [/* ... */],
4951
"frontmatter": {}
@@ -54,6 +56,10 @@ If the user provided `themeConfig` in `.vuepress/config.js`, it will also be ava
5456

5557
Finally, don't forget that `this.$route` and `this.$router` are also available as part of Vue Router's API.
5658

59+
::: tip
60+
`lastModified` is the UNIX timestamp of this file's last git commit, so please ensure you have git installed.
61+
:::
62+
5763
## Content Excerpt
5864

5965
If a markdown file contains a `<!-- more -->` comment, any content above the comment will be extracted and exposed as `$page.excerpt`. If you are building custom theme for blogging, this data can be used to render a post list with excerpts.

docs/zh/guide/custom-themes.md

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自
2828
"base": "/",
2929
"pages": [
3030
{
31+
"lastModified": 1524027677,
3132
"path": "/",
3233
"title": "VuePress",
3334
"frontmatter": {}
@@ -43,6 +44,7 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自
4344

4445
``` json
4546
{
47+
"lastModified": 1524847549,
4648
"path": "/custom-themes.html",
4749
"title": "自定义主题",
4850
"headers": [/* ... */],
@@ -54,6 +56,10 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自
5456

5557
最后,别忘了,作为 Vue Router API 的一部分,`this.$route``this.$router` 同样可以使用。
5658

59+
::: tip 提示
60+
`lastModified` 是这个文件最后一次 git 提交的 unix 时间戳,所以请确保你已经安装了 git。
61+
:::
62+
5763
## 内容摘抄
5864

5965
如果一个 markdown 文件中有一个 `<!-- more -->` 注释,则该注释之前的内容会被抓取并暴露在 `$page.excerpt` 属性中。如果你在开发一个博客主题,你可以用这个属性来渲染一个带摘抄的文章列表。

lib/prepare.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ const yamlParser = require('js-yaml')
55
const tomlParser = require('toml')
66
const createMarkdown = require('./markdown')
77
const tempPath = path.resolve(__dirname, 'app/.temp')
8-
const { inferTitle, extractHeaders, parseFrontmatter } = require('./util')
8+
const {
9+
inferTitle,
10+
extractHeaders,
11+
parseFrontmatter,
12+
getGitLastModifiedTimeStamp
13+
} = require('./util')
914

1015
fs.ensureDirSync(tempPath)
1116

@@ -178,12 +183,15 @@ async function resolveOptions (sourceDir) {
178183

179184
// resolve pagesData
180185
const pagesData = await Promise.all(pageFiles.map(async (file) => {
186+
const filepath = path.resolve(sourceDir, file)
187+
const lastModified = getGitLastModifiedTimeStamp(filepath)
181188
const data = {
189+
lastModified,
182190
path: fileToPath(file)
183191
}
184192

185193
// extract yaml frontmatter
186-
const content = await fs.readFile(path.resolve(sourceDir, file), 'utf-8')
194+
const content = await fs.readFile(filepath, 'utf-8')
187195
const frontmatter = parseFrontmatter(content)
188196
// infer title
189197
const title = inferTitle(frontmatter)

lib/util/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const spawn = require('cross-spawn')
12
const parseHeaders = require('./parseHeaders')
23

34
exports.normalizeHeadTag = tag => {
@@ -81,3 +82,7 @@ exports.extractHeaders = (content, include = [], md) => {
8182
cache.set(key, res)
8283
return res
8384
}
85+
86+
exports.getGitLastModifiedTimeStamp = filepath => {
87+
return parseInt(spawn.sync('git', ['log', '-1', '--format=%ct', filepath]).stdout.toString('utf-8'))
88+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"commander": "^2.15.1",
4646
"connect-history-api-fallback": "^1.5.0",
4747
"copy-webpack-plugin": "^4.5.1",
48+
"cross-spawn": "^6.0.5",
4849
"css-loader": "^0.28.11",
4950
"diacritics": "^1.3.0",
5051
"docsearch.js": "^2.5.2",

yarn.lock

+15-1
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,16 @@ cross-spawn@^5.0.1, cross-spawn@^5.1.0:
13741374
shebang-command "^1.2.0"
13751375
which "^1.2.9"
13761376

1377+
cross-spawn@^6.0.5:
1378+
version "6.0.5"
1379+
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4"
1380+
dependencies:
1381+
nice-try "^1.0.4"
1382+
path-key "^2.0.1"
1383+
semver "^5.5.0"
1384+
shebang-command "^1.2.0"
1385+
which "^1.2.9"
1386+
13771387
13781388
version "2.0.5"
13791389
resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8"
@@ -4284,6 +4294,10 @@ neo-async@^2.5.0:
42844294
version "2.5.1"
42854295
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.5.1.tgz#acb909e327b1e87ec9ef15f41b8a269512ad41ee"
42864296

4297+
nice-try@^1.0.4:
4298+
version "1.0.4"
4299+
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.4.tgz#d93962f6c52f2c1558c0fbda6d512819f1efe1c4"
4300+
42874301
no-case@^2.2.0:
42884302
version "2.3.2"
42894303
resolved "https://registry.yarnpkg.com/no-case/-/no-case-2.3.2.tgz#60b813396be39b3f1288a4c1ed5d1e7d28b464ac"
@@ -4686,7 +4700,7 @@ path-is-inside@^1.0.1, path-is-inside@^1.0.2:
46864700
version "1.0.2"
46874701
resolved "https://registry.yarnpkg.com/path-is-inside/-/path-is-inside-1.0.2.tgz#365417dede44430d1c11af61027facf074bdfc53"
46884702

4689-
path-key@^2.0.0:
4703+
path-key@^2.0.0, path-key@^2.0.1:
46904704
version "2.0.1"
46914705
resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40"
46924706

0 commit comments

Comments
 (0)