Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customising collection feed titles #253

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<COLLECTION>/<CATEGORY>.xml`. Specify categories like so:

```yml
Expand Down
14 changes: 1 addition & 13 deletions lib/jekyll-feed/feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,7 @@
<updated>{{ site.time | date_to_xmlschema }}</updated>
<id>{{ page.url | absolute_url | xml_escape }}</id>

{% 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 type="html">{{ title | smartify | xml_escape }}</title>
{% endif %}
<title type="html">{{ page.feed_title | smartify | xml_escape }}</title>

{% if site.description %}
<subtitle>{{ site.description | xml_escape }}</subtitle>
Expand Down
24 changes: 21 additions & 3 deletions lib/jekyll-feed/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
goulvench marked this conversation as resolved.
Show resolved Hide resolved
words << collections.dig(collection, "title") || collection.capitalize unless collection == "posts" # rubocop:disable Metrics/LineLength
goulvench marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down Expand Up @@ -85,15 +102,16 @@ 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!(
"layout" => nil,
"sitemap" => false,
"xsl" => file_exists?("feed.xslt.xml"),
"collection" => collection,
"category" => category
"category" => category,
"feed_title" => title
)
file.output
end
Expand Down
25 changes: 25 additions & 0 deletions spec/jekyll-feed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<title type="html">My Awesome Site | My collection title</title>'
end
end

context "with categories" do
let(:overrides) do
{
Expand Down