Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip date from directory slugs when calculating page.fileSlug #2111

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/TemplateFileSlug.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ class TemplateFileSlug {
}

_getRawSlug() {
let slug = this.filenameNoExt;
const slug = this.filenameNoExt;
return this._stripDateFromSlug(slug);
}

/** Removes dates in the format of YYYY-MM-DD from a given slug string candidate. */
_stripDateFromSlug(slug) {
let reg = slug.match(/\d{4}-\d{2}-\d{2}-(.*)/);
if (reg) {
return reg[1];
Expand All @@ -35,7 +40,11 @@ class TemplateFileSlug {
let rawSlug = this._getRawSlug();

if (rawSlug === "index") {
return this.dirs.length ? this.dirs[this.dirs.length - 1] : "";
if (!this.dirs.length) {
return "";
}
const lastDir = this.dirs[this.dirs.length - 1];
return this._stripDateFromSlug(lastDir);
}

return rawSlug;
Expand Down
6 changes: 6 additions & 0 deletions test/TemplateFileSlugTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ test("Easy slug with date, index with dir", (t) => {
t.is(fs.getFullPathWithoutExtension(), "/test/index");
});

test("Strips date from dir name", (t) => {
let fs = getNewSlugInstance("./2021-11-20-my-awesome-post/index.md");
t.is(fs.getSlug(), "my-awesome-post");
t.is(fs.getFullPathWithoutExtension(), "/2021-11-20-my-awesome-post/index");
});

/* Pass Input dir */
test("Easy slug, input dir", (t) => {
let fs = getNewSlugInstance("./file.html", ".");
Expand Down