Last active
July 9, 2021 14:10
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is now bundled into the Eleventy Upgrade Helper plugin: https://github.com/11ty/eleventy-upgrade-help