From 58db10c8eecfc089797c80628ef9475643072dc6 Mon Sep 17 00:00:00 2001 From: Rahul Kadyan Date: Fri, 12 Apr 2019 11:15:55 +0530 Subject: [PATCH] fix: Use directory name to compute slug if filename is readme or index --- packages/@vuepress/core/lib/node/Page.js | 33 +++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/packages/@vuepress/core/lib/node/Page.js b/packages/@vuepress/core/lib/node/Page.js index 5172dbd716..27df3b44e5 100644 --- a/packages/@vuepress/core/lib/node/Page.js +++ b/packages/@vuepress/core/lib/node/Page.js @@ -165,7 +165,19 @@ module.exports = class Page { */ get slug () { - return slugify(this.strippedFilename) + const strippedFilename = this.strippedFilename + + if (/^(index|readme)$/i.test(strippedFilename)) { + const strippedFilename = this.stripFilename( + path.basename(path.dirname(this._filePath || this.regularPath)) + ) + + if (strippedFilename) { + return slugify(strippedFilename) + } + } + + return slugify(strippedFilename) } /** @@ -179,8 +191,7 @@ module.exports = class Page { */ get strippedFilename () { - const match = this.filename.match(DATE_RE) - return match ? match[3] : this.filename + return this.stripFilename(this.filename) } /** @@ -194,6 +205,22 @@ module.exports = class Page { return inferDate(this.frontmatter, this.filename) } + /** + * stripped file name. + * + * If filename was yyyy-MM-dd-[title], the date prefix will be stripped. + * If filename was yyyy-MM-[title], the date prefix will be stripped. + * + * @param {string} fileName + * @returns {string} + * @private + */ + stripFilename(fileName) { + const match = fileName.match(DATE_RE) + + return match ? match[3] : fileName + } + /** * Convert page's metadata to JSON, note that all fields beginning * with an underscore will not be serialized.