From e26d4a152fb41b0bdce24abb31141d7b9b4fdf15 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Mon, 18 Nov 2019 22:59:22 +0000 Subject: [PATCH] fix: skip generate if no posts --- lib/generator.js | 5 +++++ test/index.js | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/generator.js b/lib/generator.js index c38f001..0eb37bb 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -24,6 +24,11 @@ module.exports = function(locals) { return b.updated - a.updated; }); + if (posts.length <= 0) { + config.sitemap.rel = false; + return; + } + const xml = template(config).render({ config, posts diff --git a/test/index.js b/test/index.js index 7ba8c85..51b31b1 100644 --- a/test/index.js +++ b/test/index.js @@ -79,6 +79,22 @@ describe('Sitemap generator', () => { }); }); +it('No posts', () => { + const hexo = new Hexo(__dirname, { silent: true }); + hexo.config.sitemap = { + path: 'sitemap.xml' + }; + const Post = hexo.model('Post'); + const generator = require('../lib/generator').bind(hexo); + + return Post.insert([]).then(data => { + const locals = hexo.locals.toObject(); + const result = typeof generator(locals); + + result.should.eql('undefined'); + }); +}); + describe('Rel-Sitemap', () => { const hexo = new Hexo(); hexo.config.sitemap = {