Note: This post was originally written by Joel Nitta and copied here on November 29, 2021 - see the original post here for a potentially updated version.
TL;DR
- giscus is a free, open-source commenting system for blogs that uses the GitHub API
- giscus uses GitHub Discussions (not Issues) to store data
- I show how to enable giscus on a Distill blog
Like many R-bloggers these days, I have made some changes: I switched from blogdown to Distill , and from disqus to utterances . Several things about utterances appealed to me: free, open-source, no data tracking. But when I started using it, I immediately was turned off by the dual use of GitHub issues as a way to store comments. It just felt odd to have an issue that wasn’t an issue!
Fortunately, I’m not the only one to feel this way, and @laymonage
actually did something about it: there is now a very similar app to utterances, called giscus. It offers almost the same functionality, but it uses GitHub Discussions as the place to store comments instead of Issues. This makes much more sense to me.
There are several blogposts on how to enable utterances on Distill, but none that I’ve found so far on giscus. So, here goes!
Enable Discussions on your blog repo. Optionally, if you want to use a non-default Discussions category for storing giscus content, add a new category. I did this and called it “Comments”. As recommended by giscus, it’s a good idea to set the discussion format to “Announcement” so that non-authorized users can’t add content via the Discussions interface (only the giscus widget on your blog).
Install the giscus GitHub app and configure it to have access to your blog’s repo.
Go to the giscus app interface, scroll down to “configuration” and fill in the details for your blog. Once you’ve done so, further down you should see an HTML code block under “Enable giscus” populated with your information.
As described in Miles McBain’s blogpost, unfortunately in Distill, you can’t just paste the HTML directly into an Rmd file. It won’t show up. But the same work-around that he describes for utterances also happily works for giscus! Read on…
- Add an
.html
file (I’ve called mine giscus.html
) to the root of your blog repo that looks like this (and is based off of Miles’ HTML):
<script>
document.addEventListener("DOMContentLoaded", function () {
if (!/posts/.test(location.pathname)) {
return;
}
var script = document.createElement("script");
script.src = "https://giscus.app/client.js";
script.setAttribute("data-repo", "[ENTER REPO HERE]");
script.setAttribute("data-repo-id", "[ENTER REPO ID HERE]");
script.setAttribute("data-category", "[ENTER CATEGORY NAME HERE]");
script.setAttribute("data-category-id", "[ENTER CATEGORY ID HERE]");
script.setAttribute("data-mapping", "pathname");
script.setAttribute("data-reactions-enabled", "0");
script.setAttribute("data-emit-metadata", "0");
script.setAttribute("data-theme", "light");
script.setAttribute("data-lang", "en");
/* wait for article to load, append script to article element */
var observer = new MutationObserver(function (mutations, observer) {
var article = document.querySelector("d-article");
if (article) {
observer.disconnect();
/* HACK: article scroll */
article.setAttribute("style", "overflow-y: hidden");
article.appendChild(script);
}
});
observer.observe(document.body, { childList: true });
});
</script>
If you compare the above code with the HTML block in the giscus app (Fig. 3), you should be able to see how the script.setAttribute
lines above map to the key-value pairs in the HTML block in the giscus app. All we have to do is copy the contents of the HTML block over to this giscus.html
file. You can see what my giscus.html
file looks like here.
- Modify
_site.yml
so that the giscus.html
file gets loaded on every Distill article page :
output:
distill::distill_article:
includes:
in_header: giscus.html
That’s it! Or it should be anyways. I recommend trying a test comment to make sure everything is working (nobody will tell you otherwise…)
Footnotes
Corrections
If you see mistakes or want to suggest changes, please create an issue on the source repository.
Reuse
Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/jhelvy/distillery, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".
Citation
For attribution, please cite this work as
Nitta (2021, Nov. 24). The Distillery: Enable giscus in Distill. Retrieved from https://distillery.rbind.io/posts/2021-11-24_using-giscus
BibTeX citation
@misc{nitta2021enable,
author = {Nitta, Joel},
title = {The Distillery: Enable giscus in Distill},
url = {https://distillery.rbind.io/posts/2021-11-24_using-giscus},
year = {2021}
}