Display webmentions

This commit is contained in:
Devon Campbell 2024-06-20 14:25:29 -04:00
commit 91326c03fc
11 changed files with 314 additions and 15 deletions

View file

@ -7,6 +7,15 @@ const markdownItAttrs = require("markdown-it-attrs");
const markdownItEleventyImg = require("markdown-it-eleventy-img");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const embedYouTube = require("eleventy-plugin-youtube-embed");
const sanitizeHtml = require("sanitize-html");
const metadata = require("./src/_data/metadata.json");
const sanitize = (html) => {
return sanitizeHtml(html, {
allowedTags: ['b', 'i', 'em', 'strong', 'a'],
allowedAttributes: { 'a': ['href'] }
});
};
module.exports = function(eleventyConfig) {
eleventyConfig.addPlugin(embedYouTube);
@ -57,7 +66,49 @@ module.exports = function(eleventyConfig) {
return '<iframe width="560" height="315" src="https://www.youtube.com/embed/' + videoId + '" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
});
});
eleventyConfig.addFilter('webmentionsByUrl', function(webmentions, url) {
const allowedTypes = ['in-reply-to', 'like-of', 'repost-of'];
const hasRequiredFields = mention => {
const { author, published, content } = mention;
return author.name && published && content;
};
const matchesUrl = mention => {
return mention['wm-target'] === url;
};
const filteredMentions = webmentions
.filter(mention => {
const isAllowedType = allowedTypes.includes(mention['wm-property']);
if (isAllowedType) {
const isReply = mention['wm-property'] === 'in-reply-to';
if (isReply) {
return matchesUrl(mention) && hasRequiredFields(mention);
}
return matchesUrl(mention);
}
}
);
const sanitizedMentions = filteredMentions.map((mention) => {
if (mention?.content?.html) {
mention.content.html = sanitize(mention.content.html);
const mastodonInstance = metadata.author.mastodon.instance;
const mastodonUsername = metadata.author.mastodon.username;
const mentionLink = `<a href="https://${mastodonInstance}/@${mastodonUsername}">@${metadata.author.mastodon.username}</a> `;
if (mention.content.html.startsWith(mentionLink)) {
mention.content.html = mention.content.html.substring(mentionLink.length)
}
}
return mention;
});
const sortedMentions = sanitizedMentions.reduce((accumulator, mention) => {
const key = mention['wm-property'];
const curGroup = accumulator[key] ?? [];
return { ...accumulator, [key]: [...curGroup, mention] };
}, {});
console.log(sortedMentions);
return sortedMentions;
});
const markdownLibrary = markdownIt({ html: true })
.use(markdownItAnchor, { slugify })