Skip to content

Commit bb44a6e

Browse files
committed
Output feed links for collections
1 parent e97bc64 commit bb44a6e

File tree

3 files changed

+56
-22
lines changed

3 files changed

+56
-22
lines changed

lib/jekyll-feed/generator.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,6 @@ def generate(site)
1919
end
2020
end
2121

22-
private
23-
24-
# Matches all whitespace that follows
25-
# 1. A '>', which closes an XML tag or
26-
# 2. A '}', which closes a Liquid tag
27-
# We will strip all of this whitespace to minify the template
28-
MINIFY_REGEX = %r!(?<=>|})\s+!.freeze
29-
30-
# Returns the plugin's config or an empty hash if not set
31-
def config
32-
@config ||= @site.config["feed"] || {}
33-
end
34-
3522
# Determines the destination path of a given feed
3623
#
3724
# collection - the name of a collection, e.g., "posts"
@@ -45,7 +32,7 @@ def feed_path(collection: "posts", category: nil)
4532
prefix = collection == "posts" ? "/feed" : "/feed/#{collection}"
4633
return "#{prefix}/#{category}.xml" if category
4734

48-
collections.dig(collection, "path") || "#{prefix}.xml"
35+
@collections.dig(collection, "path") || "#{prefix}.xml"
4936
end
5037

5138
# Returns a hash representing all collections to be processed and their metadata
@@ -69,6 +56,19 @@ def collections
6956
@collections
7057
end
7158

59+
private
60+
61+
# Matches all whitespace that follows
62+
# 1. A '>', which closes an XML tag or
63+
# 2. A '}', which closes a Liquid tag
64+
# We will strip all of this whitespace to minify the template
65+
MINIFY_REGEX = %r!(?<=>|})\s+!.freeze
66+
67+
# Returns the plugin's config or an empty hash if not set
68+
def config
69+
@config ||= @site.config["feed"] || {}
70+
end
71+
7272
# Path to feed.xml template file
7373
def feed_source_path
7474
@feed_source_path ||= File.expand_path "feed.xml", __dir__

lib/jekyll-feed/meta-tag.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@ class MetaTag < Liquid::Tag
77

88
def render(context)
99
@context = context
10-
attrs = attributes.map { |k, v| %(#{k}="#{v}") }.join(" ")
11-
"<link #{attrs} />"
10+
@generator = generator
11+
links = []
12+
generator.collections.each do |collection, meta|
13+
(meta["categories"] + [nil]).each do |category|
14+
attrs = attributes(collection, category).map { |k, v| %(#{k}="#{v}") }.join(" ")
15+
links << "<link #{attrs} />"
16+
end
17+
end
18+
links.reverse.join "\n"
1219
end
1320

1421
private
@@ -17,19 +24,20 @@ def config
1724
@config ||= @context.registers[:site].config
1825
end
1926

20-
def attributes
27+
def generator
28+
@generator ||= @context.registers[:site].generators.select { |it| it.is_a? JekyllFeed::Generator }.first # rubocop:disable Metrics/LineLength
29+
end
30+
31+
def attributes(collection, category)
32+
href = absolute_url(generator.feed_path(:collection => collection, :category => category))
2133
{
2234
:type => "application/atom+xml",
2335
:rel => "alternate",
24-
:href => absolute_url(path),
36+
:href => href,
2537
:title => title,
2638
}.keep_if { |_, v| v }
2739
end
2840

29-
def path
30-
config.dig("feed", "path") || "feed.xml"
31-
end
32-
3341
def title
3442
config["title"] || config["name"]
3543
end

spec/jekyll-feed_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,32 @@
269269
expect(feed_meta).not_to include("title=")
270270
end
271271
end
272+
context "with a collection" do
273+
let(:overrides) do
274+
{
275+
"collections" => {
276+
"collection" => {
277+
"output" => true,
278+
},
279+
},
280+
"feed" => {
281+
"collections" => {
282+
"collection" => {
283+
"categories" => ["news"],
284+
},
285+
},
286+
},
287+
}
288+
end
289+
it "renders a feed meta for each collection" do
290+
default_feed = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed.xml" title="My awesome site" />'
291+
collection_feed = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed/collection.xml" title="My awesome site" />'
292+
category_feed = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed/collection/news.xml" title="My awesome site" />'
293+
expect(feed_meta).to include(default_feed)
294+
expect(feed_meta).to include(collection_feed)
295+
expect(feed_meta).to include(category_feed)
296+
end
297+
end
272298
end
273299

274300
context "changing the feed path" do

0 commit comments

Comments
 (0)