Skip to content

Instantly share code, notes, and snippets.

@gr36
Created February 17, 2024 15:11
Show Gist options
  • Select an option

  • Save gr36/8156f93c643e4e5f558bb7103b23e7d8 to your computer and use it in GitHub Desktop.

Select an option

Save gr36/8156f93c643e4e5f558bb7103b23e7d8 to your computer and use it in GitHub Desktop.
This is the webmqntiosn filter and webventions partial for my 11ty blog
eleventyConfig.addFilter("countWebmentions", function(webmentionsFilePath, url) {
// Load and parse the webmentions JSON file
const data = JSON.parse(fs.readFileSync(webmentionsFilePath, 'utf8'));
// Filter webmentions based on the provided URL
const filteredWebmentions = data.children.filter(entry => {
return entry['like-of'] === url || entry['in-reply-to'] === url || entry['repost-of'] === url;
});
// Initialize counts for each type
let likeCount = 0;
let replyCount = 0;
let repostCount = 0;
// Loop through each entry in the filtered webmentions
filteredWebmentions.forEach(entry => {
// Check if the entry matches the given URL for like, reply, or repost
if (entry['like-of'] === url) {
likeCount++;
}
if (entry['in-reply-to'] === url) {
replyCount++;
}
if (entry['repost-of'] === url) {
repostCount++;
}
});
// Return an object containing the counts
return {
likeCount,
replyCount,
repostCount
};
});
{% set webmentionsFilePath = '_cache/webmentions.json' %}
{% set currentPageUrl = page.url | url | absoluteUrl(metadata.url) %}
{% set webmentionCounts = webmentionsFilePath | countWebmentions(currentPageUrl) %}
Likes: {{ webmentionCounts.likeCount }}
Replies: {{ webmentionCounts.replyCount }}
Reposts: {{ webmentionCounts.repostCount }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment