Skip to content

Commit 2c930c9

Browse files
zyxdulivz
authored andcommitted
feat($core): infer page's date via directory name (#1553)
1 parent 0970954 commit 2c930c9

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

packages/@vuepress/core/lib/node/Page.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ module.exports = class Page {
146146
this.buildPermalink()
147147
}
148148

149+
/**
150+
* name of page's parent directory.
151+
*
152+
* @returns {string}
153+
* @api public
154+
*/
155+
156+
get dirname () {
157+
return path.basename(path.dirname(this._filePath || this.regularPath))
158+
}
159+
149160
/**
150161
* file name of page's source markdown file, or the last cut of regularPath.
151162
*
@@ -202,7 +213,7 @@ module.exports = class Page {
202213
*/
203214

204215
get date () {
205-
return inferDate(this.frontmatter, this.filename)
216+
return inferDate(this.frontmatter, this.filename, this.dirname)
206217
}
207218

208219
/**

packages/@vuepress/core/lib/node/util/index.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,16 @@ exports.applyUserWebpackConfig = function (userConfig, config, isServer) {
5454
const DATE_RE = /(\d{4}-\d{1,2}(-\d{1,2})?)-(.*)/
5555
exports.DATE_RE = DATE_RE
5656

57-
exports.inferDate = function (frontmatter = {}, filename) {
57+
exports.inferDate = function (frontmatter = {}, filename, dirname) {
58+
let matches
59+
5860
if (frontmatter.date) {
5961
return frontmatter.date
62+
} else if (filename && (matches = filename.match(DATE_RE))) {
63+
return matches[1]
64+
} else if (dirname && (matches = dirname.match(DATE_RE))) {
65+
return matches[1]
66+
} else {
67+
return null
6068
}
61-
const match = filename.match(DATE_RE)
62-
if (match) {
63-
return match[1]
64-
}
65-
return null
6669
}

0 commit comments

Comments
 (0)