Getting JSON Feed in Jekyll

With regard to the previous post, here’s how I cooked up a Jekyll template for JSON Feed:

---
layout: null
---
{
  "version" : "https://jsonfeed.org/version/1",
  "title" : "{{ site.title }}",
  "home_page_url" : "{{ site.url }}",
  "feed_url" : "{{ "/feed.json" | prepend: site.baseurl | prepend: site.url }}",
  "author" : {
    "url" : "https://twitter.com/frippz",
    "name" : "Fredrik Frodlund"
  },
  "icon" : "{{ "/apple-touch-icon.png" | prepend: site.baseurl | prepend: site.url }}",
  "favicon" : "{{ "/favicon-32x32.png" | prepend: site.baseurl | prepend: site.url }}",
  "items" : [
  {% for post in site.posts limit:10 %}
    {
      "title" : {{ post.title | jsonify }},
      "date_published" : "{{ post.date | date_to_rfc822 }}",
      "id" : "{{ post.url | prepend: site.baseurl | prepend: site.url }}",
      "url" : "{{ post.url | prepend: site.baseurl | prepend: site.url }}",
      {% if post.external-link %}
      "external_url" : "{{ post.external-link }}",
      {% endif %}
      "author" : {
        "name" : "Fredrik Frodlund"
      },
      "content_html": {{ post.content | jsonify }}
    }{% if forloop.last == false %},{% endif %}
  {% endfor %}
  ]
}

It’s a pretty quick and dirty port of my feed.xml template, but it seems to work. You can get the above code snippet directly on Github as well, but the syntax highlighter doesn’t like the YAML front matter and Liquid template tags too much, so it looks a bit ugly.

Update (2017-05-19): I’ve updated the code snippet a bit with more Jekyll tags for a more dynamic solution (like site.url and so on). Liquid template offers a great filter called jsonify that I’m using wherever applicable.