From 14afeb69a6314661e42c4e20a3150f1dccca9a81 Mon Sep 17 00:00:00 2001 From: zyxd Date: Sat, 20 Apr 2019 00:33:49 +0400 Subject: [PATCH 1/2] Using directory name to get page date --- packages/@vuepress/core/lib/node/Page.js | 17 ++++++++++++++--- packages/@vuepress/core/lib/node/util/index.js | 15 +++++++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/packages/@vuepress/core/lib/node/Page.js b/packages/@vuepress/core/lib/node/Page.js index 27df3b44e5..dcd6684436 100644 --- a/packages/@vuepress/core/lib/node/Page.js +++ b/packages/@vuepress/core/lib/node/Page.js @@ -146,6 +146,17 @@ module.exports = class Page { this.buildPermalink() } + /** + * name of page's parent folder. + * + * @returns {string} + * @api public + */ + + get foldername () { + return path.dirname(this._filePath || this.regularPath).split(path.sep).pop() + } + /** * file name of page's source markdown file, or the last cut of regularPath. * @@ -202,7 +213,7 @@ module.exports = class Page { */ get date () { - return inferDate(this.frontmatter, this.filename) + return inferDate(this.frontmatter, this.filename, this.foldername) } /** @@ -215,9 +226,9 @@ module.exports = class Page { * @returns {string} * @private */ - stripFilename(fileName) { + stripFilename (fileName) { const match = fileName.match(DATE_RE) - + return match ? match[3] : fileName } diff --git a/packages/@vuepress/core/lib/node/util/index.js b/packages/@vuepress/core/lib/node/util/index.js index 20b97a5154..6e00b1dac6 100644 --- a/packages/@vuepress/core/lib/node/util/index.js +++ b/packages/@vuepress/core/lib/node/util/index.js @@ -54,13 +54,16 @@ exports.applyUserWebpackConfig = function (userConfig, config, isServer) { const DATE_RE = /(\d{4}-\d{1,2}(-\d{1,2})?)-(.*)/ exports.DATE_RE = DATE_RE -exports.inferDate = function (frontmatter = {}, filename) { +exports.inferDate = function (frontmatter = {}, filename, foldername) { + let matches + if (frontmatter.date) { return frontmatter.date + } else if (filename && (matches = filename.match(DATE_RE))) { + return matches[1] + } else if (foldername && (matches = foldername.match(DATE_RE))) { + return matches[1] + } else { + return null } - const match = filename.match(DATE_RE) - if (match) { - return match[1] - } - return null } From 934861a3301328545159f7dc02e67d97e2ebf469 Mon Sep 17 00:00:00 2001 From: zyxd Date: Mon, 22 Apr 2019 21:46:45 +0400 Subject: [PATCH 2/2] Fix some stuff --- packages/@vuepress/core/lib/node/Page.js | 8 ++++---- packages/@vuepress/core/lib/node/util/index.js | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/@vuepress/core/lib/node/Page.js b/packages/@vuepress/core/lib/node/Page.js index dcd6684436..0da8783010 100644 --- a/packages/@vuepress/core/lib/node/Page.js +++ b/packages/@vuepress/core/lib/node/Page.js @@ -147,14 +147,14 @@ module.exports = class Page { } /** - * name of page's parent folder. + * name of page's parent directory. * * @returns {string} * @api public */ - get foldername () { - return path.dirname(this._filePath || this.regularPath).split(path.sep).pop() + get dirname () { + return path.basename(path.dirname(this._filePath || this.regularPath)) } /** @@ -213,7 +213,7 @@ module.exports = class Page { */ get date () { - return inferDate(this.frontmatter, this.filename, this.foldername) + return inferDate(this.frontmatter, this.filename, this.dirname) } /** diff --git a/packages/@vuepress/core/lib/node/util/index.js b/packages/@vuepress/core/lib/node/util/index.js index 6e00b1dac6..1a5d914e94 100644 --- a/packages/@vuepress/core/lib/node/util/index.js +++ b/packages/@vuepress/core/lib/node/util/index.js @@ -54,14 +54,14 @@ exports.applyUserWebpackConfig = function (userConfig, config, isServer) { const DATE_RE = /(\d{4}-\d{1,2}(-\d{1,2})?)-(.*)/ exports.DATE_RE = DATE_RE -exports.inferDate = function (frontmatter = {}, filename, foldername) { +exports.inferDate = function (frontmatter = {}, filename, dirname) { let matches if (frontmatter.date) { return frontmatter.date } else if (filename && (matches = filename.match(DATE_RE))) { return matches[1] - } else if (foldername && (matches = foldername.match(DATE_RE))) { + } else if (dirname && (matches = dirname.match(DATE_RE))) { return matches[1] } else { return null