garden/Clippings/Webmention Setup for Eleventy.md
2024-11-30 00:21:52 -06:00

5.3 KiB
Raw Blame History

author title source clipped published topics tags
Chris Burnell Webmention Setup for Eleventy https://chrisburnell.com/article/webmention-eleventy-setup/ 2024-06-25 2022-08-25
clipping

Webmention Setup for Eleventy

Heres a quick run-through of how I retrieve and utilise Webmentions with my Eleventy website.

Unless you already have your own Webmention receiver, sign up for one and add the secret key/token to your project. Below are two examples:

If youre using Webmention.io, add your API Key (found on your settings page) to your project as an environment variable, i.e. in a .env file in the root of your project:

WEBMENTION_IO_TOKEN=njJql0lKXnotreal4x3Wmd

If youre using go-jamming, once youve set up your server and defined your token, youll need add it to your project as an environment variable, i.e. in a .env file in the root of your project:

GO_JAMMING_TOKEN=njJql0lKXnotreal4x3Wmd

Install the eleventy-cache-webmentions package from npm:

npm install @chrisburnell/eleventy-cache-webmentions

Create a config file. You may wish to put this file in your Global Data Folder so you can access the data across your Eleventy project. The location and filename of your config file are up to you, as long as its somewhere in your project that you can access with JavaScript using require().

If youre using Webmention.io:

const { defaults } = require("@chrisburnell/eleventy-cache-webmentions")


require("dotenv").config()

module.exports = Object.assign({}, defaults, {
	domain: "https://EXAMPLE.COM",
	feed: `https://webmention.io/api/mentions.jf2?domain=EXAMPLE.COM&token=${process.env.WEBMENTION_IO_TOKEN}&per-page=9001`,
	key: "children",
})

If youre using go-jamming:

const { defaults } = require("@chrisburnell/eleventy-cache-webmentions")


require("dotenv").config()

module.exports = Object.assign({}, defaults, {
	domain: "https://EXAMPLE.COM",
	feed: `https://JAM.EXAMPLE.COM/webmention/EXAMPLE.COM/${process.env.GO_JAMMING_TOKEN}`,
	key: "json",
})

Add both to Eleventy by adding them to your Eleventy Config:

Note: The path to your Webmentions config depends on where your Data files live, e.g. ./src/data/configWebmentions.js.

const pluginWebmentions = require("@chrisburnell/eleventy-cache-webmentions")
const configWebmentions = require("../path_to_your_config/configWebmentions.js")

module.exports = function(eleventyConfig) {
	eleventyConfig.addPlugin(pluginWebmentions, configWebmentions)
}

Add some Directory Specific Data Files wherever your pages and/or posts live. For example, if your pages all live in a pages/ folder, you would add the following code block to a pages.11tydata.js file inside pages/:

const { getWebmentions } = require("@chrisburnell/eleventy-cache-webmentions")
const configWebmentions = require("../path_to_your_config/configWebmentions.js")

module.exports = {
	eleventyComputed: {
		webmentions: (data) => {
			return getWebmentions(configWebmentions, configWebmentions.domain + data.page.url)
		},
	}
}

Webmentions are now attached to each page!

You can access them quite easily on a given page:

{% for webmention in webmentions %}
	
	{{ webmention.author.name }} sent this page a {{ webmention | getType }}
{% endfor %}

And even when looping through something like a collection:

{% for item in collections.pages %}
	
	<h2>{{ item.data.title }}</h2>
	<p>This page has {{ item.data.webmentions.length }} webmention{{ 's' if item.data.webmentions.length > 1 }}</p>
{% endfor %}

How you want to filter the array of Webmentions attached to each page is up to you, but I recommend using the getWebmentionsByTypes Filter to split Webmentions into groups categorised by type—this will make it easier to figure out which Webmentions are binary interactions (e.g. likes, reposts) and which have richer content you might want to display (e.g. mentions, replies).

{% set reactions = webmentions | getWebmentionsByTypes(['like-of', 'repost-of', 'bookmark-of']) %}

{% set replies = webmentions | getWebmentionsByTypes(['mention-of', 'in-reply-to']) %}

And that's pretty much all there is to it! Let me know if you have any suggestions or issues, and feel free to contribute back to the Eleventy plugin that is the workhorse behind this implementation over on GitHub or just get in touch.