Skip to content

Commit 272df57

Browse files
authored
feat: last updated UI in default theme. (#338)
1 parent c65a8b7 commit 272df57

File tree

12 files changed

+102
-18
lines changed

12 files changed

+102
-18
lines changed

docs/.vuepress/config.js

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = {
3434
label: 'English',
3535
selectText: 'Languages',
3636
editLinkText: 'Edit this page on GitHub',
37+
lastUpdated: 'Last Updated',
3738
nav: [
3839
{
3940
text: 'Guide',
@@ -56,6 +57,7 @@ module.exports = {
5657
label: '简体中文',
5758
selectText: '选择语言',
5859
editLinkText: '在 GitHub 上编辑此页',
60+
lastUpdated: '上次更新',
5961
nav: [
6062
{
6163
text: '指南',

docs/default-theme-config/README.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ Built-in Search only builds index from the title, `h2` and `h3` headers, if you
267267

268268
The `themeConfig.algolia` option allows you to use [Algolia DocSearch](https://community.algolia.com/docsearch/) to replace the simple built-in search. To enable it, you need to provide at least `apiKey` and `indexName`:
269269

270-
```js
270+
``` js
271271
module.exports = {
272272
themeConfig: {
273273
algolia: {
@@ -280,6 +280,24 @@ module.exports = {
280280

281281
For more options, refer to [Algolia DocSearch's documentation](https://github.com/algolia/docsearch#docsearch-options).
282282

283+
## Last Updated
284+
285+
The `themeConfig.lastUpdated` option allows you to get the UNIX timestamp(ms) of each file's last `git` commit, and it will also display at the bottom of each page with a appropriate format:
286+
287+
``` js
288+
module.exports = {
289+
themeConfig: {
290+
lastUpdated: 'Last Updated', // string | boolean
291+
}
292+
}
293+
```
294+
295+
Note that it's `off` by default. If given `string`, it will be displayed as a prefix (default value: `Last Updated`).
296+
297+
::: warning
298+
Since `lastUpdated` is based on `git`, so you can only use it in a `git` repository.
299+
:::
300+
283301
## Prev / Next Links
284302

285303
Prev and next links are automatically inferred based on the sidebar order of the active page. You can also explicitly overwrite or disable them using `YAML front matter`:

docs/guide/custom-themes.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This is the value of `$site` of this very website:
2828
"base": "/",
2929
"pages": [
3030
{
31-
"lastModified": 1524027677,
31+
"lastUpdated": 1524027677000,
3232
"path": "/",
3333
"title": "VuePress",
3434
"frontmatter": {}
@@ -44,7 +44,7 @@ This is the `$page` object for this page you are looking at:
4444

4545
``` json
4646
{
47-
"lastModified": 1524847549,
47+
"lastUpdated": 1524847549000,
4848
"path": "/guide/custom-themes.html",
4949
"title": "Custom Themes",
5050
"headers": [/* ... */],
@@ -57,7 +57,8 @@ If the user provided `themeConfig` in `.vuepress/config.js`, it will also be ava
5757
Finally, don't forget that `this.$route` and `this.$router` are also available as part of Vue Router's API.
5858

5959
::: tip
60-
`lastModified` is the UNIX timestamp of this file's last git commit, so please ensure you have git installed.
60+
`lastUpdated` is the UNIX timestamp of this file's last git commit, for more details, refer to [Last Updated](../default-theme-config/#last-updated).
61+
6162
:::
6263

6364
## Content Excerpt

docs/zh/default-theme-config/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,24 @@ module.exports = {
276276

277277
更多选项请参考 [Algolia DocSearch 的文档](https://github.com/algolia/docsearch#docsearch-options)
278278

279+
## 最近更新
280+
281+
你可以通过 `themeConfig.lastUpdated` 选项来获取每个文件最后一次 `git` 提交的 UNIX 时间戳(ms),同时它将以合适的日期格式显示在每一页的底部:
282+
283+
``` js
284+
module.exports = {
285+
themeConfig: {
286+
lastUpdated: 'Last Updated', // string | boolean
287+
}
288+
}
289+
```
290+
291+
请注意,`themeConfig.lastUpdated` 默认是关闭的,如果给定一个字符串,它将会作为前缀显示(默认值是:`Last Updated`)。
292+
293+
::: warning 使用须知
294+
由于 `lastUpdated` 是基于 `git` 的, 所以你只能在一个基于 `git` 的项目中启用它。
295+
:::
296+
279297
## 上 / 下一篇链接
280298

281299
上一篇和下一篇文章的链接将会自动地根据当前页面的侧边栏的顺序来获取。你也可以使用 `YAML front matter` 来明确地重写或者禁用它:

docs/zh/guide/basic-config.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = {
3232

3333
由于 VuePress 是一个标准的 Vue 应用,你可以通过创建一个 `.vuepress/enhanceApp.js` 文件来做一些应用级别的配置,当该文件存在的时候,会被导入到应用内部。`enhanceApp.js` 应该 `export default` 一个钩子函数,并接受一个包含了一些应用级别属性的对象作为参数。你可以使用这个钩子来安装一些附加的 Vue 插件、注册全局组件,或者增加额外的路由钩子等:
3434

35-
```js
35+
``` js
3636
export default ({
3737
Vue, // VuePress 正在使用的 Vue 构造函数
3838
options, // 附加到根实例的一些选项

docs/zh/guide/custom-themes.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自
2828
"base": "/",
2929
"pages": [
3030
{
31-
"lastModified": 1524027677,
31+
"lastUpdated": 1524027677000,
3232
"path": "/",
3333
"title": "VuePress",
3434
"frontmatter": {}
@@ -44,7 +44,7 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自
4444

4545
``` json
4646
{
47-
"lastModified": 1524847549,
47+
"lastUpdated": 1524847549000,
4848
"path": "/custom-themes.html",
4949
"title": "自定义主题",
5050
"headers": [/* ... */],
@@ -57,7 +57,7 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自
5757
最后,别忘了,作为 Vue Router API 的一部分,`this.$route``this.$router` 同样可以使用。
5858

5959
::: tip 提示
60-
`lastModified` 是这个文件最后一次 git 提交的 unix 时间戳,所以请确保你已经安装了 git
60+
`lastUpdated` 是这个文件最后一次 git 提交的 UNIX 时间戳,更多细节请参考:[最近更新](../default-theme-config/#最近更新)
6161
:::
6262

6363
## 内容摘抄
@@ -80,7 +80,7 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自
8080

8181
自定义主题也可以通过主题根目录下的 `enhanceApp.js` 文件来对 VuePress 应用进行拓展配置。这个文件应当 `export default` 一个钩子函数,并接受一个包含了一些应用级别属性的对象作为参数。你可以使用这个钩子来安装一些附加的 Vue 插件、注册全局组件,或者增加额外的路由钩子等:
8282

83-
```js
83+
``` js
8484
export default ({
8585
Vue, // VuePress 正在使用的 Vue 构造函数
8686
options, // 附加到根实例的一些选项

docs/zh/guide/deploy.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pages:
111111
```
112112

113113
`.firebaserc`:
114-
```js
114+
``` js
115115
{
116116
"projects": {
117117
"default": "<YOUR_FIREBASE_ID>"

docs/zh/guide/markdown.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
└─ four.md
3939
```
4040

41-
```md
41+
``` md
4242
[Home](/) <!-- 跳转到根部的 README.md -->
4343
[foo](/foo/) <!-- 跳转到 foo 文件夹的 index.html -->
4444
[foo heading anchor](/foo/#heading) <!-- 跳转到 foo/index.html 的特定 anchor 位置 -->

docs/zh/guide/using-vue.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export default {
131131

132132
VuePress 对以下预处理器已经内置相关的 webpack 配置:`sass``scss``less``stylus``pug`。要使用它们你只需要在项目中安装对应的依赖即可。例如,要使用 `sass`,需要安装:
133133

134-
```bash
134+
``` bash
135135
yarn add -D sass-loader node-sass
136136
```
137137

@@ -146,7 +146,7 @@ yarn add -D sass-loader node-sass
146146

147147
要在组件中使用 `<template lang="pug">`,则需要安装 `pug``pug-plain-loader`:
148148

149-
```bash
149+
``` bash
150150
yarn add -D pug pug-plain-loader
151151
```
152152

lib/default-theme/Page.vue

+36
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<div class="content edit-link" v-if="editLink">
55
<a :href="editLink" target="_blank" rel="noopener noreferrer">{{ editLinkText }}</a>
66
<OutboundLink/>
7+
<div v-if="lastUpdated" class="last-updated">
8+
<span class="prefix">{{ lastUpdatedText }}: </span>
9+
<span class="time">{{ lastUpdated }}</span>
10+
</div>
711
</div>
812
<div class="content page-nav" v-if="prev || next">
913
<p class="inner">
@@ -31,6 +35,20 @@ export default {
3135
components: { OutboundLink },
3236
props: ['sidebarItems'],
3337
computed: {
38+
lastUpdated () {
39+
if (this.$page.lastUpdated) {
40+
return new Date(this.$page.lastUpdated).toLocaleString(this.$lang)
41+
}
42+
},
43+
lastUpdatedText () {
44+
if (typeof this.$themeLocaleConfig.lastUpdated === 'string') {
45+
return this.$themeLocaleConfig.lastUpdated
46+
}
47+
if (typeof this.$site.themeConfig.lastUpdated === 'string') {
48+
return this.$site.themeConfig.lastUpdated
49+
}
50+
return 'Last Updated'
51+
},
3452
prev () {
3553
const prev = this.$page.frontmatter.prev
3654
if (prev === false) {
@@ -129,6 +147,15 @@ function find (page, items, offset) {
129147
a
130148
color lighten($textColor, 25%)
131149
margin-right 0.25rem
150+
.last-updated
151+
margin-bottom .5rem
152+
float right
153+
font-weight 500
154+
font-size .9em
155+
.prefix
156+
color lighten($textColor, 25%)
157+
.time
158+
color #aaa
132159
133160
.page-nav.content
134161
padding-top 1rem !important
@@ -138,6 +165,15 @@ function find (page, items, offset) {
138165
margin-top 0 !important
139166
border-top 1px solid $borderColor
140167
padding-top 1rem
168+
overflow auto // clear float
141169
.next
142170
float right
171+
172+
@media (max-width: $MQMobile)
173+
.edit-link.content .last-updated
174+
float none
175+
text-align left
176+
margin-top 1rem
177+
font-size .8em
178+
143179
</style>

lib/prepare.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const {
99
inferTitle,
1010
extractHeaders,
1111
parseFrontmatter,
12-
getGitLastModifiedTimeStamp
12+
getGitLastUpdatedTimeStamp
1313
} = require('./util')
1414

1515
fs.ensureDirSync(tempPath)
@@ -181,15 +181,24 @@ async function resolveOptions (sourceDir) {
181181
// resolve pageFiles
182182
const pageFiles = sort(await globby(['**/*.md', '!.vuepress', '!node_modules'], { cwd: sourceDir }))
183183

184+
// resolve lastUpdated
185+
const shouldResolveLastUpdated = (
186+
themeConfig.lastUpdated ||
187+
Object.keys(siteConfig.locales && themeConfig.locales || {})
188+
.some(base => themeConfig.locales[base].lastUpdated)
189+
)
190+
184191
// resolve pagesData
185192
const pagesData = await Promise.all(pageFiles.map(async (file) => {
186193
const filepath = path.resolve(sourceDir, file)
187-
const lastModified = getGitLastModifiedTimeStamp(filepath)
188194
const data = {
189-
lastModified,
190195
path: fileToPath(file)
191196
}
192197

198+
if (shouldResolveLastUpdated) {
199+
data.lastUpdated = getGitLastUpdatedTimeStamp(filepath)
200+
}
201+
193202
// extract yaml frontmatter
194203
const content = await fs.readFile(filepath, 'utf-8')
195204
const frontmatter = parseFrontmatter(content)

lib/util/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@ exports.extractHeaders = (content, include = [], md) => {
8383
return res
8484
}
8585

86-
exports.getGitLastModifiedTimeStamp = filepath => {
87-
return parseInt(spawn.sync('git', ['log', '-1', '--format=%ct', filepath]).stdout.toString('utf-8'))
86+
exports.getGitLastUpdatedTimeStamp = filepath => {
87+
return parseInt(spawn.sync('git', ['log', '-1', '--format=%ct', filepath]).stdout.toString('utf-8')) * 1000
8888
}

0 commit comments

Comments
 (0)