For the last couple of years this site has run on Jekyll, the Ruby-based static site generator that powers GitHub Pages. The speed, security, easy version control and traffic-proof nature of a static site means I’ll never use anything else unless it absolutely requires dynamic content.

The only downside, for someone sharing their work, their hobbies and their thoughts, is a lack of feedback. No server-side processing means no dynamic content, means no comment system. Writing soon feels like whimpering into an uncaring void.

Here’s how I added a comment system using GitHub pull requests.

Looking for Solutions

The traditional solution is to embed a third party discussion platform like Disqus using JavaScript, but this is unsatisfactory for a few reasons. Embedding Disqus means using Disqus’s branding, Disqus accounts, and worst of all Disqus’s user tracking code. Security minded users with Ghostery or adblockers installed might not even see the comment section. Comments may not be picked up by search engines as they don’t exist on page load. And what if Disqus goes out of business? There go your comments.

The attraction of a static site all neatly version controlled on GitHub is owning all your own data, and that’s what we need to keep.

Eduardo Bouças has put thought into this problem and his solution is Staticman, a webservice that accepts post requests and pushes the data to your git repo (or creates a pull request if you want moderation). It looks awesome, and if you’re hosting on GitHub Pages you even get dynamic updating (since pages update on git push) – but I’m still wary of relying on another third party service. It would be great to eliminate even that bit of server side processing.

And honestly, filling in a quick form to post a comment has always seemed a bit too easy to me. Make it too simple to leave a stranger feedback and you end up with YouTube. Wouldn’t it be cool to ensure only other nerds could comment on your code?

Enter GitHub

That got me thinking about GitHub’s online code editor. I often use it to correct the numerous typos in my README files, but have you ever noticed there’s an edit button on other people’s files too?

When you don’t have write access to a repo, clicking that button automatically creates a fork and opens the editor. You can commit your changes right there then send a pull request on the next page. Fantastic! We can use this to allow others to edit our pages, adding their comments as they wish. No need to bother writing a comment edit form, or handling server side processing; GitHub already does it.

The more I thought about it the better it seemed as a solution:

Increased barrier to entry (effort) Without making visitors jump through too many hoops, it’s good to make it slightly harder to leave a comment than a simple text input. If you need to put time and thought into formatting a response, you’ll also put time and thought into what you want to say. Increased barrier to entry (skill) When you write about technical subjects, the best insights are going to come from a technical audience. Although the Liquid template markup is simple, the mechanics of commiting a change and sending a pull request ensure a minimum level of understanding. Moderation Comment moderation is controversial. Some people believe in absolute free speech even in private, and that any moderation system amounts to censorship. Not me! This is my website, I pay the bills. I’ll publish what I want on here. Transparency Although I don’t want every comment automatically posted, it’s only fair that moderation is transparent. With GitHub comments there’s a public record of submitted comments in the form of pull requests. Prove I’m too cowardly to debate you by linking to the rejected PR. Flexibility You can provide Markdown for simplicity, but there’s really no need to filter or escape your commenters’ full HTML . You aren’t processing it so there’s no risk of injection vulnerabilities, and you’re going to manually review the code before merging and going live so you can check for bad behaviour then.

The downside is that maybe the barriers to entry are too high, and you won’t get comments at all. But to me, that’s much preferable to getting low effort comments.

Implementation

The simplest implementation would go something like this:

<a href= "https://github.com/simlrh/nourish.je/edit/master/{{ page.path }}" > Leave a comment </a>

Leave a comment

But that would be a little too cruel to your visitors. Following the link takes them to the top of a long page of code, and when they find the right spot they have to mark up the comment’s HTML on their own. No one’s going to do it.

So I put together a small Jekyll plugin to make it easier. Install jekyll-github-comments with

gem install jekyll-github-comments

Add to the gems in _config.yaml, and set your git repo name in the configuration options:

gems: - jekyll - github - comments github_comments: repo: "simlrh/nourish.je"

You can also set the branch you want pull requests to go to, but the default is master .

No templates are included with the plugin, so add your own that suit your site. They go in _includes and by default they’re named comments.html for the comment section and comment.html for the individual comments. You can set a different template name in the configuration. See the README for available variables or this gist for an example.

Usage

Now it’s ready to use. To add a comment section to a page, use the Liquid tag {% responses %} . It’s not required, but I put a little comment in there to help people get started:

{% responses %} <!-- Fill in this template to comment --> {% response author = "Your Name" email = "your.email@example.com" date = "YYYY-MM-DD HH:MM:SS" %} {% endresponse %} {% endresponses %}

That {% response %} tag isn’t rendered yet since the content is blank. But when a visitor fills in their name, email & post date, and puts their message inside the block, it will render like this:

That’s a much more manageable amount of code to write.

Not only is the code simple, so is finding where to put it; the “Reply” link of each comment, and the “Leave a comment” link of the comment section as a whole, takes you to the exact line of the file where your comment should go.

Comments can be nested, and can optionally support Markdown by running the comment content through the markdownify filter in your template:

{% responses %} {% response author = "John" email = "john@example.com" date = "2016-11-22 19:36:48" %} I'm a top level comment {% responses %} {% response author = "Dave" email = "dave@example.com" date = "2016-11-22 19:37:38" %} I'm a reply {% endresponse %} {% response author = "Chris" email = "chris@example.com" date = "2016-11-22 19:38:05" %} > I'm a top level comment *Not me*, I'm **nested!** {% endresponse %} {% endresponses %} {% endresponse %} {% endresponses %}

renders to:

Conclusion

My static site now has a nice looking comment section that perfectly matches the design. Comments are stored in plain files, but they can still be restyled via the templating system. Everything is under version control. No server side processing is required, besides what is provided by GitHub, which the site relied on anyway.

The slight barrier to entry displays just the right level of passive aggression to visitors, and the nerdiness of the system makes me feel smug and self-satisfied.

If you use jekyll-github-comments on your own site please let me know… in the comments!