From 2c930c907e4398e113ef29e7de05686638b45ee6 Mon Sep 17 00:00:00 2001 From: zyxd Date: Wed, 24 Apr 2019 00:51:31 +0400 Subject: [PATCH] feat($core): infer page's date via directory name (#1553) --- packages/@vuepress/core/lib/node/Page.js | 13 ++++++++++++- packages/@vuepress/core/lib/node/util/index.js | 15 +++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/@vuepress/core/lib/node/Page.js b/packages/@vuepress/core/lib/node/Page.js index 9cf6ce147a..0da8783010 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 directory. + * + * @returns {string} + * @api public + */ + + get dirname () { + return path.basename(path.dirname(this._filePath || this.regularPath)) + } + /** * 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.dirname) } /** diff --git a/packages/@vuepress/core/lib/node/util/index.js b/packages/@vuepress/core/lib/node/util/index.js index 20b97a5154..1a5d914e94 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, dirname) { + let matches + if (frontmatter.date) { return frontmatter.date + } else if (filename && (matches = filename.match(DATE_RE))) { + return matches[1] + } else if (dirname && (matches = dirname.match(DATE_RE))) { + return matches[1] + } else { + return null } - const match = filename.match(DATE_RE) - if (match) { - return match[1] - } - return null }