Enable giscus in Distill

How to use the giscus commenting system on a Distill blog

true
2021-11-24

TL;DR

Image by [Adam Solomon](https://unsplash.com/@solomac) on [unsplash](https://unsplash.com/photos/WHUDOzd5IYU).

Figure 1: Image by Adam Solomon on unsplash.

Like many R-bloggers these days, I have made some changes: I switched from blogdown to Distill 1, and from disqus to utterances 2. 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 3 on how to enable utterances on Distill, but none that I’ve found so far on giscus. So, here goes!

  1. 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).

  2. Install the giscus GitHub app and configure it to have access to your blog’s repo.

  3. 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.

[giscus](https://giscus.app/) configuration menu.

Figure 2: giscus configuration menu.

[giscus](https://giscus.app/) HTML block. Once you fill in the fields in the configuration menu, the parts starting with `[ENTER ...]` will get automatically populated.

Figure 3: giscus HTML block. Once you fill in the fields in the configuration menu, the parts starting with [ENTER ...] will get automatically populated.

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…

  1. 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.

  1. Modify _site.yml so that the giscus.html file gets loaded on every Distill article page 4:
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…)

Last updated

2021-11-29 10:35:23 JST

Details

source code, R environment


  1. blogdown and Distill are R packages for making websites. In a nutshell, Distill is much simpler to use than blogdown, at the cost of some design flexibility. For more about making the switch, you can get caught up with posts from Thomas Mock, Frie Preu, Lisa Lendway, and Andreas Handel.↩︎

  2. disqus and utterances are tools that let users comment on blog posts. Recently many R-bloggers have been moving away from disqus because it has a habit of tracking user’s data and causing page bloat. More recently, when I checked on my disqus account (in the process of migrating away!), it had a option to “opt-out” of data tracking, but that means data-tracking is on by default.↩︎

  3. For example, Vebash Naidoo’s tutorial and Michael McCarthy’s post describing how to control the location of the comments section.↩︎

  4. The _site.yml file is longer than this, but I’m just showing the relevant code to add. You can see my _site.yml file here.↩︎

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/joelnitta/joelnitta-home, 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). Joel Nitta: Enable giscus in Distill. Retrieved from https://joelnitta.com/using-giscus

BibTeX citation

@misc{nitta2021enable,
  author = {Nitta, Joel},
  title = {Joel Nitta: Enable giscus in Distill},
  url = {https://joelnitta.com/using-giscus},
  year = {2021}
}