Skip to content

Commit

Permalink
fix: handle null post.updated
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Jun 22, 2020
1 parent a139572 commit 404581a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion atom.xml
Expand Up @@ -21,7 +21,7 @@
<link href="{{ post.permalink | uriencode }}"/>
<id>{{ post.permalink | uriencode }}</id>
<published>{{ post.date.toISOString() }}</published>
<updated>{{ post.updated.toISOString() }}</updated>
<updated>{% if post.updated %}{{ post.updated.toISOString() }}{% else %}{{ post.date.toISOString() }}{% endif %}</updated>
{% if config.feed.content and post.content %}
<content type="html"><![CDATA[{{ post.content | noControlChars | safe }}]]></content>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion rss2.xml
Expand Up @@ -15,7 +15,7 @@
<atom:link href="{{ feed_url | uriencode }}" rel="self" type="application/rss+xml"/>
{% if config.feed.hub %}<atom:link href="{{ config.feed.hub | uriencode }}" rel="hub"/>{% endif %}
<description>{{ config.description }}</description>
<pubDate>{{ posts.first().updated.toDate().toUTCString() }}</pubDate>
<pubDate>{% if posts.first().updated %}{{ posts.first().updated.toDate().toUTCString() }}{% else %}{{ posts.first().date.toDate().toUTCString() }}{% endif %}</pubDate>
<generator>http://hexo.io/</generator>
{% for post in posts.toArray() %}
<item>
Expand Down
22 changes: 21 additions & 1 deletion test/index.js
Expand Up @@ -52,7 +52,8 @@ describe('Feed generator', () => {
await Post.insert([
{source: 'foo', slug: 'foo', content: '<h6>TestHTML</h6>', date: 1e8},
{source: 'bar', slug: 'bar', date: 1e8 + 1},
{source: 'baz', slug: 'baz', title: 'With Image', image: 'test.png', date: 1e8 - 1}
{source: 'baz', slug: 'baz', title: 'With Image', image: 'test.png', date: 1e8 - 1},
{source: 'date', slug: 'date', title: 'date', date: 1e8 - 2, updated: undefined}
]);
posts = Post.sort('-date');
locals = hexo.locals.toObject();
Expand Down Expand Up @@ -375,6 +376,25 @@ describe('Feed generator', () => {
feed_url: 'http://localhost/atom.xml'
}));
});

it('no updated date', () => {
hexo.config.optional_updated = true;
hexo.config.feed = {
type: 'atom',
path: 'atom.xml'
};
hexo.config = Object.assign(hexo.config, urlConfig);
const feedCfg = hexo.config.feed;
const result = generator(locals, feedCfg.type, feedCfg.path);

const $ = cheerio.load(result.data);
const updated = $('feed>entry:nth-of-type(4)>updated').text();
const date = $('feed>entry:nth-of-type(4)>published').text();

updated.should.eql(date);

hexo.config.optional_updated = false;
});
});

it('No posts', () => {
Expand Down

0 comments on commit 404581a

Please sign in to comment.