Using the Fediverse to Add Comments to a Static Website
Since I migrated this website to HUGO I have been searching for an elegant way to allow comments on our blog posts. I looked at most solutions given in the Hugo Documentation. They all seemed overly complex and involved third party services. So I started thinking if it would be possible to use statuses on Mastodon. It turns out I was not alone and this has been done before several times. I went with a system described by Daniel Pecos Martínez.
To start with, I added a fediverse value to archtypes/default.md. It is empty by default and on creation of a new post will contain the status ID once a status and blog post has been created.
+++
title = '{{ replace .File.ContentBaseName "-" " " | title }}'
date = {{ .Date }}
draft = false
fediverse = ""
authors = [""]
+++
Next add parameters to config/_default/params.toml
[Fediverse]
enable = true
host = "foostodon.org"
user = "sinodun"
Download fontawesome-webfont.ttf, fontawesome-webfont.woff2 and fontawesome-webfont.woff into static/css/fonts/
Download mastodon-comments.js and purify.min.js into static/js/. Download font-awesome.min.css into static/css/. Edit font-awesome.min.css and change the path to the fonts from ../fonts/ to fonts/. I used vim and %s/\.\.\/fonts/fonts/g.
Include these scripts and stylesheets in the one of the files in layouts/partials/header/
<script defer src="/js/purify.min.js"></script>
<link rel="stylesheet" href="/css/font-awesome.min.css">
<script defer src="/js/mastodon-comments.js"></script>
Add this to the layouts/_default/single.html after the {{ .Content }} line
{{ if .Params.fediverse }}
<mastodon-comments host="{{ .Site.Params.fediverse.host }}" user="{{ .Site.Params.fediverse.user }}" tootId={{ .Params.fediverse }} style="width : 1024px"></mastodon-comments>
{{ end }}
As Daniel Pecos Martínez mentions there is a chicken and egg problem with the post link and the mastodon status ID. So in the end I wrote a script to post the status, get the ID and then publish the blog post.