first
This commit is contained in:
59
_config/filters.js
Normal file
59
_config/filters.js
Normal file
@ -0,0 +1,59 @@
|
||||
import { DateTime } from "luxon";
|
||||
|
||||
export default function (eleventyConfig) {
|
||||
/* Return the keys used in an object */
|
||||
eleventyConfig.addFilter("getKeys", (target) => {
|
||||
return Object.keys(target);
|
||||
});
|
||||
|
||||
/* Return n elements from a list */
|
||||
eleventyConfig.addFilter("head", (collection = [], n) => {
|
||||
if (!Array.isArray(collection) || collection.length === 0) {
|
||||
return [];
|
||||
}
|
||||
if (n < 0) {
|
||||
return collection.slice(n);
|
||||
}
|
||||
|
||||
return collection.slice(0, n);
|
||||
});
|
||||
|
||||
/* 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;
|
||||
});
|
||||
|
||||
/* Human-readable dates */
|
||||
eleventyConfig.addFilter("readableDate", (dateObj, zone) => {
|
||||
return DateTime.fromJSDate(dateObj, { zone: zone || "utc" }).toLocaleString(
|
||||
DateTime.DATE_FULL,
|
||||
);
|
||||
});
|
||||
|
||||
/* Filter out structural tags */
|
||||
eleventyConfig.addFilter("removeBasicTags", (tags) => {
|
||||
return tags.filter((tag) => ["all"].indexOf(tag) === -1);
|
||||
});
|
||||
|
||||
/* What it says on the tin */
|
||||
eleventyConfig.addFilter("sortAlphabetically", (strings) =>
|
||||
(strings || []).sort((b, a) => b.localeCompare(a)),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user