# Writing a Github-hosted commenting system
Posted 26 Mar 2013. Tagged: github, jQuery and blogI just pushed the beta version of my commenting system for this blog to Github. Code-named talaria, it provides commenting for any file-identifiable1 content via Github commit message comments.
talaria works based on a number of assumptions:
- content (such as a blog post) is wrapped in a DOM
container, such as a
div
or even better anarticle
, which contains ana.permalink
- the permalink can be used to figure out the actual path and filename of the content-source (such as a markup file)
- the DOM provides the necessary nodes which talaria uses as template and reference points for comment insertion
A liquid template that enables usage of talaria would look structurally similar to the following:
...
<script type="text/javascript" src="/path/to/comments.js"></script>
...
<article>
<header><a class="permalink" href="/permalink-to-post">A post</a></header>
...
{% include comments-placeholder.html %}
</article>
...
<footer>
{% include comments-template.html %}
</footer>
...
The actual functionality is very simple and the majority of the work
is handled by the Github API, but here’s a breakdown: talaria extracts
all permalinks on the page. These are converted to the respective
paths to the content source files. It then iterates over all files,
querying the Github API for commits related to each
file. Then it retrieves the comments for each commit. Retrieved
data is cached locally2 (if supported by the browser) using
sessionStorage
. The next steps entail sorting the comments based on
their updated_at
field and rendering them using the template
snippets. Finally, talaria sets the href
for the buttons to add
comments, and, if the current URL indicates paginated
content, the comments are initially left hidden and an appropriate
“view comments” link is displayed instead, if not, the comments are
displayed.
Writing talaria was a great way to experiment with $.Deferred()
,
which are totally awesome!
-
I consider content “file-identifiable”, if the full content is contained in its own file, such as the markup source for a blog post. ↩
-
This also helps with the relatively low limit of 60 requests per hour (considering that with talaria the absolute minimum is 2 requests per page view) for unauthenticated queries against the Github API. ↩