Skip to content

Commit 9efc678

Browse files
znckulivz
authored andcommitted
fix($core): use directory name to compute slug if filename is readme or index (close: #1443) (#1535)
1 parent fb324d5 commit 9efc678

File tree

1 file changed

+30
-3
lines changed
  • packages/@vuepress/core/lib/node

1 file changed

+30
-3
lines changed

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

+30-3
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,19 @@ module.exports = class Page {
165165
*/
166166

167167
get slug () {
168-
return slugify(this.strippedFilename)
168+
const strippedFilename = this.strippedFilename
169+
170+
if (/^(index|readme)$/i.test(strippedFilename)) {
171+
const strippedFilename = this.stripFilename(
172+
path.basename(path.dirname(this._filePath || this.regularPath))
173+
)
174+
175+
if (strippedFilename) {
176+
return slugify(strippedFilename)
177+
}
178+
}
179+
180+
return slugify(strippedFilename)
169181
}
170182

171183
/**
@@ -179,8 +191,7 @@ module.exports = class Page {
179191
*/
180192

181193
get strippedFilename () {
182-
const match = this.filename.match(DATE_RE)
183-
return match ? match[3] : this.filename
194+
return this.stripFilename(this.filename)
184195
}
185196

186197
/**
@@ -194,6 +205,22 @@ module.exports = class Page {
194205
return inferDate(this.frontmatter, this.filename)
195206
}
196207

208+
/**
209+
* stripped file name.
210+
*
211+
* If filename was yyyy-MM-dd-[title], the date prefix will be stripped.
212+
* If filename was yyyy-MM-[title], the date prefix will be stripped.
213+
*
214+
* @param {string} fileName
215+
* @returns {string}
216+
* @private
217+
*/
218+
stripFilename(fileName) {
219+
const match = fileName.match(DATE_RE)
220+
221+
return match ? match[3] : fileName
222+
}
223+
197224
/**
198225
* Convert page's metadata to JSON, note that all fields beginning
199226
* with an underscore will not be serialized.

0 commit comments

Comments
 (0)