Charlie Park!

(or, rather, his tumblelog)

JSON Feeds in Eleventy

I’ve wanted to set up an RSS/JSON feed for this tumblelog for a little bit, but hadn’t made the time. I just took care of it, and it was really straightforward.

I started with Andy Bell’s quick intro on making a JSON Feed in Eleventy, but then made some modifications to help it conform more to the JSON Feed spec.

Here’s what I ended up with, in a file called feed.njk in my root directory:

permalink: "/feed.json"
"version": "",
"title": "Charlie Park!",
"home_page_url": "",
"feed_url": "",
"icon": "",
"favicon": "",
"language": "en-US",
"items": [
{% for item in | reverse %}
"id": "{{ item.url }}",
"url": "{{ item.url }}",
"title": "{{ }}: {{ item.url }}",
"content_html": "{{ item.templateContent | replace(stet'stet"stet'stet, "\"") | replace("\n", "") | replace("\\", "\\\\") | safe }}"
}{% if not loop.last %},{% endif %}
{% endfor %}

That string of replace()s in the last line of the for loop is to handle some string transformations so that the post content better functions with the JSON spec. Since I use smart quotes in my posts, it won’t mess up my quotation marks. Just be aware that you might need to keep an eye out on those.