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": "template/feed.json",

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

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

Step 2

Add the feed template:

source
+----- template
       +------- 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 ~ tags:post date:desc 1:100 "," %}
  ]
}

Step 3

Add the item template:

source
+----- template
       +------- 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://localhost:1985/feed.json

Next