Skip to content

Instantly share code, notes, and snippets.

@zachleat
Last active July 9, 2021 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachleat/a58bc9e7273fc182a3c9c1234fee82c8 to your computer and use it in GitHub Desktop.
Save zachleat/a58bc9e7273fc182a3c9c1234fee82c8 to your computer and use it in GitHub Desktop.
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