Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JSON feed template for eleventy-plugin-rss
const pluginRss = require("@11ty/eleventy-plugin-rss");
module.exports = function(eleventyConfig) {
// Lots of other stuff probably goes here
eleventyConfig.addCollection("allUpdates", function(collection) {
return collection.getFilteredByGlob(["posts/*.md", "photos/*.md", "notes/*.md"]).sort(function(a, b) {
return b.date - a.date;
});
});
// Lots of other stuff probably goes here
}
---
permalink: feed.json
metadata:
title: My Site Title
url: https://my.url
author:
name: My Name
email: me@my.url
avatar: /path/to/pic.jpg
feed:
subtitle: My feed subtitle
filename: feed.json
path: feed/feed.json
url: https://my.url/feed.json
id: https://my.url
---
{
"version": "https://jsonfeed.org/version/1",
"title": "{{ metadata.title }}",
"home_page_url": "{{ metadata.url }}",
"feed_url": "{{ metadata.url }}/feed.json",
"favicon": "{{ metadata.url }}/static/img/icon.png",
"icon": "{{ metadata.url }}/static/img/icon.png",
"description": "{{ metadata.feed.subtitle }}",
"author": {
"name": "{{ metadata.author.name }}",
"url": "{{ metadata.url }}",
"avatar": "{{ metadata.url }}{{ metadata.author.avatar }}"
},
"items": [
{% set comma = joiner(",") -%}
{%- for post in collections.allUpdates -%}
{%- set absolutePostUrl -%}{{ post.url | url | absoluteUrl(metadata.url) }}{%- endset -%}
{%- set title -%}{{ post.data.title }}{%- endset -%}
{%- set banner -%}{{ post.data.hero.image }}{%- endset -%}
{%- set summary -%}{{ post.data.summary }}{%- endset -%}
{%- set heroimg -%}{{ post.data.hero.image }}{%- endset -%}
{%- set herocap -%}{{ post.data.hero.caption }}{%- endset -%}
{%- set reg1 = "\"" -%}
{%- set rep1 %}\"{% endset -%}
{%- set reg2 = "\\\\\\" -%} {# Handling one weird edge case! #}
{%- set rep2 %}\\\\\\{% endset -%}
{%- set reg3 = "\n" -%}
{%- set rep3 %}\n{% endset -%}
{%- set reg4 = "\r" -%}
{%- set rep4 %}\r{% endset -%}
{%- set reg5 = "\t" -%}
{%- set rep5 %}\t{% endset -%}
{{- comma() }}
{ "id": "{{ absolutePostUrl }}",
"content_html": "{%- if heroimg -%}<figure><img src=\"{{ heroimg | url | absoluteUrl(metadata.url) }}\" alt=\"{{ herocap }}\"><figcaption>{{ herocap }}</figcaption></figure>{%- endif -%}{{- post.templateContent
| htmlToAbsoluteUrls(absolutePostUrl)
| replace(reg1, rep1)
| replace(reg2, rep2)
| replace(reg3, rep3)
| replace(reg4, rep4)
| replace(reg5, rep5)
| safe -}}",
"url": "{{ absolutePostUrl }}",
"date_published": "{{ post.date | rfc3339 }}"
{%- if summary %},
"summary": "{{ post.data.summary }}"
{% endif -%}
{%- if not "note" in post.data.tags %},
"title": "{{ title }}"
{% endif -%}
{%- if banner %},
"banner_image": "{{ metadata.url }}{{ banner }}"
{% endif -%},
"author": {
"name": " {{ metadata.author.name }}",
"url": "{{ metadata.url }}",
"avatar": "{{ metadata.author.avatar | url | absoluteUrl(metadata.url) }}"
},
}
{% endfor %}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.