Skip to content

Commit

Permalink
feat($core): infer page's date via directory name (#1553)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxd authored and ulivz committed Apr 23, 2019
1 parent 0970954 commit 2c930c9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
13 changes: 12 additions & 1 deletion packages/@vuepress/core/lib/node/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -202,7 +213,7 @@ module.exports = class Page {
*/

get date () {
return inferDate(this.frontmatter, this.filename)
return inferDate(this.frontmatter, this.filename, this.dirname)
}

/**
Expand Down
15 changes: 9 additions & 6 deletions packages/@vuepress/core/lib/node/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 2c930c9

Please sign in to comment.