From 82d45023479a02fe7f2a5ecf95be2c11707a87e9 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Sat, 28 Apr 2018 00:45:49 +0800 Subject: [PATCH 1/5] feat: generate the timestamp of creation and last updated --- docs/guide/custom-themes.md | 10 +++++++++- docs/zh/guide/custom-themes.md | 8 ++++++++ lib/default-theme/Layout.vue | 2 +- lib/prepare.js | 6 +++++- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/docs/guide/custom-themes.md b/docs/guide/custom-themes.md index f70f2b3dca..5f01bd5caf 100644 --- a/docs/guide/custom-themes.md +++ b/docs/guide/custom-themes.md @@ -28,6 +28,8 @@ This is the value of `$site` of this very website: "base": "/", "pages": [ { + "birthtimeMs": 1524773182000, + "mtimeMs": 1524773182000, "path": "/", "title": "VuePress", "frontmatter": {} @@ -43,7 +45,9 @@ This is the `$page` object for this page you are looking at: ``` json { - "path": "/custom-themes.html", + "birthtimeMs": 1524846768000, + "mtimeMs": 1524846768000, + "path": "/guide/custom-themes.html", "title": "Custom Themes", "headers": [/* ... */], "frontmatter": {} @@ -54,6 +58,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 + `birthtimeMs` and `mtimeMs` is respectively `the time of file creation` and `the last time this file was modified`, For more details, refer to [Node.js documentation](https://nodejs.org/dist/latest/docs/api/fs.html#fs_stat_time_values) +::: + ## Content Excerpt If a markdown file contains a `` 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. diff --git a/docs/zh/guide/custom-themes.md b/docs/zh/guide/custom-themes.md index 474bca7aad..d33a83b587 100644 --- a/docs/zh/guide/custom-themes.md +++ b/docs/zh/guide/custom-themes.md @@ -28,6 +28,8 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自 "base": "/", "pages": [ { + "birthtimeMs": 1524495591000, + "mtimeMs": 1524495591000, "path": "/", "title": "VuePress", "frontmatter": {} @@ -43,6 +45,8 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自 ``` json { + "birthtimeMs": 1524544404000, + "mtimeMs": 1524544404000, "path": "/custom-themes.html", "title": "自定义主题", "headers": [/* ... */], @@ -54,6 +58,10 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自 最后,别忘了,作为 Vue Router API 的一部分,`this.$route` 和 `this.$router` 同样可以使用。 +::: tip 提示 + `birthtimeMs` 和 `mtimeMs` 分别是 `文件的创建时间` 和 `文件最后被修改的时间`, 更多细节请参考 [Node.js 的文档](https://nodejs.org/dist/latest/docs/api/fs.html#fs_stat_time_values)。 +::: + ## 内容摘抄 如果一个 markdown 文件中有一个 `` 注释,则该注释之前的内容会被抓取并暴露在 `$page.excerpt` 属性中。如果你在开发一个博客主题,你可以用这个属性来渲染一个带摘抄的文章列表。 diff --git a/lib/default-theme/Layout.vue b/lib/default-theme/Layout.vue index 7ad99f1190..09c994457f 100644 --- a/lib/default-theme/Layout.vue +++ b/lib/default-theme/Layout.vue @@ -165,7 +165,7 @@ export default { const sidebarLinks = [].slice.call(document.querySelectorAll('.sidebar-link')) const anchors = [].slice.call(document.querySelectorAll('.header-anchor')) .filter(anchor => sidebarLinks.some(sidebarLink => sidebarLink.hash === anchor.hash)) - + const scrollTop = Math.max(window.pageYOffset, document.documentElement.scrollTop, document.body.scrollTop) for (let i = 0; i < anchors.length; i++) { diff --git a/lib/prepare.js b/lib/prepare.js index 2d0fc56db2..aa96195683 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -182,12 +182,16 @@ async function resolveOptions (sourceDir) { // resolve pages const pagesData = await Promise.all(options.pageFiles.map(async (file) => { + const filepath = path.resolve(sourceDir, file) + const { birthtimeMs, mtimeMs } = await fs.stat(filepath) const data = { + birthtimeMs, + mtimeMs, 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) From e4fb2dcf1f7183161ac510e4972aff8fd64d0beb Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Tue, 1 May 2018 22:16:22 +0800 Subject: [PATCH 2/5] feat: leverage git last modified time stamp --- lib/prepare.js | 7 +++---- lib/util/index.js | 5 +++++ yarn.lock | 16 +++++++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/prepare.js b/lib/prepare.js index bb021b0e49..af775da1db 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -5,7 +5,7 @@ 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) @@ -183,10 +183,9 @@ async function resolveOptions (sourceDir) { // resolve pages const pagesData = await Promise.all(options.pageFiles.map(async (file) => { const filepath = path.resolve(sourceDir, file) - const { birthtimeMs, mtimeMs } = await fs.stat(filepath) + const lastModified = getGitLastModifiedTimeStamp(filepath) const data = { - birthtimeMs, - mtimeMs, + lastModified, path: fileToPath(file) } diff --git a/lib/util/index.js b/lib/util/index.js index 67588591da..15f8aa39fb 100644 --- a/lib/util/index.js +++ b/lib/util/index.js @@ -1,3 +1,4 @@ +const spawn = require('cross-spawn') const parseHeaders = require('./parseHeaders') exports.normalizeHeadTag = tag => { @@ -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()) +} diff --git a/yarn.lock b/yarn.lock index e86ccec3b0..cd4483a612 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" From 10bba8d292de0ba7831334d35441b115f71f0fcd Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Tue, 1 May 2018 22:31:26 +0800 Subject: [PATCH 3/5] docs: update --- docs/guide/custom-themes.md | 8 +++----- docs/zh/guide/custom-themes.md | 8 +++----- package.json | 1 + 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/docs/guide/custom-themes.md b/docs/guide/custom-themes.md index 5f01bd5caf..172399c9b7 100644 --- a/docs/guide/custom-themes.md +++ b/docs/guide/custom-themes.md @@ -28,8 +28,7 @@ This is the value of `$site` of this very website: "base": "/", "pages": [ { - "birthtimeMs": 1524773182000, - "mtimeMs": 1524773182000, + "lastModified": 1524027677, "path": "/", "title": "VuePress", "frontmatter": {} @@ -45,8 +44,7 @@ This is the `$page` object for this page you are looking at: ``` json { - "birthtimeMs": 1524846768000, - "mtimeMs": 1524846768000, + "lastModified": 1524847549, "path": "/guide/custom-themes.html", "title": "Custom Themes", "headers": [/* ... */], @@ -59,7 +57,7 @@ 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 - `birthtimeMs` and `mtimeMs` is respectively `the time of file creation` and `the last time this file was modified`, For more details, refer to [Node.js documentation](https://nodejs.org/dist/latest/docs/api/fs.html#fs_stat_time_values) + `lastModified` is the UNIX timestamp of this file's last git commit, so please ensure you have installed git. ::: ## Content Excerpt diff --git a/docs/zh/guide/custom-themes.md b/docs/zh/guide/custom-themes.md index d33a83b587..8b5ebffc3e 100644 --- a/docs/zh/guide/custom-themes.md +++ b/docs/zh/guide/custom-themes.md @@ -28,8 +28,7 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自 "base": "/", "pages": [ { - "birthtimeMs": 1524495591000, - "mtimeMs": 1524495591000, + "lastModified": 1524027677, "path": "/", "title": "VuePress", "frontmatter": {} @@ -45,8 +44,7 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自 ``` json { - "birthtimeMs": 1524544404000, - "mtimeMs": 1524544404000, + "lastModified": 1524847549, "path": "/custom-themes.html", "title": "自定义主题", "headers": [/* ... */], @@ -59,7 +57,7 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自 最后,别忘了,作为 Vue Router API 的一部分,`this.$route` 和 `this.$router` 同样可以使用。 ::: tip 提示 - `birthtimeMs` 和 `mtimeMs` 分别是 `文件的创建时间` 和 `文件最后被修改的时间`, 更多细节请参考 [Node.js 的文档](https://nodejs.org/dist/latest/docs/api/fs.html#fs_stat_time_values)。 + `lastModified` 是这个文件最后一次 git 提交的 unix 时间戳,所以请确保你已经安装了 git。 ::: ## 内容摘抄 diff --git a/package.json b/package.json index 1768fee7af..e490caa9df 100644 --- a/package.json +++ b/package.json @@ -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", From 97e4961cdbf8f934e499a94e73d7d66f981b6567 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Fri, 4 May 2018 00:50:55 +0800 Subject: [PATCH 4/5] fix: merge conflict --- lib/prepare.js | 12 ++++++++++-- lib/util/index.js | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/prepare.js b/lib/prepare.js index e892f1cbcc..46b478de6f 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -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) @@ -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) diff --git a/lib/util/index.js b/lib/util/index.js index 15f8aa39fb..a1c2a70b09 100644 --- a/lib/util/index.js +++ b/lib/util/index.js @@ -84,5 +84,5 @@ exports.extractHeaders = (content, include = [], md) => { } exports.getGitLastModifiedTimeStamp = filepath => { - return parseInt(spawn.sync('git', ['log', '-1', '--format=%ct', filepath]).stdout.toString()) + return parseInt(spawn.sync('git', ['log', '-1', '--format=%ct', filepath]).stdout.toString('utf-8')) } From a385de4438a9f3e1a995a33cd74397e509a67672 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Fri, 4 May 2018 00:51:40 +0800 Subject: [PATCH 5/5] docs: tweaks --- docs/guide/custom-themes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guide/custom-themes.md b/docs/guide/custom-themes.md index 172399c9b7..190173df77 100644 --- a/docs/guide/custom-themes.md +++ b/docs/guide/custom-themes.md @@ -57,7 +57,7 @@ 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 installed git. + `lastModified` is the UNIX timestamp of this file's last git commit, so please ensure you have git installed. ::: ## Content Excerpt