Skip to content

Commit f3941ca

Browse files
committed
Make feed_meta helper collection- and category-aware
1 parent bb44a6e commit f3941ca

File tree

3 files changed

+108
-33
lines changed

3 files changed

+108
-33
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ There are several ways to convey author-specific information. Author information
120120

121121
The plugin exposes a helper tag to expose the appropriate meta tags to support automated discovery of your feed. Simply place `{% feed_meta %}` someplace in your template's `<head>` section, to output the necessary metadata.
122122

123+
The helper can also generate the link for a given collection: `{% feed_meta my_collection %}` or a given category: `{% feed_meta my_collection my_category %}`.
124+
125+
To generate links for every collections and categories, call the helper using this syntax: `{% feed_meta include: all %}`.
126+
123127
### SmartyPants
124128

125129
The plugin uses [Jekyll's `smartify` filter](https://jekyllrb.com/docs/templates/) for processing the site title and post titles. This will translate plain ASCII punctuation into "smart" typographic punctuation. This will not render or strip any Markdown you may be using in a title.

lib/jekyll-feed/meta-tag.rb

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,26 @@ class MetaTag < Liquid::Tag
55
# Use Jekyll's native relative_url filter
66
include Jekyll::Filters::URLFilters
77

8+
def initialize(tag_name, args, tokens)
9+
super
10+
@args = args
11+
end
12+
813
def render(context)
914
@context = context
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} />"
15+
if @args.strip == "include: all"
16+
links = []
17+
generator.collections.each do |collection, meta|
18+
(meta["categories"] + [nil]).each do |category|
19+
links << link(collection, category)
20+
end
1621
end
22+
links.reverse.join "\n"
23+
else
24+
@collection, @category = @args.split(" ")
25+
@collection ||= "posts"
26+
link(@collection, @category) if valid_collection && valid_category
1727
end
18-
links.reverse.join "\n"
1928
end
2029

2130
private
@@ -28,6 +37,11 @@ def generator
2837
@generator ||= @context.registers[:site].generators.select { |it| it.is_a? JekyllFeed::Generator }.first # rubocop:disable Metrics/LineLength
2938
end
3039

40+
def link(collection, category)
41+
attrs = attributes(collection, category).map { |k, v| %(#{k}="#{v}") }.join(" ")
42+
"<link #{attrs} />"
43+
end
44+
3145
def attributes(collection, category)
3246
href = absolute_url(generator.feed_path(:collection => collection, :category => category))
3347
{
@@ -41,5 +55,28 @@ def attributes(collection, category)
4155
def title
4256
config["title"] || config["name"]
4357
end
58+
59+
def valid_collection
60+
return true if generator.collections.key? @collection
61+
62+
Jekyll.logger.warn(
63+
"Jekyll Feed:",
64+
"Invalid collection name. Please review `{% feed_meta #{@args} %}`"
65+
)
66+
false
67+
end
68+
69+
def valid_category
70+
return true if @collection == "posts" || @category.nil?
71+
72+
collection = generator.collections[@collection]
73+
return true if collection.key?("categories") && collection["categories"].include?(@category)
74+
75+
Jekyll.logger.warn(
76+
"Jekyll Feed:",
77+
"Invalid category name. Please review `{% feed_meta #{@args} %}`"
78+
)
79+
false
80+
end
4481
end
4582
end

spec/jekyll-feed_spec.rb

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -269,32 +269,6 @@
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
298272
end
299273

300274
context "changing the feed path" do
@@ -339,6 +313,66 @@
339313
end
340314
end
341315

316+
context "selecting a particular collection" do
317+
let(:overrides) do
318+
{
319+
"collections" => {
320+
"collection" => {
321+
"output" => true,
322+
},
323+
},
324+
"feed" => {
325+
"collections" => {
326+
"collection" => {
327+
"categories" => ["news"],
328+
},
329+
},
330+
},
331+
}
332+
end
333+
let(:default_feed) { Liquid::Template.parse("{% feed_meta posts %}").render!(context, {}) }
334+
let(:collection_feed) { Liquid::Template.parse("{% feed_meta collection %}").render!(context, {}) }
335+
let(:category_feed) { Liquid::Template.parse("{% feed_meta collection news %}").render!(context, {}) }
336+
337+
it "renders the feed meta for the selected collection" do
338+
default_feed_link = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed.xml" title="My awesome site" />'
339+
collection_feed_link = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed/collection.xml" title="My awesome site" />'
340+
category_feed_link = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed/collection/news.xml" title="My awesome site" />'
341+
expect(default_feed).to eql(default_feed_link)
342+
expect(collection_feed).to eql(collection_feed_link)
343+
expect(category_feed).to eql(category_feed_link)
344+
end
345+
end
346+
347+
context "requesting all feed links" do
348+
let(:overrides) do
349+
{
350+
"collections" => {
351+
"collection" => {
352+
"output" => true,
353+
},
354+
},
355+
"feed" => {
356+
"collections" => {
357+
"collection" => {
358+
"categories" => ["news"],
359+
},
360+
},
361+
},
362+
}
363+
end
364+
let(:full_feed_meta) { Liquid::Template.parse("{% feed_meta include: all %}").render!(context, {}) }
365+
366+
it "renders the feed meta for all collections and categories" do
367+
default_feed_link = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed.xml" title="My awesome site" />'
368+
collection_feed_link = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed/collection.xml" title="My awesome site" />'
369+
category_feed_link = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed/collection/news.xml" title="My awesome site" />'
370+
expect(full_feed_meta).to include(default_feed_link)
371+
expect(full_feed_meta).to include(collection_feed_link)
372+
expect(full_feed_meta).to include(category_feed_link)
373+
end
374+
end
375+
342376
context "feed stylesheet" do
343377
it "includes the stylesheet" do
344378
expect(contents).to include('<?xml-stylesheet type="text/xml" href="http://example.org/feed.xslt.xml"?>')

0 commit comments

Comments
 (0)