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
- How to add a new block?
- How to add a new blog post?
- How to build reusable HTML components?
- How to implement a custom plugin?
- How to display a list of items?
- How to display a list of links?
- How to add dynamic data to a page with a plugin?
- How to use Stapy on Windows?
- How to host my static website for free?
- How to auto-deploy my website with GitHub Action?
- How to write content in Markdown?
- ✓ How to add a JSON feed?
- How to use Stapy with Apache HTTP Server?
- How to update Stapy?