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 configuration of collection feed's title and backing collection #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* add option to set a collection feed's title and backing collection

...

## 3.1.2 / 2024-03-02
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,27 @@ feed:
path: "/changes.xml"
```

By default, collection feeds will have a title based on the collection name. If you'd like to customize the feed title, specify a collection's title as follows:

```yml
feed:
collections:
changes:
title: Recent Changes
```

By default, collection feeds will use the same collection as the configuration key. If you'd like to customize the collection, or have multiple configurations for a single collection, specify as follows:

```yml
feed:
collections:
my_feed:
collection: posts
daily_email: # single article feed for daily email automation
collection: posts
limit: 1
```

Finally, collections can also have category feeds which are outputted as `/feed/<COLLECTION>/<CATEGORY>.xml`. Specify categories like so:

```yml
Expand Down
9 changes: 4 additions & 5 deletions lib/bridgetown-feed/feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
<id>{{ page.url | absolute_url | xml_escape }}</id>

{% assign title = site.metadata.title | default: site.metadata.name %}
{% if page.collection != "posts" %}
{% assign collection = page.collection | capitalize %}
{% assign title = title | append: " | " | append: collection %}
{% if page.feed_meta.title and page.feed_meta.title != "Posts" %}
{% assign title = title | append: " | " | append: page.feed_meta.title %}
{% endif %}
{% if page.category %}
{% assign category = page.category | capitalize %}
Expand All @@ -39,12 +38,12 @@
</author>
{% endif %}

{% assign feed_collection = collections[page.collection] %}
{% assign feed_collection = collections[page.feed_meta.collection] %}
{% find posts where feed_collection.resources, draft != true %}
{% if page.category %}
{% assign posts = posts | where: "category",page.category %}
{% endif %}
{% assign post_limit = site.feed.collections[page.collection].post_limit | default: site.feed.post_limit | default: 10 %}
{% assign post_limit = page.feed_meta.post_limit | default: site.feed.post_limit | default: 10 %}
{% for post in posts limit: post_limit %}
{% assign post_id = post.data.id | default: post.id %}
<entry{% if post.lang %}{{" "}}xml:lang="{{ post.lang }}"{% endif %}>
Expand Down
12 changes: 7 additions & 5 deletions lib/bridgetown-feed/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def generate(site)
path = feed_path(collection: name, category: category)
next if file_exists?(path)

@site.generated_pages << make_page(path, collection: name, category: category)
@site.generated_pages << make_page(path, feed_meta: meta, category: category)
end
end
end
Expand Down Expand Up @@ -61,8 +61,10 @@ def collections
end

@collections = normalize_posts_meta(@collections)
@collections.each_value do |meta|
@collections.each_pair do |key, meta|
meta["categories"] = (meta["categories"] || []).to_set
meta["title"] ||= key.capitalize
meta["collection"] ||= key
end

@collections
Expand All @@ -84,7 +86,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, feed_meta:, category: nil)
Bridgetown::GeneratedPage.new(@site, __dir__, "", file_path, from_plugin: true).tap do |file|
file.content = feed_template
file.data.merge!(
Expand All @@ -93,8 +95,8 @@ def make_page(file_path, collection: "posts", category: nil)
"template_engine" => "liquid",
"sitemap" => false,
"xsl" => file_exists?("feed.xslt.xml"),
"collection" => collection,
"category" => category
"category" => category,
"feed_meta" => feed_meta
)
file.output
end
Expand Down
74 changes: 74 additions & 0 deletions spec/bridgetown-feed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -583,4 +583,78 @@
end
end
end

describe "feed title override" do
let(:feed) { RSS::Parser.parse(contents) }

context "with posts collection" do
let(:overrides) do
{
"feed" => {
"collections" => {
"posts" => {}
}
},
}
end

it "doesn't include a posts title" do
expect(feed.title.content).not_to include "Posts"
end
end

context "with feed title override" do
let(:overrides) do
{
"feed" => {
"collections" => {
"posts" => {
"title": "My Feed"
},
},
},
}
end

it "includes the custom title" do
expect(feed.title.content).to include "My Feed"
end
end
end

describe "feed collection override" do
context "without specifing the collection" do
let(:overrides) do
{
"feed" => {
"collections" => {
"posts" => {}
}
},
}
end

it "uses the key name as the collection" do
expect(config.feed.collections.posts.collection).to eq "posts"
end
end

context "without specifing the collection" do
let(:overrides) do
{
"feed" => {
"collections" => {
"my_posts" => {
"collection" => "posts"
}
}
},
}
end

it "uses the key name as the collection" do
expect(config.feed.collections.my_posts.collection).to eq "posts"
end
end
end
end