Skip to content

Commit

Permalink
fix: skip generate if no posts
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Nov 18, 2019
1 parent 6a5559d commit e26d4a1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit e26d4a1

Please sign in to comment.