Skip to content

Commit

Permalink
Merge pull request #107 from tomap/fix/noPosts
Browse files Browse the repository at this point in the history
fix: handle sites with no posts
  • Loading branch information
curbengh authored Nov 17, 2019
2 parents 41a4505 + e8338a7 commit 8227043
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/autodiscovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function autodiscoveryInject(data) {
const path = feed.path;
let autodiscoveryTag = '';

if (data.match(/type=['|"]?application\/(atom|rss)\+xml['|"]?/i)) return;
if (data.match(/type=['|"]?application\/(atom|rss)\+xml['|"]?/i) || feed.autodiscovery === false) return;

type.forEach((feedType, i) => {
autodiscoveryTag += `<link rel="alternate" href="${url_for.call(this, path[i])}" `
Expand Down
5 changes: 5 additions & 0 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ module.exports = function(locals, type, path) {
return post.draft !== true;
});

if (posts.length <= 0) {
feedConfig.autodiscovery = false;
return;
}

if (feedConfig.limit) posts = posts.limit(feedConfig.limit);

let url = config.url;
Expand Down
39 changes: 37 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,30 @@ describe('Feed generator', () => {
});
});

it('No posts', () => {
const hexo = new Hexo(__dirname, {
silent: true
});
const Post = hexo.model('Post');
const generator = require('../lib/generator').bind(hexo);

require('../node_modules/hexo/lib/plugins/helper')(hexo);

hexo.config.feed = {
type: 'atom',
path: 'atom.xml'
};
hexo.config = Object.assign(hexo.config, urlConfig);
const feedCfg = hexo.config.feed;

return Post.insert([]).then(data => {
const locals = hexo.locals.toObject();
const result = typeof generator(locals, feedCfg.type, feedCfg.path);

result.should.eql('undefined');
});
});

describe('Autodiscovery', () => {
const hexo = new Hexo();
const autoDiscovery = require('../lib/autodiscovery').bind(hexo);
Expand All @@ -331,11 +355,11 @@ describe('Autodiscovery', () => {
};
hexo.config.feed = {
type: ['atom'],
path: ['atom.xml']
path: ['atom.xml'],
autodiscovery: true
};
hexo.config = Object.assign(hexo.config, urlConfig);


it('default', () => {
const content = '<head><link></head>';
const result = autoDiscovery(content).trim();
Expand All @@ -346,6 +370,17 @@ describe('Autodiscovery', () => {
$('link[type="application/atom+xml"]').attr('title').should.eql(hexo.config.title);
});

it('disable', () => {
hexo.config.feed.autodiscovery = false;
const content = '<head><link></head>';
const result = autoDiscovery(content);

const resultType = typeof result;
resultType.should.eql('undefined');

hexo.config.feed.autodiscovery = true;
});

it('prepend root', () => {
hexo.config.root = '/root/';
const content = '<head><link></head>';
Expand Down

0 comments on commit 8227043

Please sign in to comment.