From b723f2973f67b9e725e3d9fd84eee4184dccffe1 Mon Sep 17 00:00:00 2001 From: Goulven Champenois Date: Wed, 14 Nov 2018 14:00:09 +0100 Subject: [PATCH 1/3] Allow customising collection feed titles --- README.md | 9 +++++++++ lib/jekyll-feed/feed.xml | 14 +------------- lib/jekyll-feed/generator.rb | 24 +++++++++++++++++++++--- spec/jekyll-feed_spec.rb | 25 +++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 84d68634..296c29eb 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,15 @@ feed: path: "/changes.xml" ``` +Collection feed titles will include the capitalized collection name. If you'd like to customize the feed title, specify a collection's custom title as follows: + +```yml +feed: + collections: + changes: + title: "My collection title" +``` + Finally, collections can also have category feeds which are outputted as `/feed//.xml`. Specify categories like so: ```yml diff --git a/lib/jekyll-feed/feed.xml b/lib/jekyll-feed/feed.xml index cf9f9ed4..cc4e9cdb 100644 --- a/lib/jekyll-feed/feed.xml +++ b/lib/jekyll-feed/feed.xml @@ -9,19 +9,7 @@ {{ site.time | date_to_xmlschema }} {{ page.url | absolute_url | xml_escape }} - {% assign title = site.title | default: site.name %} - {% if page.collection != "posts" %} - {% assign collection = page.collection | capitalize %} - {% assign title = title | append: " | " | append: collection %} - {% endif %} - {% if page.category %} - {% assign category = page.category | capitalize %} - {% assign title = title | append: " | " | append: category %} - {% endif %} - - {% if title %} - {{ title | smartify | xml_escape }} - {% endif %} + {{ page.feed_title | smartify | xml_escape }} {% if site.description %} {{ site.description | xml_escape }} diff --git a/lib/jekyll-feed/generator.rb b/lib/jekyll-feed/generator.rb index 13684056..bd1b3401 100644 --- a/lib/jekyll-feed/generator.rb +++ b/lib/jekyll-feed/generator.rb @@ -12,9 +12,10 @@ def generate(site) Jekyll.logger.info "Jekyll Feed:", "Generating feed for #{name}" (meta["categories"] + [nil]).each do |category| path = feed_path(:collection => name, :category => category) + feed_title = feed_title(:collection => name, :category => category) next if file_exists?(path) - @site.pages << make_page(path, :collection => name, :category => category) + @site.pages << make_page(path, feed_title, :collection => name, :category => category) end end end @@ -48,6 +49,22 @@ def feed_path(collection: "posts", category: nil) collections.dig(collection, "path") || "#{prefix}.xml" end + # Determines the title of a given feed + # + # collection - the name of a collection, e.g., "posts" + # category - a category within that collection, e.g., "news" + # + # Will return the site title or name + # ...followed by collection title or capitalized name + # ...followed by capitalized category name + def feed_title(collection: "posts", category: nil) + words = [] + words << (@site.config["title"] || @site.config["name"]) + words << collections.dig(collection, "title") || collection.capitalize unless collection == "posts" # rubocop:disable Metrics/LineLength + words << category.capitalize if category + words.uniq.join " | " + end + # Returns a hash representing all collections to be processed and their metadata # in the form of { collection_name => { categories = [...], path = "..." } } def collections @@ -85,7 +102,7 @@ def file_exists?(file_path) # Generates contents for a file - def make_page(file_path, collection: "posts", category: nil) + def make_page(file_path, title, collection: "posts", category: nil) PageWithoutAFile.new(@site, __dir__, "", file_path).tap do |file| file.content = feed_template file.data.merge!( @@ -93,7 +110,8 @@ def make_page(file_path, collection: "posts", category: nil) "sitemap" => false, "xsl" => file_exists?("feed.xslt.xml"), "collection" => collection, - "category" => category + "category" => category, + "feed_title" => title ) file.output end diff --git a/spec/jekyll-feed_spec.rb b/spec/jekyll-feed_spec.rb index 1a8cc5c7..4e3bfd40 100644 --- a/spec/jekyll-feed_spec.rb +++ b/spec/jekyll-feed_spec.rb @@ -416,6 +416,31 @@ end end + context "with collection title" do + let(:collection_with_title_feed) { File.read(dest_dir("feed/collection_with_title.xml")) } + let(:overrides) do + { + "collections" => { + "collection_with_title" => { + "output" => true, + "path" => 'collection_with_title' + }, + }, + "feed" => { + "collections" => { + "collection_with_title" => { + "title" => "My collection title", + }, + }, + }, + } + end + + it "outputs the collection feed with custom title" do + expect(collection_with_title_feed).to match 'My Awesome Site | My collection title' + end + end + context "with categories" do let(:overrides) do { From 32fdd9ddcd7c99f21b8ba4a25de8f72c0811bd86 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Thu, 15 Nov 2018 20:57:25 +0100 Subject: [PATCH 2/3] Improve code readability by reducing line length Thanks @ashmaroli! Co-Authored-By: goulvench --- 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 bd1b3401..dd0aebc3 100644 --- a/lib/jekyll-feed/generator.rb +++ b/lib/jekyll-feed/generator.rb @@ -60,7 +60,9 @@ def feed_path(collection: "posts", category: nil) def feed_title(collection: "posts", category: nil) words = [] words << (@site.config["title"] || @site.config["name"]) - words << collections.dig(collection, "title") || collection.capitalize unless collection == "posts" # rubocop:disable Metrics/LineLength + unless collection == "posts" + words << (collections.dig(collection, "title") || collection.capitalize) + end words << category.capitalize if category words.uniq.join " | " end From 216f874c1a27fc0d4197fe8156acc5b7f87b529b Mon Sep 17 00:00:00 2001 From: Goulven Champenois Date: Thu, 15 Nov 2018 22:53:07 +0100 Subject: [PATCH 3/3] Remove empty instead of duplicate words when generating feed_titles --- 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 dd0aebc3..865a3aa7 100644 --- a/lib/jekyll-feed/generator.rb +++ b/lib/jekyll-feed/generator.rb @@ -64,7 +64,7 @@ def feed_title(collection: "posts", category: nil) words << (collections.dig(collection, "title") || collection.capitalize) end words << category.capitalize if category - words.uniq.join " | " + words.compact.join " | " end # Returns a hash representing all collections to be processed and their metadata