Skip to content

Commit 5819c7f

Browse files
authored
Merge pull request #117 from curbengh/optional-updated
fix: compatibility with null post.updated
2 parents 62f0cc2 + e597747 commit 5819c7f

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

atom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<link href="{{ feed_url | uriencode }}" rel="self"/>
77
{% if config.feed.hub %}<link href="{{ config.feed.hub | uriencode }}" rel="hub"/>{% endif %}
88
<link href="{{ url | uriencode }}"/>
9-
<updated>{{ posts.first().updated.toISOString() }}</updated>
9+
<updated>{% if posts.first().updated %}{{ posts.first().updated.toISOString() }}{% else %}{{ posts.first().date.toISOString() }}{% endif %}</updated>
1010
<id>{{ url | uriencode }}</id>
1111
{% if config.author %}
1212
<author>
@@ -21,7 +21,7 @@
2121
<link href="{{ post.permalink | uriencode }}"/>
2222
<id>{{ post.permalink | uriencode }}</id>
2323
<published>{{ post.date.toISOString() }}</published>
24-
<updated>{{ post.updated.toISOString() }}</updated>
24+
<updated>{% if post.updated %}{{ post.updated.toISOString() }}{% else %}{{ post.date.toISOString() }}{% endif %}</updated>
2525
{% if config.feed.content and post.content %}
2626
<content type="html"><![CDATA[{{ post.content | noControlChars | safe }}]]></content>
2727
{% endif %}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"cheerio": "^0.22.0",
4343
"eslint": "^7.0.0",
4444
"eslint-config-hexo": "^4.1.0",
45-
"hexo": "^4.0.0",
45+
"hexo": "^5.0.0",
4646
"mocha": "^8.0.1",
4747
"nyc": "^15.1.0"
4848
},

rss2.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<atom:link href="{{ feed_url | uriencode }}" rel="self" type="application/rss+xml"/>
1616
{% if config.feed.hub %}<atom:link href="{{ config.feed.hub | uriencode }}" rel="hub"/>{% endif %}
1717
<description>{{ config.description }}</description>
18-
<pubDate>{{ posts.first().updated.toDate().toUTCString() }}</pubDate>
18+
<pubDate>{% if posts.first().updated %}{{ posts.first().updated.toDate().toUTCString() }}{% else %}{{ posts.first().date.toDate().toUTCString() }}{% endif %}</pubDate>
1919
<generator>http://hexo.io/</generator>
2020
{% for post in posts.toArray() %}
2121
<item>

test/index.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ describe('Feed generator', () => {
5757
await Post.insert([
5858
{source: 'foo', slug: 'foo', content: '<h6>TestHTML</h6>', date: 1e8},
5959
{source: 'bar', slug: 'bar', date: 1e8 + 1},
60-
{source: 'baz', slug: 'baz', title: 'With Image', image: 'test.png', date: 1e8 - 1}
60+
{source: 'baz', slug: 'baz', title: 'With Image', image: 'test.png', date: 1e8 - 1},
61+
{source: 'date', slug: 'date', title: 'date', date: 1e8 - 2, updated: undefined},
62+
{source: 'updated', slug: 'updated', title: 'updated', date: 1e8 - 2, updated: 1e8 + 10}
6163
]);
6264
posts = Post.sort('-date');
6365
locals = hexo.locals.toObject();
@@ -396,6 +398,40 @@ describe('Feed generator', () => {
396398
feed_url: 'http://localhost/atom.xml'
397399
}));
398400
});
401+
402+
it('no updated date', async () => {
403+
hexo.config.feed = {
404+
type: 'atom',
405+
path: 'atom.xml'
406+
};
407+
hexo.config = Object.assign(hexo.config, urlConfig);
408+
const feedCfg = hexo.config.feed;
409+
const result = generator(locals, feedCfg.type, feedCfg.path);
410+
411+
const { items } = await p(result.data);
412+
const post = items.filter(({ title }) => title === 'date');
413+
const { date, updated } = post[0];
414+
415+
updated.should.eql(date);
416+
});
417+
418+
it('updated date', async () => {
419+
hexo.config.feed = {
420+
type: 'atom',
421+
path: 'atom.xml'
422+
};
423+
hexo.config = Object.assign(hexo.config, urlConfig);
424+
const feedCfg = hexo.config.feed;
425+
const result = generator(locals, feedCfg.type, feedCfg.path);
426+
427+
const { items } = await p(result.data);
428+
const post = items.filter(({ title }) => title === 'updated');
429+
const { date, updated } = post[0];
430+
const expected = new Date(1e8 + 10).toISOString();
431+
432+
updated.should.eql(expected);
433+
date.should.not.eql(updated);
434+
});
399435
});
400436

401437
it('No posts', () => {

test/parse.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const template = {
4343
id: 'id',
4444
title: 'title',
4545
date: 'published',
46+
updated: 'updated',
4647
description: 'summary',
4748
content: 'content[@type="html"]',
4849
image: 'content[@type="image"]/@src',

0 commit comments

Comments
 (0)