generify slugify method

This commit is contained in:
2023-10-11 20:36:57 +10:30
parent 3f2e659778
commit 8167cbcfdd

View File

@ -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 `<div>
<a class="hide" href="${embedLink}" target="${slug}">
<img class="album-tile-cover-image" src="${coverImage}">
<a class="hide" href="${embedLink}" target="${slug}">
<img class="album-tile-cover-image" src="${coverImage}">
</a>
<iframe class="album-tile-iframe" name="${slug}" src="about:blank" seamless></iframe>
<b>${title}</b>
@ -43,17 +32,7 @@ module.exports = function (eleventyConfig) {
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') {
@ -77,7 +56,7 @@ module.exports = function (eleventyConfig) {
videoString = `<video autoplay loop muted controls poster="${video.poster}">
<source src="${video.link}" type="video/mp4"></source>
<img src="${video.poster}"></img>
</video><br>`
</video><br>`
}
return `<hr>
@ -172,3 +151,17 @@ module.exports = function (eleventyConfig) {
},
};
};
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, '-');
}