gram reformat

This commit is contained in:
2026-05-14 09:19:05 -07:00
parent 6585b8e74d
commit c51e5b502a

View File

@ -1,57 +1,65 @@
import { DateTime } from "luxon"; import { DateTime } from "luxon";
export default function(eleventyConfig) { export default function (eleventyConfig) {
// Return the keys used in an object // Return the keys used in an object
eleventyConfig.addFilter("getKeys", target => { eleventyConfig.addFilter("getKeys", (target) => {
return Object.keys(target); return Object.keys(target);
}); });
/* Exclude all posts with given URLs from a collection list */ /* Exclude all posts with given URLs from a collection list */
eleventyConfig.addFilter("excludeFromCollection", function (collection=[], urls=[this.ctx.page.url]) { eleventyConfig.addFilter(
return collection.filter(post => urls.indexOf(post.url) === -1); "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 */ /* Filter a collection by a set of tags, returning any that share one or more tags */
eleventyConfig.addFilter("filterByTags", function(collection=[], ...requiredTags) { eleventyConfig.addFilter(
return collection.filter(post => { "filterByTags",
return requiredTags.flat().some(tag => post.data.tags.includes(tag)); function (collection = [], ...requiredTags) {
}); return collection.filter((post) => {
}); return requiredTags.flat().some((tag) => post.data.tags.includes(tag));
});
},
);
/* Return n elements from a list */ /* Return n elements from a list */
eleventyConfig.addFilter("head", (collection=[], n) => { eleventyConfig.addFilter("head", (collection = [], n) => {
if(!Array.isArray(collection) || collection.length === 0) { if (!Array.isArray(collection) || collection.length === 0) {
return []; return [];
} }
if( n < 0 ) { if (n < 0) {
return collection.slice(n); return collection.slice(n);
} }
return collection.slice(0, n); return collection.slice(0, n);
}); });
/* For <time> elements */ /* For <time> elements */
eleventyConfig.addFilter("htmlDateString", (dateObj) => { eleventyConfig.addFilter("htmlDateString", (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: "utc" }).toFormat('yyyy-LL-dd'); return DateTime.fromJSDate(dateObj, { zone: "utc" }).toFormat("yyyy-LL-dd");
}); });
/* What it says on the tin */ /* What it says on the tin */
eleventyConfig.addFilter("randomize", function(array) { eleventyConfig.addFilter("randomize", function (array) {
// Create a copy of the array to avoid modifying the original // Create a copy of the array to avoid modifying the original
let shuffledArray = array.slice(); let shuffledArray = array.slice();
// Fisher-Yates shuffle algorithm // Fisher-Yates shuffle algorithm
for (let i = shuffledArray.length - 1; i > 0; i--) { for (let i = shuffledArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1)); const j = Math.floor(Math.random() * (i + 1));
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]]; [shuffledArray[i], shuffledArray[j]] = [
shuffledArray[j],
shuffledArray[i],
];
} }
return shuffledArray; return shuffledArray;
}); });
/* Human-readable dates */ /* Human-readable dates */
eleventyConfig.addFilter("readableDate", (dateObj, zone) => { eleventyConfig.addFilter("readableDate", (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: "utc" }).toLocaleString( return DateTime.fromJSDate(dateObj, { zone: "utc" }).toLocaleString(
DateTime.DATE_FULL, DateTime.DATE_FULL,
); );
@ -59,16 +67,26 @@ export default function(eleventyConfig) {
/* Filter out structural tags */ /* Filter out structural tags */
eleventyConfig.addFilter("removeBasicTags", (tags) => { eleventyConfig.addFilter("removeBasicTags", (tags) => {
return tags.filter(tag => ["all", "posts", "gallery", "reference", "tagPagination", "blur"].indexOf(tag) === -1); return tags.filter(
(tag) =>
[
"all",
"posts",
"gallery",
"reference",
"tagPagination",
"blur",
].indexOf(tag) === -1,
);
}); });
/* What it says on the tin */ /* What it says on the tin */
eleventyConfig.addFilter("sortAlphabetically", strings => eleventyConfig.addFilter("sortAlphabetically", (strings) =>
(strings || []).sort((b, a) => b.localeCompare(a)) (strings || []).sort((b, a) => b.localeCompare(a)),
); );
/* Remove year from image filenames for OG metadata file path */ /* Remove year from image filenames for OG metadata file path */
eleventyConfig.addFilter("toOgFilename", (filename) => { eleventyConfig.addFilter("toOgFilename", (filename) => {
return filename.split("/")[1]; return filename.split("/")[1];
}); });
}; }