diff --git a/.eleventy.js b/.eleventy.js index d4774be..376f56c 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -24,6 +24,14 @@ module.exports = function(eleventyConfig) { return DateTime.fromJSDate(dateObj, {zone: 'utc'}).toFormat('yyyy-LL-dd'); }); + // Content for jsonfeed + eleventyConfig.addFilter('jsonStringify', content => { + if (!content) { + content = ""; + } + return JSON.stringify(content); + }); + // Get the first `n` elements of a collection. eleventyConfig.addFilter("head", (array, n) => { if( n < 0 ) { diff --git a/_data/metadata.json b/_data/metadata.json index 8d2a754..f94ee8e 100755 --- a/_data/metadata.json +++ b/_data/metadata.json @@ -8,6 +8,10 @@ "path": "/feed/feed.xml", "id": "https://myurl.com/" }, + "jsonfeed": { + "path": "/feed/feed.json", + "url": "https://myurl.com/feed/feed.json" + }, "author": { "name": "Your Name Here", "email": "youremailaddress@example.com" diff --git a/_includes/layouts/base.njk b/_includes/layouts/base.njk index 020bfe6..44724b5 100644 --- a/_includes/layouts/base.njk +++ b/_includes/layouts/base.njk @@ -8,6 +8,7 @@ +
diff --git a/feed/json.njk b/feed/json.njk new file mode 100644 index 0000000..97e0841 --- /dev/null +++ b/feed/json.njk @@ -0,0 +1,38 @@ +--- +permalink: feed/feed.json +eleventyExcludeFromCollections: true +--- +{ + "version": "https://jsonfeed.org/version/1", + "title": "{{ metadata.title }}", + "home_page_url": "{{ metadata.url }}", + "feed_url": "{{ metadata.jsonfeed.url }}", + "description": "{{ metadata.description }}", + "author": { + "name": "{{ metadata.author.name }}", + "url": "{{ metadata.author.url }}" + }, + "items": [ + {%- for post in collections.posts %} + {% set absolutePostUrl %}{{ post.url | url | absoluteUrl(metadata.url) }}{% endset -%} + { + "id": "{{ absolutePostUrl }}", + "url": "{{ absolutePostUrl }}", + "title": "{{ post.data.title }}", + "content_html": {{ post.templateContent | jsonStringify | safe }}, + "date_published": "{{ post.date | rssDate }}", + "tags": [ + {%- for c in post.data.category %} + "{{ c }}" + {%- if not loop.last -%} + , + {%- endif -%} + {%- endfor %} + ] + } + {%- if not loop.last -%} + , + {%- endif -%} + {%- endfor %} + ] +}