Skip to content

Commit

Permalink
feat: generate the timestamp of last updated for each doc (close #258) (
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz authored May 3, 2018
1 parent fd547ea commit d9b290b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
8 changes: 7 additions & 1 deletion docs/guide/custom-themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This is the value of `$site` of this very website:
"base": "/",
"pages": [
{
"lastModified": 1524027677,
"path": "/",
"title": "VuePress",
"frontmatter": {}
Expand All @@ -43,7 +44,8 @@ This is the `$page` object for this page you are looking at:

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

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

::: tip
`lastModified` is the UNIX timestamp of this file's last git commit, so please ensure you have git installed.
:::

## Content Excerpt

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.
Expand Down
6 changes: 6 additions & 0 deletions docs/zh/guide/custom-themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自
"base": "/",
"pages": [
{
"lastModified": 1524027677,
"path": "/",
"title": "VuePress",
"frontmatter": {}
Expand All @@ -43,6 +44,7 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自

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

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

::: tip 提示
`lastModified` 是这个文件最后一次 git 提交的 unix 时间戳,所以请确保你已经安装了 git。
:::

## 内容摘抄

如果一个 markdown 文件中有一个 `<!-- more -->` 注释,则该注释之前的内容会被抓取并暴露在 `$page.excerpt` 属性中。如果你在开发一个博客主题,你可以用这个属性来渲染一个带摘抄的文章列表。
Expand Down
12 changes: 10 additions & 2 deletions lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ const yamlParser = require('js-yaml')
const tomlParser = require('toml')
const createMarkdown = require('./markdown')
const tempPath = path.resolve(__dirname, 'app/.temp')
const { inferTitle, extractHeaders, parseFrontmatter } = require('./util')
const {
inferTitle,
extractHeaders,
parseFrontmatter,
getGitLastModifiedTimeStamp
} = require('./util')

fs.ensureDirSync(tempPath)

Expand Down Expand Up @@ -178,12 +183,15 @@ async function resolveOptions (sourceDir) {

// resolve pagesData
const pagesData = await Promise.all(pageFiles.map(async (file) => {
const filepath = path.resolve(sourceDir, file)
const lastModified = getGitLastModifiedTimeStamp(filepath)
const data = {
lastModified,
path: fileToPath(file)
}

// extract yaml frontmatter
const content = await fs.readFile(path.resolve(sourceDir, file), 'utf-8')
const content = await fs.readFile(filepath, 'utf-8')
const frontmatter = parseFrontmatter(content)
// infer title
const title = inferTitle(frontmatter)
Expand Down
5 changes: 5 additions & 0 deletions lib/util/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const spawn = require('cross-spawn')
const parseHeaders = require('./parseHeaders')

exports.normalizeHeadTag = tag => {
Expand Down Expand Up @@ -81,3 +82,7 @@ exports.extractHeaders = (content, include = [], md) => {
cache.set(key, res)
return res
}

exports.getGitLastModifiedTimeStamp = filepath => {
return parseInt(spawn.sync('git', ['log', '-1', '--format=%ct', filepath]).stdout.toString('utf-8'))
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"commander": "^2.15.1",
"connect-history-api-fallback": "^1.5.0",
"copy-webpack-plugin": "^4.5.1",
"cross-spawn": "^6.0.5",
"css-loader": "^0.28.11",
"diacritics": "^1.3.0",
"docsearch.js": "^2.5.2",
Expand Down
16 changes: 15 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,16 @@ cross-spawn@^5.0.1, cross-spawn@^5.1.0:
shebang-command "^1.2.0"
which "^1.2.9"

cross-spawn@^6.0.5:
version "6.0.5"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4"
dependencies:
nice-try "^1.0.4"
path-key "^2.0.1"
semver "^5.5.0"
shebang-command "^1.2.0"
which "^1.2.9"

cryptiles@2.x.x:
version "2.0.5"
resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8"
Expand Down Expand Up @@ -4284,6 +4294,10 @@ neo-async@^2.5.0:
version "2.5.1"
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.5.1.tgz#acb909e327b1e87ec9ef15f41b8a269512ad41ee"

nice-try@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.4.tgz#d93962f6c52f2c1558c0fbda6d512819f1efe1c4"

no-case@^2.2.0:
version "2.3.2"
resolved "https://registry.yarnpkg.com/no-case/-/no-case-2.3.2.tgz#60b813396be39b3f1288a4c1ed5d1e7d28b464ac"
Expand Down Expand Up @@ -4686,7 +4700,7 @@ path-is-inside@^1.0.1, path-is-inside@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/path-is-inside/-/path-is-inside-1.0.2.tgz#365417dede44430d1c11af61027facf074bdfc53"

path-key@^2.0.0:
path-key@^2.0.0, path-key@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40"

Expand Down

0 comments on commit d9b290b

Please sign in to comment.