From 8fe15cd03c4c648eab1fb4d9f2cdf4693cc492ef Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Thu, 28 Nov 2019 11:34:53 +0000 Subject: [PATCH] fix: handle null post.updated --- atom.xml | 4 ++-- package.json | 2 +- rss2.xml | 2 +- test/index.js | 38 +++++++++++++++++++++++++++++++++++++- test/parse.js | 1 + 5 files changed, 42 insertions(+), 5 deletions(-) diff --git a/atom.xml b/atom.xml index 299d813..654931a 100644 --- a/atom.xml +++ b/atom.xml @@ -6,7 +6,7 @@ {% if config.feed.hub %}{% endif %} - {{ posts.first().updated.toISOString() }} + {% if posts.first().updated %}{{ posts.first().updated.toISOString() }}{% else %}{{ posts.first().date.toISOString() }}{% endif %} {{ url | uriencode }} {% if config.author %} @@ -21,7 +21,7 @@ {{ post.permalink | uriencode }} {{ post.date.toISOString() }} - {{ post.updated.toISOString() }} + {% if post.updated %}{{ post.updated.toISOString() }}{% else %}{{ post.date.toISOString() }}{% endif %} {% if config.feed.content and post.content %} {% endif %} diff --git a/package.json b/package.json index 7362d87..4ff28d3 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/rss2.xml b/rss2.xml index fb0522c..158663b 100644 --- a/rss2.xml +++ b/rss2.xml @@ -15,7 +15,7 @@ {% if config.feed.hub %}{% endif %} {{ config.description }} - {{ posts.first().updated.toDate().toUTCString() }} + {% if posts.first().updated %}{{ posts.first().updated.toDate().toUTCString() }}{% else %}{{ posts.first().date.toDate().toUTCString() }}{% endif %} http://hexo.io/ {% for post in posts.toArray() %} diff --git a/test/index.js b/test/index.js index 8edc103..81bc3a8 100644 --- a/test/index.js +++ b/test/index.js @@ -52,7 +52,9 @@ describe('Feed generator', () => { await Post.insert([ {source: 'foo', slug: 'foo', content: '
TestHTML
', 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(); @@ -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', () => { diff --git a/test/parse.js b/test/parse.js index 7a49dce..92f880f 100644 --- a/test/parse.js +++ b/test/parse.js @@ -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',