Skip to content

Commit

Permalink
Fixes #278
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jul 1, 2021
1 parent 89fd4a2 commit b2c4942
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"dependencies": {
"@11ty/dependency-tree": "^2.0.0",
"@iarna/toml": "^2.2.5",
"@sindresorhus/slugify": "^1.1.2",
"browser-sync": "^2.26.14",
"chalk": "^4.1.0",
"chokidar": "^3.5.1",
Expand Down
6 changes: 3 additions & 3 deletions src/Filters/Slug.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const slugify = require("slugify");

module.exports = function(str) {
return slugify(str, {
module.exports = function(str, options = {}) {
return slugify(str, Object.assign({
replacement: "-",
lower: true
});
}, options));
};
5 changes: 5 additions & 0 deletions src/Filters/Slugify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const slugify = require("@sindresorhus/slugify");

module.exports = function(str, options = {}) {
return slugify(str, options);
};
4 changes: 4 additions & 0 deletions src/defaultConfig.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const urlFilter = require("./Filters/Url");
const serverlessUrlFilter = require("./Filters/ServerlessUrl");
const slugFilter = require("./Filters/Slug");
const slugifyFilter = require("./Filters/Slugify");
const getCollectionItem = require("./Filters/GetCollectionItem");

module.exports = function (config) {
let eleventyConfig = this;

config.addFilter("slug", slugFilter);
config.addFilter("slugify", slugifyFilter);

config.addFilter("url", function (url, pathPrefixOverride) {
let pathPrefix =
pathPrefixOverride || eleventyConfig.getConfig().pathPrefix;
Expand Down
19 changes: 19 additions & 0 deletions test/TemplateTest_Permalink.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,22 @@ test("Reuse permalink in directory specific data file", async (t) => {
t.is(await tmpl.getOutputPath(), "./dist/2016/01/01/index.html");
});

test("Using slugify filter!", async (t) => {
let tmpl = getNewTemplate(
"./test/slugify-filter/test.njk",
"./test/slugify-filter/",
"./dist"
);

t.is(await tmpl.getOutputPath(), "./dist/subdir/slug-love-candidate-lyublyu/index.html");
});

test("Using slugify filter with apostrophe", async (t) => {
let tmpl = getNewTemplate(
"./test/slugify-filter/apostrophe.njk",
"./test/slugify-filter/",
"./dist"
);

t.is(await tmpl.getOutputPath(), "./dist/subdir/hi-i-m-zach/index.html");
});
5 changes: 5 additions & 0 deletions test/slugify-filter/apostrophe.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: "Hi I'm Zach"
permalink: subdir/{{ title | slugify }}/index.html
---
Slugged.
5 changes: 5 additions & 0 deletions test/slugify-filter/test.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: _Slug ♥ CANDIDATE люблю $#%-
permalink: subdir/{{ title | slugify }}/index.html
---
Slugged.

0 comments on commit b2c4942

Please sign in to comment.