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

Improve collection and category management #255

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 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 Expand Up @@ -170,6 +174,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
54 changes: 37 additions & 17 deletions lib/jekyll-feed/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,14 @@ 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

private

# Matches all whitespace that follows
# 1. A '>', which closes an XML tag or
# 2. A '}', which closes a Liquid tag
# We will strip all of this whitespace to minify the template
MINIFY_REGEX = %r!(?<=>|})\s+!.freeze

# Returns the plugin's config or an empty hash if not set
def config
@config ||= @site.config["feed"] || {}
end

# Determines the destination path of a given feed
#
# collection - the name of a collection, e.g., "posts"
Expand All @@ -45,7 +33,25 @@ def feed_path(collection: "posts", category: nil)
prefix = collection == "posts" ? "/feed" : "/feed/#{collection}"
return "#{prefix}/#{category}.xml" if category

collections.dig(collection, "path") || "#{prefix}.xml"
@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"])
unless collection == "posts"
words << (collections.dig(collection, "title") || collection.capitalize)
end
words << category.capitalize if category
words.compact.join " | "
end

# Returns a hash representing all collections to be processed and their metadata
Expand All @@ -69,6 +75,19 @@ def collections
@collections
end

private

# Matches all whitespace that follows
# 1. A '>', which closes an XML tag or
# 2. A '}', which closes a Liquid tag
# We will strip all of this whitespace to minify the template
MINIFY_REGEX = %r!(?<=>|})\s+!.freeze

# Returns the plugin's config or an empty hash if not set
def config
@config ||= @site.config["feed"] || {}
end

# Path to feed.xml template file
def feed_source_path
@feed_source_path ||= File.expand_path "feed.xml", __dir__
Expand All @@ -85,15 +104,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
60 changes: 51 additions & 9 deletions lib/jekyll-feed/meta-tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +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
attrs = attributes.map { |k, v| %(#{k}="#{v}") }.join(" ")
"<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
end

private
Expand All @@ -17,21 +33,47 @@ def config
@config ||= @context.registers[:site].config
end

def attributes
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))
title = generator.feed_title(:collection => collection, :category => category)
{
:type => "application/atom+xml",
:rel => "alternate",
:href => absolute_url(path),
:href => href,
:title => title,
}.keep_if { |_, v| v }
}.delete_if { |_, v| v.strip.empty? }
end

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

invalidate_with_warning("collection")
end

def path
config.dig("feed", "path") || "feed.xml"
def valid_category
return true if @collection == "posts" || @category.nil?

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

invalidate_with_warning("category")
end

def title
config["title"] || config["name"]
def invalidate_with_warning(type)
Jekyll.logger.warn(
"Jekyll Feed:",
"Invalid #{type} name. Please review `{% feed_meta #{@args} %}`"
)
false
end
end
end
85 changes: 85 additions & 0 deletions spec/jekyll-feed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,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 | Collection" />'
category_feed_link = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed/collection/news.xml" title="My awesome site | Collection | News" />'
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 | Collection" />'
category_feed_link = '<link type="application/atom+xml" rel="alternate" href="http://example.org/feed/collection/news.xml" title="My awesome site | Collection | News" />'
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 Expand Up @@ -429,6 +489,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