Skip to content

Commit

Permalink
Make feed_meta helper collection- and category-aware
Browse files Browse the repository at this point in the history
  • Loading branch information
goulvench committed Jan 21, 2019
1 parent 77288c6 commit 4d58045
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 33 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ There are several ways to convey author-specific information. Author information

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.

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 %}`.

To generate links for every collections and categories, call the helper using this syntax: `{% feed_meta include: all %}`.

### SmartyPants

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.
Expand Down
51 changes: 44 additions & 7 deletions lib/jekyll-feed/meta-tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@ class MetaTag < Liquid::Tag
# Use Jekyll's native relative_url filter
include Jekyll::Filters::URLFilters

def initialize(tag_name, args, tokens)
super
@args = args
end

def render(context)
@context = context
@generator = generator
links = []
generator.collections.each do |collection, meta|
(meta["categories"] + [nil]).each do |category|
attrs = attributes(collection, category).map { |k, v| %(#{k}="#{v}") }.join(" ")
links << "<link #{attrs} />"
if @args.strip == "include: all"
links = []
generator.collections.each do |collection, meta|
(meta["categories"] + [nil]).each do |category|
links << link(collection, category)
end
end
links.reverse.join "\n"
else
@collection, @category = @args.split(" ")
@collection ||= "posts"
link(@collection, @category) if valid_collection && valid_category
end
links.reverse.join "\n"
end

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

def link(collection, category)
attrs = attributes(collection, category).map { |k, v| %(#{k}="#{v}") }.join(" ")
"<link #{attrs} />"
end

def attributes(collection, category)
href = absolute_url(generator.feed_path(:collection => collection, :category => category))
{
Expand All @@ -41,5 +55,28 @@ def attributes(collection, category)
def title
config["title"] || config["name"]
end

def valid_collection
return true if generator.collections.key? @collection

Jekyll.logger.warn(
"Jekyll Feed:",
"Invalid collection name. Please review `{% feed_meta #{@args} %}`"
)
false
end

def valid_category
return true if @collection == "posts" || @category.nil?

collection = generator.collections[@collection]
return true if collection.key?("categories") && collection["categories"].include?(@category)

Jekyll.logger.warn(
"Jekyll Feed:",
"Invalid category name. Please review `{% feed_meta #{@args} %}`"
)
false
end
end
end
86 changes: 60 additions & 26 deletions spec/jekyll-feed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,32 +269,6 @@
expect(feed_meta).not_to include("title=")
end
end
context "with a collection" do
let(:overrides) do
{
"collections" => {
"collection" => {
"output" => true,
},
},
"feed" => {
"collections" => {
"collection" => {
"categories" => ["news"],
},
},
},
}
end
it "renders a feed meta for each collection" do
default_feed = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed.xml" title="My awesome site" />'
collection_feed = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed/collection.xml" title="My awesome site" />'
category_feed = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed/collection/news.xml" title="My awesome site" />'
expect(feed_meta).to include(default_feed)
expect(feed_meta).to include(collection_feed)
expect(feed_meta).to include(category_feed)
end
end
end

context "changing the feed path" do
Expand Down Expand Up @@ -339,6 +313,66 @@
end
end

context "selecting a particular collection" do
let(:overrides) do
{
"collections" => {
"collection" => {
"output" => true,
},
},
"feed" => {
"collections" => {
"collection" => {
"categories" => ["news"],
},
},
},
}
end
let(:default_feed) { Liquid::Template.parse("{% feed_meta posts %}").render!(context, {}) }
let(:collection_feed) { Liquid::Template.parse("{% feed_meta collection %}").render!(context, {}) }
let(:category_feed) { Liquid::Template.parse("{% feed_meta collection news %}").render!(context, {}) }

it "renders the feed meta for the selected collection" do
default_feed_link = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed.xml" title="My awesome site" />'
collection_feed_link = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed/collection.xml" title="My awesome site" />'
category_feed_link = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed/collection/news.xml" title="My awesome site" />'
expect(default_feed).to eql(default_feed_link)
expect(collection_feed).to eql(collection_feed_link)
expect(category_feed).to eql(category_feed_link)
end
end

context "requesting all feed links" do
let(:overrides) do
{
"collections" => {
"collection" => {
"output" => true,
},
},
"feed" => {
"collections" => {
"collection" => {
"categories" => ["news"],
},
},
},
}
end
let(:full_feed_meta) { Liquid::Template.parse("{% feed_meta include: all %}").render!(context, {}) }

it "renders the feed meta for all collections and categories" do
default_feed_link = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed.xml" title="My awesome site" />'
collection_feed_link = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed/collection.xml" title="My awesome site" />'
category_feed_link = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed/collection/news.xml" title="My awesome site" />'
expect(full_feed_meta).to include(default_feed_link)
expect(full_feed_meta).to include(collection_feed_link)
expect(full_feed_meta).to include(category_feed_link)
end
end

context "feed stylesheet" do
it "includes the stylesheet" do
expect(contents).to include('<?xml-stylesheet type="text/xml" href="http://example.org/feed.xslt.xml"?>')
Expand Down

0 comments on commit 4d58045

Please sign in to comment.