Created
February 17, 2024 15:11
-
-
Save gr36/8156f93c643e4e5f558bb7103b23e7d8 to your computer and use it in GitHub Desktop.
This is the webmqntiosn filter and webventions partial for my 11ty blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| }; | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {% 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