2026-02-18 18:44:17 -08:00
|
|
|
import { DateTime } from "luxon";
|
|
|
|
|
|
|
|
|
|
export default function(eleventyConfig) {
|
2026-02-19 12:07:10 -08:00
|
|
|
// Return the keys used in an object
|
|
|
|
|
eleventyConfig.addFilter("getKeys", target => {
|
|
|
|
|
return Object.keys(target);
|
2026-02-18 18:44:17 -08:00
|
|
|
});
|
|
|
|
|
|
2026-02-20 08:36:46 -08:00
|
|
|
/* Exclude all posts with given URLs from a collection list */
|
|
|
|
|
eleventyConfig.addFilter("excludeFromCollection", function (collection=[], urls=[this.ctx.page.url]) {
|
|
|
|
|
return collection.filter(post => urls.indexOf(post.url) === -1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/* Filter a collection by a set of tags, returning any that share one or more tags */
|
|
|
|
|
eleventyConfig.addFilter("filterByTags", function(collection=[], ...requiredTags) {
|
|
|
|
|
return collection.filter(post => {
|
|
|
|
|
return requiredTags.flat().some(tag => post.data.tags.includes(tag));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2026-02-18 18:44:17 -08:00
|
|
|
/* For <time> elements */
|
|
|
|
|
eleventyConfig.addFilter("htmlDateString", (dateObj) => {
|
|
|
|
|
return DateTime.fromJSDate(dateObj, { zone: "utc" }).toFormat('yyyy-LL-dd');
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-20 08:36:46 -08:00
|
|
|
/* 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;
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-19 12:07:10 -08:00
|
|
|
/* Human-readable dates */
|
|
|
|
|
eleventyConfig.addFilter("readableDate", (dateObj, format, zone) => {
|
|
|
|
|
return DateTime.fromJSDate(dateObj, { zone: zone || "utc" })
|
|
|
|
|
.toLocaleString(DateTime.DATE_FULL);
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-18 18:44:17 -08:00
|
|
|
/* Filter out structural tags */
|
|
|
|
|
eleventyConfig.addFilter("removeBasicTags", (tags) => {
|
|
|
|
|
return tags.filter(tag => ["all", "posts", "gallery", "reference", "tagPagination"].indexOf(tag) === -1);
|
|
|
|
|
});
|
2026-02-19 12:07:10 -08:00
|
|
|
|
2026-02-20 08:36:46 -08:00
|
|
|
/* What it says on the tin */
|
2026-02-19 12:07:10 -08:00
|
|
|
eleventyConfig.addFilter("sortAlphabetically", strings =>
|
|
|
|
|
(strings || []).sort((b, a) => b.localeCompare(a))
|
|
|
|
|
);
|
|
|
|
|
|
2026-02-20 08:02:52 -08:00
|
|
|
/* Remove year from image filenames for OG metadata file path */
|
2026-02-19 17:54:01 -08:00
|
|
|
eleventyConfig.addFilter("toOgFilename", (filename) => {
|
|
|
|
|
return filename.split("/")[1];
|
2026-02-19 21:07:01 -08:00
|
|
|
});
|
2026-02-18 18:44:17 -08:00
|
|
|
};
|