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

Add site.feed.updated = latest_post setting #368

Open
wants to merge 1 commit 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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ feed:

To note, you shouldn't have to do this unless you already have a feed you're using, and you can't or wish not to redirect existing subscribers.

### Updated timestamp

By default, the feed will set `<updated>` to the current time at build time, as provided by Jekyll via `site.time`.

Enable the `updated: latest_post` setting to use a deterministic value based on when the latest post was published or updated. (The [jekyll-last-modified-at](https://github.com/gjtorikian/jekyll-last-modified-at) plugin is supported to help provide post modification times).

```yml
feed:
updated: latest_post
```

### Optional front matter

The plugin will use the following post metadata, automatically generated by Jekyll, which you can override via a post's YAML front matter:
Expand Down
14 changes: 13 additions & 1 deletion lib/jekyll-feed/feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<generator uri="https://jekyllrb.com/" version="{{ jekyll.version }}">Jekyll</generator>
<link href="{{ page.url | absolute_url }}" rel="self" type="application/atom+xml" />
<link href="{{ '/' | absolute_url }}" rel="alternate" type="text/html" {% if site.lang %}hreflang="{{ site.lang }}" {% endif %}/>
<updated>{{ site.time | date_to_xmlschema }}</updated>
<id>{{ page.url | absolute_url | xml_escape }}</id>

{% assign title = site.title | default: site.name %}
Expand Down Expand Up @@ -51,7 +50,20 @@
{% assign posts = posts | where_exp: "post", "post.draft != true" %}
{% endunless %}
{% assign posts = posts | sort: "date" | reverse %}

{% assign posts_limit = site.feed.posts_limit | default: 10 %}

{% assign last_updated = nil %}
{% if site.feed.updated == "latest_post" %}
{% for post in posts limit: posts_limit %}
{% assign post_updated = post.last_modified_at | default: post.date %}
{% if last_updated == nil or post_updated > last_updated %}
{% assign last_updated = post_updated %}
{% endif %}
{% endfor %}
{% endif %}
<updated>{{ last_updated | default: site.time | date_to_xmlschema }}</updated>

{% for post in posts limit: posts_limit %}
<entry{% if post.lang %}{{" "}}xml:lang="{{ post.lang }}"{% endif %}>
{% assign post_title = post.title | smartify | strip_html | normalize_whitespace | xml_escape %}
Expand Down
19 changes: 19 additions & 0 deletions spec/jekyll-feed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@

context "parsing" do
let(:feed) { RSS::Parser.parse(contents) }
let(:overrides) { { "time" => Time.parse("2018-12-31") } }

it "outputs an RSS feed" do
expect(feed.feed_type).to eql("atom")
Expand All @@ -136,6 +137,24 @@
expect(feed.generator.version).to eql(Jekyll::VERSION)
end

context "with site.feed.updated = latest_post" do
let(:overrides) do
{
"feed" => {
"updated" => "latest_post",
},
}
end

it "outputs the last post time" do
expect(feed.updated.content).to eql(Time.parse("2016-04-25"))
end
end

it "outputs the current time" do
expect(feed.updated.content).to eql(Time.parse("2018-12-31"))
end

it "includes the items" do
expect(feed.items.count).to eql(10)
end
Expand Down