Skip to content

Instantly share code, notes, and snippets.

@zachleat
Last active Jul 9, 2021
Embed
What would you like to do?
Eleventy URL Linter to check for changed URLs when swapping from `slug` to `slugify` filters (via @pdehaan)
const assert = require("assert");
const inspect = require("util").inspect;
module.exports= (eleventyConfig) => {
// ever so slightly modified from @pdehaan’s original:
// https://github.com/11ty/eleventy/issues/278#issuecomment-873367464
const slugFn = eleventyConfig.getFilter("slug");
const slugifyFn = eleventyConfig.getFilter("slugify");
const slugErrors = new Set();
eleventyConfig.addFilter("slug", function (str="") {
const slugValue = slugFn(str);
try {
assert.strictEqual(slugValue, slugifyFn(str));
} catch (err) {
// Only display a unique error once.
if (!slugErrors.has(err.message)) {
console.error(`\nslug-vs-slugify filter mismatch for ${inspect(str)}\n${err.message}`);
slugErrors.add(err.message);
process.exitCode = 2;
}
} finally {
return slugValue;
}
});
};
@zachleat
Copy link
Author

zachleat commented Jul 9, 2021

This is now bundled into the Eleventy Upgrade Helper plugin: https://github.com/11ty/eleventy-upgrade-help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment