From 8167cbcfdd86bb1e5cce913951954ac0a079b0f2 Mon Sep 17 00:00:00 2001 From: jonathan Date: Wed, 11 Oct 2023 20:36:57 +1030 Subject: [PATCH] generify slugify method --- .eleventy.js | 65 +++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/.eleventy.js b/.eleventy.js index a98bfb5..fee49bc 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -12,22 +12,11 @@ module.exports = function (eleventyConfig) { eleventyConfig.addPassthroughCopy("./src/_assets/js"); eleventyConfig.addShortcode("albumtile", function (title, embedLink, coverImage) { - // hugely overcomplicated universal slug method - var slug = title - .toLowerCase() - .trim() - // remove accents - .normalize('NFD') - .replace(/[\u0300-\u036f]/g, '') - // replace invalid characters with spaces - .replace(/[^a-z0-9\s-]/g, ' ') - .trim() - // replace multiple spaces or hyphens with a hyphen - .replace(/[\s-]+/g, '-'); - + var slug = slugify(title); + return `
- - + + ${title} @@ -42,18 +31,8 @@ module.exports = function (eleventyConfig) { var imageString = ""; var linkString = ""; var videoString = ""; - - var slug = title - .toLowerCase() - .trim() - // remove accents - .normalize('NFD') - .replace(/[\u0300-\u036f]/g, '') - // replace invalid characters with spaces - .replace(/[^a-z0-9\s-]/g, ' ') - .trim() - // replace multiple spaces or hyphens with a hyphen - .replace(/[\s-]+/g, '-'); + + var slug = slugify(title); if (Array.isArray(link)) { if (typeof link[0] === 'string') { @@ -72,14 +51,14 @@ module.exports = function (eleventyConfig) { imageString = `
` } - + if (video) { videoString = `
` +
` } - + return `

${title}
@@ -89,7 +68,7 @@ module.exports = function (eleventyConfig) { ${description}

` }); - + // make a list of all tags besides "post" and add them to the collection eleventyConfig.addCollection("tagsList", function (collectionApi) { const tagsList = new Set(); @@ -105,12 +84,12 @@ module.exports = function (eleventyConfig) { const sortedTagsList = new Set(Array.from(tagsList).sort()); return sortedTagsList; }); - + // limit filter eleventyConfig.addFilter("limit", function (array, limit) { return array.slice(0, limit); }); - + // minify all html files eleventyConfig.addTransform("htmlmin", function (content) { if (this.page.outputPath && this.page.outputPath.endsWith(".html")) { @@ -142,7 +121,7 @@ module.exports = function (eleventyConfig) { return data.permalink; } }); - + // When `eleventyExcludeFromCollections` is true, the file is not included in any collections eleventyConfig.addGlobalData("eleventyComputed.eleventyExcludeFromCollections", function () { return (data) => { @@ -154,7 +133,7 @@ module.exports = function (eleventyConfig) { return data.eleventyExcludeFromCollections; } }); - + eleventyConfig.on("eleventy.before", ({ runMode }) => { // Set the environment variable if (runMode === "serve" || runMode === "watch") { @@ -171,4 +150,18 @@ module.exports = function (eleventyConfig) { includes: "_includes", }, }; -}; \ No newline at end of file +}; + +var slugify = function (preSlug) { + return preSlug + .toLowerCase() + .trim() + // remove accents + .normalize('NFD') + .replace(/[\u0300-\u036f]/g, '') + // replace invalid characters with spaces + .replace(/[^a-z0-9\s-]/g, ' ') + .trim() + // replace multiple spaces or hyphens with a hyphen + .replace(/[\s-]+/g, '-'); +} \ No newline at end of file