From 3c8ac5ebed71eca5302a11c09a1117f81db9137e Mon Sep 17 00:00:00 2001 From: Benji Encalada Mora Date: Wed, 30 Oct 2019 13:24:59 -0500 Subject: [PATCH 1/5] Add jsonfeed to metadata --- _data/metadata.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/_data/metadata.json b/_data/metadata.json index 9fb34c4..7a0c03e 100644 --- a/_data/metadata.json +++ b/_data/metadata.json @@ -9,6 +9,10 @@ "url": "https://myurl.com/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" From 6b0b772b999e29cd046b87bdd05e9dfd4c1bfe1f Mon Sep 17 00:00:00 2001 From: Benji Encalada Mora Date: Wed, 30 Oct 2019 13:25:58 -0500 Subject: [PATCH 2/5] Add filter for HTML content --- .eleventy.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.eleventy.js b/.eleventy.js index 1d3dbc1..2f610c6 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -19,6 +19,16 @@ module.exports = function(eleventyConfig) { return DateTime.fromJSDate(dateObj, {zone: 'utc'}).toFormat('yyyy-LL-dd'); }); + // Content for jsonfeed + eleventyConfig.addFilter('jsonContent', content => { + if (content) { + content = content.replace(/(\r\n|\n|\r)/gm, ""); + content = content.replace(/\t/g, "\\t"); + content = content.replace(/"/g, '\\"'); + } + return content; + }); + // Get the first `n` elements of a collection. eleventyConfig.addFilter("head", (array, n) => { if( n < 0 ) { From a34bf51ce1a3a2e193736c31c64e15336f44c2a1 Mon Sep 17 00:00:00 2001 From: Benji Encalada Mora Date: Wed, 30 Oct 2019 13:26:42 -0500 Subject: [PATCH 3/5] Add jsonfeed template --- feed/json.njk | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 feed/json.njk diff --git a/feed/json.njk b/feed/json.njk new file mode 100644 index 0000000..85200d0 --- /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 | jsonContent | 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 %} + ] +} From fb7383c3ed4b26c7aaface1aa2b06c7401bb730d Mon Sep 17 00:00:00 2001 From: Benji Encalada Mora Date: Wed, 30 Oct 2019 13:27:38 -0500 Subject: [PATCH 4/5] Add jsonfeed to base template --- _includes/layouts/base.njk | 1 + 1 file changed, 1 insertion(+) diff --git a/_includes/layouts/base.njk b/_includes/layouts/base.njk index 85c274b..29a33b6 100644 --- a/_includes/layouts/base.njk +++ b/_includes/layouts/base.njk @@ -8,6 +8,7 @@ +
From 12219c480fb26a534cd65aeaac9f51557a8dbc3e Mon Sep 17 00:00:00 2001 From: Benji Encalada Mora Date: Mon, 20 Jan 2020 01:26:45 -0600 Subject: [PATCH 5/5] use JSON.stringify for content --- .eleventy.js | 10 ++++------ feed/json.njk | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/.eleventy.js b/.eleventy.js index 2f610c6..77c9143 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -20,13 +20,11 @@ module.exports = function(eleventyConfig) { }); // Content for jsonfeed - eleventyConfig.addFilter('jsonContent', content => { - if (content) { - content = content.replace(/(\r\n|\n|\r)/gm, ""); - content = content.replace(/\t/g, "\\t"); - content = content.replace(/"/g, '\\"'); + eleventyConfig.addFilter('jsonStringify', content => { + if (!content) { + content = ""; } - return content; + return JSON.stringify(content); }); // Get the first `n` elements of a collection. diff --git a/feed/json.njk b/feed/json.njk index 85200d0..97e0841 100644 --- a/feed/json.njk +++ b/feed/json.njk @@ -19,7 +19,7 @@ eleventyExcludeFromCollections: true "id": "{{ absolutePostUrl }}", "url": "{{ absolutePostUrl }}", "title": "{{ post.data.title }}", - "content_html": "{{ post.templateContent | jsonContent | safe }}", + "content_html": {{ post.templateContent | jsonStringify | safe }}, "date_published": "{{ post.date | rssDate }}", "tags": [ {%- for c in post.data.category %}