From 3955be2de4be9d5d298a0eaee308a4f91e2eb672 Mon Sep 17 00:00:00 2001 From: hugmanrique Date: Wed, 17 May 2017 18:09:58 -0400 Subject: [PATCH 01/13] JSON feed Liquid schema --- lib/jekyll-feed/feed.json | 63 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 lib/jekyll-feed/feed.json diff --git a/lib/jekyll-feed/feed.json b/lib/jekyll-feed/feed.json new file mode 100644 index 00000000..0ccf0d82 --- /dev/null +++ b/lib/jekyll-feed/feed.json @@ -0,0 +1,63 @@ +{ + "version": "https://jsonfeed.org/version/1", + {% assign site_title = site.title | site.name } + {% if site_title %} + "title": {{ site_title | smartify | json }}, + {% endif %} + "home_page_url": "{{ '/' | absolute_url }}", + "feed_url": "{{ page.url | absolute_url }}", + {% if site.description } + "description": {{ site.description | json }}, + {% endif %} + {% if site.author %} + "author": { + "name": {{ site.author.name | default: site.author | json }}, + {% if site.author.uri %} + "url": {{ site.author.uri | json }}, + {% endif %} + } + {% endif %} + "items": [ + {% assign posts = site.posts | where_exp: "post", "post.draft != true" %} + {% for post in posts limmit: 10 %} + { + "id": {{ post.id | absolute_url | json }}, + "url": "{{ post.url | absolute_url }}", + "title": {{ post.title | smartify | strip_html | normalize_whitespace | json }}, + {% if post.excerpt and post.excerpt != empty %} + {% assign summary = post.excerpt | strip_html | normalize_whitespace | json %} + "content_html": {{ summary }}, + "summary": {{ summary }}, + {% endif %} + {% assign post_image = post.image.path | default: post.image %} + {% if post_image %} + {% unless post_image contains "://" %} + {% assign post_image = post_image | absolute_url | xml_escape %} + {% endunless %} + "image": "{{ post_image }}", + {% endif %} + "date_published": "{{ post.date | date_to_xmlschema }}", + "date_modified": "{{ post.last_modified_at | default: post.date | date_to_xmlschema }}", + {% assign post_author = post.author | default: post.authors[0] %} + {% if post_author %} + {% assign post_author = site.data.authors[post_author] | default: post_author %} + {% assign post_author_uri = post_author.uri | default: nil %} + {% assign post_author_name = post_author.name | default: post_author %} + "author": { + {% if post_author_name %} + "name": {{ post_author_name | json }}, + {% endif %} + {% if post_author_uri %} + "url": {{ post_author_uri | json }} + {% endif %} + } + {% endif %} + "tags": [ + {% for tag in post.tags %} + {{ tag | json }} + {% endfor %} + ] + } + {% endfor %} + ] +} \ No newline at end of file From 2605b5d31aafc1e0e7346d6d22abaee18bb111dd Mon Sep 17 00:00:00 2001 From: hugmanrique Date: Wed, 17 May 2017 18:19:51 -0400 Subject: [PATCH 02/13] Separate XML and JSON methods --- lib/jekyll-feed/generator.rb | 37 +++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/jekyll-feed/generator.rb b/lib/jekyll-feed/generator.rb index 0ff34ed7..8572d671 100644 --- a/lib/jekyll-feed/generator.rb +++ b/lib/jekyll-feed/generator.rb @@ -7,7 +7,7 @@ class Generator < Jekyll::Generator def generate(site) @site = site return if file_exists?(feed_path) - @site.pages << content_for_file(feed_path, feed_source_path) + @site.pages << xml_content_for_file(feed_path, feed_source_path) end private @@ -27,11 +27,25 @@ def feed_path end end + # Path to JSON feed from config, or feed.json for default + def json_feed_path + if @site.config["feed"] && @site.config["feed"]["path"] + @site.config["feed"]["path"] + else + "feed.json" + end + end + # Path to feed.xml template file def feed_source_path File.expand_path "./feed.xml", File.dirname(__FILE__) end + # Path to feed.json template file + def feed_json_source_path + File.expand_path "./feed.json", File.dirname(__FILE__) + end + # Checks if a file already exists in the site source def file_exists?(file_path) if @site.respond_to?(:in_source_dir) @@ -42,14 +56,31 @@ def file_exists?(file_path) end # Generates contents for a file - def content_for_file(file_path, file_source_path) + def content_for_file(file_path, file_source_path, regex) file = PageWithoutAFile.new(@site, File.dirname(__FILE__), "", file_path) - file.content = File.read(file_source_path).gsub(MINIFY_REGEX, "") + content = File.read(file_source_path) + + if regex + content = content.gsub(regex, "") + end + + file.content = content file.data["layout"] = nil file.data["sitemap"] = false + file + end + + def xml_content_for_file(file_path, file_source_path) + file = content_for_file(file_path, file_source_path, MINIFY_REGEX) file.data["xsl"] = file_exists?("feed.xslt.xml") file.output file end + + def json_content_for_file(file_path, file_source_path) + file = content_for_file(file_path, file_source_path, nil) + file.output + file + end end end From e95bd5045856e6a167d6be22ca40b944972be4b6 Mon Sep 17 00:00:00 2001 From: hugmanrique Date: Wed, 17 May 2017 18:22:21 -0400 Subject: [PATCH 03/13] Generate JSON feed.json file --- lib/jekyll-feed/generator.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/jekyll-feed/generator.rb b/lib/jekyll-feed/generator.rb index 8572d671..ab300bea 100644 --- a/lib/jekyll-feed/generator.rb +++ b/lib/jekyll-feed/generator.rb @@ -6,8 +6,10 @@ class Generator < Jekyll::Generator # Main plugin action, called by Jekyll-core def generate(site) @site = site - return if file_exists?(feed_path) + + return if file_exists?(feed_path) and file_exists?(json_feed_path) @site.pages << xml_content_for_file(feed_path, feed_source_path) + @site.pages << json_content_for_file(json_feed_path, feed_json_source_path) end private From 885174344b93ae7688e140a24731428a9d12d11d Mon Sep 17 00:00:00 2001 From: hugmanrique Date: Wed, 17 May 2017 18:24:48 -0400 Subject: [PATCH 04/13] Change config path --- lib/jekyll-feed/generator.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/jekyll-feed/generator.rb b/lib/jekyll-feed/generator.rb index ab300bea..a113ad09 100644 --- a/lib/jekyll-feed/generator.rb +++ b/lib/jekyll-feed/generator.rb @@ -6,7 +6,7 @@ class Generator < Jekyll::Generator # Main plugin action, called by Jekyll-core def generate(site) @site = site - + return if file_exists?(feed_path) and file_exists?(json_feed_path) @site.pages << xml_content_for_file(feed_path, feed_source_path) @site.pages << json_content_for_file(json_feed_path, feed_json_source_path) @@ -31,8 +31,8 @@ def feed_path # Path to JSON feed from config, or feed.json for default def json_feed_path - if @site.config["feed"] && @site.config["feed"]["path"] - @site.config["feed"]["path"] + if @site.config["feed"] && @site.config["feed"]["json_path"] + @site.config["feed"]["json_path"] else "feed.json" end From 03efd38e32a0d86d60bad43ef606885fffd70c11 Mon Sep 17 00:00:00 2001 From: hugmanrique Date: Wed, 17 May 2017 18:26:44 -0400 Subject: [PATCH 05/13] Add JSON details to README --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ed75c2b2..f385041b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Jekyll Feed plugin -A Jekyll plugin to generate an Atom (RSS-like) feed of your Jekyll posts +A Jekyll plugin to generate an Atom (RSS-like) feed and a [JSON feed](https://jsonfeed.org/version/1) of your Jekyll posts [![Build Status](https://travis-ci.org/jekyll/jekyll-feed.svg)](https://travis-ci.org/jekyll/jekyll-feed) [![Gem Version](https://badge.fury.io/rb/jekyll-feed.svg)](https://badge.fury.io/rb/jekyll-feed) @@ -39,6 +39,7 @@ Do you already have an existing feed someplace other than `/feed.xml`, but are o ```yml feed: path: atom.xml + json_path: json_feed.json ``` To note, you shouldn't have to do this unless you already have a feed you're using, and you can't or wish not to redirect existing subscribers. From ce265cb80e705029009939a44c89bc5f0296a3af Mon Sep 17 00:00:00 2001 From: hugmanrique Date: Wed, 17 May 2017 18:35:36 -0400 Subject: [PATCH 06/13] Forgot Liquid closing tag --- lib/jekyll-feed/feed.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll-feed/feed.json b/lib/jekyll-feed/feed.json index 0ccf0d82..8c264bd3 100644 --- a/lib/jekyll-feed/feed.json +++ b/lib/jekyll-feed/feed.json @@ -1,6 +1,6 @@ { "version": "https://jsonfeed.org/version/1", - {% assign site_title = site.title | site.name } + {% assign site_title = site.title | site.name %} {% if site_title %} "title": {{ site_title | smartify | json }}, {% endif %} From 8f07155f44a0ea744599e0c4abc454e92d30fc39 Mon Sep 17 00:00:00 2001 From: hugmanrique Date: Wed, 17 May 2017 18:39:13 -0400 Subject: [PATCH 07/13] Add JSON array commas unless last loop --- lib/jekyll-feed/feed.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/jekyll-feed/feed.json b/lib/jekyll-feed/feed.json index 8c264bd3..2005af35 100644 --- a/lib/jekyll-feed/feed.json +++ b/lib/jekyll-feed/feed.json @@ -6,7 +6,7 @@ {% endif %} "home_page_url": "{{ '/' | absolute_url }}", "feed_url": "{{ page.url | absolute_url }}", - {% if site.description } + {% if site.description %} "description": {{ site.description | json }}, {% endif %} {% if site.author %} @@ -55,9 +55,11 @@ "tags": [ {% for tag in post.tags %} {{ tag | json }} + {% unless forloop.last %},{% endunless %} {% endfor %} ] } + {% unless forloop.last %},{% endunless %} {% endfor %} ] } \ No newline at end of file From 51d3422aa0bb5b373035dea09b50a0e0164074a6 Mon Sep 17 00:00:00 2001 From: hugmanrique Date: Wed, 17 May 2017 18:41:17 -0400 Subject: [PATCH 08/13] Replace and by && --- lib/jekyll-feed/generator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll-feed/generator.rb b/lib/jekyll-feed/generator.rb index a113ad09..e46f4ba7 100644 --- a/lib/jekyll-feed/generator.rb +++ b/lib/jekyll-feed/generator.rb @@ -7,7 +7,7 @@ class Generator < Jekyll::Generator def generate(site) @site = site - return if file_exists?(feed_path) and file_exists?(json_feed_path) + return if file_exists?(feed_path) && file_exists?(json_feed_path) @site.pages << xml_content_for_file(feed_path, feed_source_path) @site.pages << json_content_for_file(json_feed_path, feed_json_source_path) end From ac4263896d5fe2e223027e575359ab1dabe9a810 Mon Sep 17 00:00:00 2001 From: hugmanrique Date: Wed, 17 May 2017 18:45:57 -0400 Subject: [PATCH 09/13] Add more docs info --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f385041b..9c3ef6b8 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ gems: ## Usage -The plugin will automatically generate an Atom feed at `/feed.xml`. +The plugin will automatically generate an Atom feed at `/feed.xml` and a [JSON feed](https://jsonfeed.org/) at `/feed.json`. ### Optional configuration options From 107019205e395d11b40ceee7dcbe13090a791327 Mon Sep 17 00:00:00 2001 From: hugmanrique Date: Thu, 18 May 2017 19:57:20 -0400 Subject: [PATCH 10/13] Fix JSON formatting issues --- lib/jekyll-feed/feed.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/jekyll-feed/feed.json b/lib/jekyll-feed/feed.json index 2005af35..63ecb3e7 100644 --- a/lib/jekyll-feed/feed.json +++ b/lib/jekyll-feed/feed.json @@ -13,9 +13,9 @@ "author": { "name": {{ site.author.name | default: site.author | json }}, {% if site.author.uri %} - "url": {{ site.author.uri | json }}, + "url": {{ site.author.uri | json }} {% endif %} - } + }, {% endif %} "items": [ {% assign posts = site.posts | where_exp: "post", "post.draft != true" %} @@ -25,9 +25,9 @@ "url": "{{ post.url | absolute_url }}", "title": {{ post.title | smartify | strip_html | normalize_whitespace | json }}, {% if post.excerpt and post.excerpt != empty %} - {% assign summary = post.excerpt | strip_html | normalize_whitespace | json %} - "content_html": {{ summary }}, - "summary": {{ summary }}, + {% assign post_summary = post.excerpt | strip_html | normalize_whitespace | json %} + "content_html": {{ post_summary }}, + "summary": {{ post_summary }}, {% endif %} {% assign post_image = post.image.path | default: post.image %} {% if post_image %} @@ -50,7 +50,7 @@ {% if post_author_uri %} "url": {{ post_author_uri | json }} {% endif %} - } + }, {% endif %} "tags": [ {% for tag in post.tags %} From 6ccf5040e726ab150d4d36a4bc02cc293a45ab6f Mon Sep 17 00:00:00 2001 From: hugmanrique Date: Wed, 24 May 2017 16:13:30 -0400 Subject: [PATCH 11/13] Fix limit typo --- lib/jekyll-feed/feed.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll-feed/feed.json b/lib/jekyll-feed/feed.json index 63ecb3e7..6035a6c3 100644 --- a/lib/jekyll-feed/feed.json +++ b/lib/jekyll-feed/feed.json @@ -19,7 +19,7 @@ {% endif %} "items": [ {% assign posts = site.posts | where_exp: "post", "post.draft != true" %} - {% for post in posts limmit: 10 %} + {% for post in posts limit: 10 %} { "id": {{ post.id | absolute_url | json }}, "url": "{{ post.url | absolute_url }}", From eaf474c9494ca1f0dc23cbdc9a09374d661a903a Mon Sep 17 00:00:00 2001 From: hugmanrique Date: Wed, 24 May 2017 16:20:35 -0400 Subject: [PATCH 12/13] Adapt JSON to @vallieres version --- lib/jekyll-feed/feed.json | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/jekyll-feed/feed.json b/lib/jekyll-feed/feed.json index 6035a6c3..3f4fa7d7 100644 --- a/lib/jekyll-feed/feed.json +++ b/lib/jekyll-feed/feed.json @@ -4,11 +4,14 @@ {% if site_title %} "title": {{ site_title | smartify | json }}, {% endif %} - "home_page_url": "{{ '/' | absolute_url }}", - "feed_url": "{{ page.url | absolute_url }}", {% if site.description %} "description": {{ site.description | json }}, {% endif %} + "home_page_url": "{{ '/' | absolute_url }}", + "feed_url": "{{ page.url | absolute_url }}", + "icon": "{{ "/apple-touch-icon.png" | absolute_url }}", + "favicon": "{{ "/favicon.ico" | absolute_url }}", + "expired": false, {% if site.author %} "author": { "name": {{ site.author.name | default: site.author | json }}, @@ -52,6 +55,15 @@ {% endif %} }, {% endif %} + {% if post.enclosure %} + "attachments": [ + { + "url": "{{ post.enclosure }}", + "mime_type": "{{ post.enclosure_type }}", + "size_in_bytes": "{{ post.enclosure_length }}" + } + ], + {% endif %} "tags": [ {% for tag in post.tags %} {{ tag | json }} From c16385e03e3dd5d20102a5507d65e060b9add16b Mon Sep 17 00:00:00 2001 From: Hugo Manrique Date: Sat, 17 Jun 2017 13:32:27 -0400 Subject: [PATCH 13/13] Simplify tags loop @bogdanvlviv suggested this --- lib/jekyll-feed/feed.json | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/jekyll-feed/feed.json b/lib/jekyll-feed/feed.json index 3f4fa7d7..c8a51a0c 100644 --- a/lib/jekyll-feed/feed.json +++ b/lib/jekyll-feed/feed.json @@ -64,14 +64,9 @@ } ], {% endif %} - "tags": [ - {% for tag in post.tags %} - {{ tag | json }} - {% unless forloop.last %},{% endunless %} - {% endfor %} - ] + "tags": {{ post.tags | jsonify }} } {% unless forloop.last %},{% endunless %} {% endfor %} ] -} \ No newline at end of file +}