Files
kfitz.info/_config/filters.js
Kathleen Fitzpatrick 891ed350b9 fix webmentions & css
2024-11-29 11:14:42 -05:00

107 lines
3.1 KiB
JavaScript

import { DateTime } from "luxon";
import sanitizeHtml from 'sanitize-html';
export default function(eleventyConfig) {
eleventyConfig.addFilter("readableDate", (dateObj, format, zone) => {
// Formatting tokens for Luxon: https://moment.github.io/luxon/#/formatting?id=table-of-tokens
return DateTime.fromJSDate(dateObj, { zone: zone || "utc" }).toFormat(format || "dd LLLL yyyy");
});
eleventyConfig.addFilter('dateFromTimestamp', (timestamp) => {
return DateTime.fromISO(timestamp, { zone: 'America/New_York' }).toJSDate()
})
eleventyConfig.addFilter("htmlDateString", (dateObj) => {
// dateObj input: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
return DateTime.fromJSDate(dateObj, { zone: "utc" }).toFormat('yyyy-LL-dd');
});
// Get the first `n` elements of a collection.
eleventyConfig.addFilter("head", (array, n) => {
if(!Array.isArray(array) || array.length === 0) {
return [];
}
if( n < 0 ) {
return array.slice(n);
}
return array.slice(0, n);
});
eleventyConfig.addFilter('mentionsForUrl', (webmentions, url) => {
const allowedTypes = ['in-reply-to', 'mention-of']
const allowedHTML = {
allowedTags: ['b', 'i', 'em', 'strong', 'a'],
allowedAttributes: {
a: ['href']
}
}
const clean = (entry) => {
const { html, text } = entry.content
if (html) {
entry.content.value = sanitizeHtml(text, allowedHTML)
};
return entry;
}
// sort webmentions by published timestamp chronologically.
// swap a.published and b.published to reverse order.
const orderByDate = (a, b) => new Date(b.published) - new Date(a.published)
// only allow webmentions that have an author name and a timestamp
const checkRequiredFields = (entry) => {
const { author, published } = entry
return !!author && !!author.name && !!published
}
return webmentions
.filter((entry) => entry['wm-target'] === url)
.filter((entry) => allowedTypes.includes(entry['wm-property']))
.filter(checkRequiredFields)
.sort(orderByDate)
.map(clean)
});
eleventyConfig.addFilter('likesForUrl', (webmentions, url) => {
const allowedTypes = ['like-of']
return webmentions
.filter((entry) => entry['wm-target'] === url)
.filter((entry) => allowedTypes.includes(entry['wm-property']))
});
eleventyConfig.addFilter('repostsForUrl', (webmentions, url) => {
const allowedTypes = ['repost-of']
return webmentions
.filter((entry) => entry['wm-target'] === url)
.filter((entry) => allowedTypes.includes(entry['wm-property']))
});
eleventyConfig.addFilter('size', (mentions) => {
return !mentions ? 0 : mentions.length
});
eleventyConfig.addFilter('webmentionsByType', (mentions, mentionType) => {
return mentions.filter(entry => !!entry['mentionType'])
});
// Return the smallest number argument
eleventyConfig.addFilter("min", (...numbers) => {
return Math.min.apply(null, numbers);
});
// Return the keys used in an object
eleventyConfig.addFilter("getKeys", target => {
return Object.keys(target);
});
eleventyConfig.addFilter("filterTagList", function filterTagList(tags) {
return (tags || []).filter(tag => ["all", "posts"].indexOf(tag) === -1);
});
};