Static Site Generator

How-to: How to add a JSON feed?

How to add a JSON feed?

Step 1

Add a new page with the path feed.json:

source
+----- pages
       +---- feed.json.json
{
  "template": "content/feed.json",

  "feed.item": "content/feed/item.json",

  "feed.title": "Articles about Stapy",
  "feed.author": "Stapy"
}

Step 2

Add the feed template:

source
+----- content
       +------- feed.json
{
  "version": "https://jsonfeed.org/version/1.1",
  "title": "{{ feed.title }}",
  "authors": [
    {
      "name": "{{ feed.author }}"
    }
  ],
  "home_page_url": "{{ url }}",
  "feed_url": "{{ url }}{{ _path }}",
  "items": [
{% feed.item delimiter:"," ~ SELECT ITEMS 1-100 WHERE "post" in tags ORDER BY date desc %}
  ]
}

Step 3

Add the item template:

source
+----- content
       +------- feed
                +--- item.json
{
  "id": "{{ url }}{{ $_path }}",
  "url": "{{ url }}{{ $_path }}",
  "title": "{{ $title }}",
  "content_html": "{{ $intro }}",
  "authors": [
    {
      "name": "{{ $author }}"
    }
  ],
  "date_published": "{{ $date_rfc_3339 }}"
}

Step 4

Add a plugin to format the post date (YYYY-MM-DD) to RFC 3339 (YYYY-MM-DDTHH:MM:SS.ssZ):

plugins
+------ date.py
from datetime import datetime


def page_data_merged(data: dict) -> dict:
    if 'date' not in data:
        return data
    date = datetime.strptime(data['date'], '%Y-%m-%d')
    data['date_rfc_3339'] = date.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-4] + 'Z'
    return data

Step 5

Go to http://127.0.0.1:1985/feed.json

Next