A Mastodon/Fediverse comment section for your Zola blog
27.01.2024This blogpost is about how to add a Mastodon/Fediverse comment section to your Zola blog with just a few simple steps.
Carl Schwan wrote a very nice piece of JavaScript Didn’t think I’d ever say something as positive as this about JavaScript. So far my contact points with it have been less than pleasant and I try to avoid it wherever possible. and posted it for everyone to use, re-use, modify, et cetera:
https://carlschwan.eu/2020/12/29/adding-comments-to-your-static-blog-with-mastodon/
This script has been improved multiple times now by different people which is awesome in and of itself. So a big thank you goes not only to Carl Schwan for kicking this thing off but especially also to Veronica Olsen and Daniel Pecos who both took the time and wrote down their experiences with this system.
I love this kind of stuff – someone comes up with a working prototype, others like the ideas or the purpose behind it and also feel inclined to work on it. Funny how nice the world can be sometimes. In the end I did the same and adapted it to my situation.
How-to prepare your blog
Here’s what you need to do to be able to implement this functionality on your Zola-powered blog as well.
New comments shortcode
Just add a comments.html
shortcode to your Zola blog project.
Create a file with this name inside the templates/shortcodes
folder.
You may need to create these folders first.
Copy the following content and paste it into the file:
comments.js
file.
This has no real reason besides readability and personal style preference.
It’s also a lot easier for you to adapt it to your needs, should you for example want to change your config variable names.
Comments
Respond to this post with an account on the Fediverse (like Mastodon).
Load comments
Loading comments relies on JavaScript. Try enabling JavaScript and reloading, or visit the original post on Mastodon.
You need JavaScript to view the comments.
JavaScript for comments loading
Copy the code below and paste it into a new comments.js
file in the static/comments
directory.
I’ve adapted the JavaScript by Carl Schwan a bit to my liking. So much for avoiding JavaScript as much as possible… Somehow the structure of the resulting HTML elements and CSS classes was hard for me to understand and I’ve decided to rework them. The resulting structure can be found further down in the paragraphs on CSS styling, so I won’t explain it here in detail. After a few hours of back and forth between JS and CSS, here’s what I came up with:
"load-comment""click", loadComments;
Hygiene treatment
Next, you want to use DOMPurify to sanitise the results.
Be sure to grab an up to date version.
You can always find the most recent release here.
Also don’t forget to update your DOMPurify file from time to time.
Go ahead and download it and put it in your static/comments/
folder.
If you’re unsure how to proceed here:
Just download the purify.min.js
and purify.min.js.map
files from inside the dist
folder in the linked git repository and put them in the above-mentioned folder.
Configure all the things
Finally, you need to add some info to your config.toml, namely:
[]
= "ruhr.social"
= "laubblaeser"
Replace the values of host
and username
with your appropriate information.
Styles ohne Ende
Of course our comment section won’t look nice now. Here’s my SCSS extension file for good measure. Feel free to take it up from where it is and change it however you like.
I’m no web developer by any means (besides this personal project you’re currently visiting) and there’s probably some weird and/or redundant stuff in there. Oh, and it’s of course also influenced by how my blog’s CSS is structured in general. It works for now, so I’m happy after all.
{
{
:;
:;
}
{
:;
:;
:;
:;
:;
{
:;
}
{
:;
}
{ // all meta information of the comment
:;
:;
:;
:;
:;
:;
:;
:;
{ // the link to the author's profile
:;
:;
{ // author's avatar
:;
{ // actual author's avatar picture
:;
:;
:;
:;
:;
}
}
}
{ // displayInstance, timestamp, and faves
:;
:;
{ // author's displayed name, not username
:;
:;
}
{ // author's instance
}
{ // timestamp
}
{ // fav counter
}
}
}
{ // actual comment content
:;
:;
:;
:;
:;
}
}
}
How-to use it in posts
In your blog posts you can now call the shortcode at some point where you like your comment section to appear. Normally that’s the end of the post.
{{ comments(id="123456789") }}
Replace the value of id
with your Mastodon post id.
But where to get the id
from if you haven’t published anything yet?
Well, Daniel Pecos has written on this chicken-or-egg problem and ended up with the following workflow:
- Publish the post without Fediverse discussion enabled (no toot ID)
- Publish the toot containing the URL to the post and grab its ID
- Change the post to include the toot ID
- Republish the website
This workflow makes sense to me: You want to make your new post public in the fediverse and want people to read it. Whoever comes from there will likely comment there anyways. And those people who stumble upon your website by other means will have to wait a minute or so until the comment section is fully working. That’s absolutely fine and a straightforward approach to deal with this.
And that’s it, really. Feel free to leave a comment if this post was helpful to you. ;)
Comments
Respond to this post with an account on the Fediverse (like Mastodon).