Skip to content

Commit

Permalink
Merge pull request #2111 from AleksandrHovhannisyan/directory-slug-st…
Browse files Browse the repository at this point in the history
…rip-out-date

Strip date from directory slugs
  • Loading branch information
zachleat committed May 10, 2022
2 parents d445975 + 2488ab1 commit bf79be5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
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

0 comments on commit bf79be5

Please sign in to comment.