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 25, 2020
1 parent a139572 commit 8fe15cd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
4 changes: 2 additions & 2 deletions atom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link href="{{ feed_url | uriencode }}" rel="self"/>
{% if config.feed.hub %}<link href="{{ config.feed.hub | uriencode }}" rel="hub"/>{% endif %}
<link href="{{ url | uriencode }}"/>
<updated>{{ posts.first().updated.toISOString() }}</updated>
<updated>{% if posts.first().updated %}{{ posts.first().updated.toISOString() }}{% else %}{{ posts.first().date.toISOString() }}{% endif %}</updated>
<id>{{ url | uriencode }}</id>
{% if config.author %}
<author>
Expand All @@ -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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"cheerio": "^0.22.0",
"eslint": "^7.0.0",
"eslint-config-hexo": "^4.1.0",
"hexo": "^4.0.0",
"hexo": "hexojs/hexo",
"mocha": "^8.0.1",
"nyc": "15.0.1"
},
Expand Down
2 changes: 1 addition & 1 deletion rss2.xml
Original file line number Diff line number Diff line change
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
38 changes: 37 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ 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},
{source: 'updated', slug: 'updated', title: 'updated', date: 1e8 - 2, updated: 1e8 + 10}
]);
posts = Post.sort('-date');
locals = hexo.locals.toObject();
Expand Down Expand Up @@ -375,6 +377,40 @@ describe('Feed generator', () => {
feed_url: 'http://localhost/atom.xml'
}));
});

it('no updated date', async () => {
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 { items } = await p(result.data);
const post = items.filter(({ title }) => title === 'date');
const { date, updated } = post[0];

updated.should.eql(date);
});

it('updated date', async () => {
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 { items } = await p(result.data);
const post = items.filter(({ title }) => title === 'updated');
const { date, updated } = post[0];
const expected = new Date(1e8 + 10).toISOString();

updated.should.eql(expected);
date.should.not.eql(updated);
});
});

it('No posts', () => {
Expand Down
1 change: 1 addition & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const template = {
id: 'id',
title: 'title',
date: 'published',
updated: 'updated',
description: 'summary',
content: 'content[@type="html"]',
image: 'content[@type="image"]/@src',
Expand Down

0 comments on commit 8fe15cd

Please sign in to comment.