Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle sites with no posts #107

Merged
merged 3 commits into from
Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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