Files
beall/_config/filters.js
T
inherentlee 61dc9603a2
continuous-integration/drone/push Build is passing
htmlDateString
2026-09-23 14:56:37 -07:00

41 lines
1.2 KiB
JavaScript

import { DateTime } from "luxon";
export default function (eleventyConfig) {
/* Create a single alt string from a multi-string alt object */
eleventyConfig.addFilter("createAlt", (altObject) => {
let alt;
if (altObject.length > 1)
alt = `a ${altObject.length} picture collage.`;
for (let i = 0; i < altObject.length; i++)
alt += ` Image ${i + 1} is ${altObject[i]}`;
return alt;
});
/* For <time> elements */
eleventyConfig.addFilter("htmlDateString", (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: "utc" }).toFormat("yyyy-LL-dd");
});
/* What it says on the tin */
eleventyConfig.addFilter("randomize", function(array) {
// Create a copy of the array to avoid modifying the original
let shuffledArray = array.slice();
// Fisher-Yates shuffle algorithm
for (let i = shuffledArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
}
return shuffledArray;
});
/* Sort alphabetically by item title */
eleventyConfig.addFilter("sortAlphabeticallyByTitle", collection => {
collection.sort((b, a) => b.data.title.localeCompare(a.data.title));
return collection;
});
};