Skip to content

Commit

Permalink
Merge 0314e98 into af1be56
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Nov 26, 2019
2 parents af1be56 + 0314e98 commit c090938
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 32 deletions.
67 changes: 42 additions & 25 deletions index.js
Expand Up @@ -18,29 +18,36 @@ let type = config.type;
let path = config.path;
const feedFn = require('./lib/generator');

if (typeof type === 'string') type = [type];

if (!type || !Array.isArray(type)) {
type = ['atom'];
}

if (Array.isArray(type) && type.length > 2) {
type = type.slice(0, 2);
if (!type || (typeof type !== 'string' && !Array.isArray(type))) {
type = 'atom';
}

type = type.map((str, i) => {
str = str.toLowerCase();
if (str !== 'atom' && str !== 'rss2') {
if (i === 0) str = 'atom';
else str = 'rss2';
if (Array.isArray(type)) {
if (type.length > 2) type = type.slice(0, 2);
switch (type.length) {
case 0:
type = 'atom';
break;
case 1:
if (type[0] !== 'atom' && type[0] !== 'rss2') {
type = 'atom';
}
break;
case 2:
if (type !== ['atom', 'rss2'] && type !== ['rss2', 'atom']) {
type = ['atom', 'rss2'];
}
break;
}
return str;
});
}

if (typeof path === 'string') path = [path];
if (typeof type === 'string') {
if (type !== 'atom' && type !== 'rss2') type = 'atom';
}

if (!path || !Array.isArray(path)) {
path = type.map(str => str.concat('.xml'));
if (!path || typeof path !== typeof type) {
if (typeof type === 'string') path = type.concat('.xml');
else path = type.map(str => str.concat('.xml'));
}

if (Array.isArray(path)) {
Expand All @@ -49,20 +56,30 @@ if (Array.isArray(path)) {
else if (path.length === 0) path = type.map(str => str.concat('.xml'));
else path.push(type[1].concat('.xml'));
}

path = path.map(str => {
if (!extname(str)) return str.concat('.xml');
return str;
});
}

path = path.map(str => {
if (!extname(str)) return str.concat('.xml');
return str;
});
if (typeof path === 'string') {
if (!extname(path)) path += '.xml';
}

config.type = type;
config.path = path;

for (const feedType of type) {
hexo.extend.generator.register(feedType, locals => {
return feedFn.call(hexo, locals, feedType, path[type.indexOf(feedType)]);
if (typeof type === 'string') {
hexo.extend.generator.register(type, locals => {
return feedFn.call(hexo, locals, type, path);
});
} else {
for (const feedType of type) {
hexo.extend.generator.register(feedType, locals => {
return feedFn.call(hexo, locals, feedType, path[type.indexOf(feedType)]);
});
}
}

if (typeof config.autodiscovery !== 'boolean') config.autodiscovery = true;
Expand Down
16 changes: 10 additions & 6 deletions lib/autodiscovery.js
Expand Up @@ -5,16 +5,20 @@ const { url_for } = require('hexo-util');
function autodiscoveryInject(data) {
const { config } = this;
const { feed } = config;
const type = feed.type;
const path = feed.path;
const { path, type } = feed;
let autodiscoveryTag = '';

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])}" `
+ `title="${config.title}" type="application/${feedType.replace(/2$/, '')}+xml">\n`;
});
if (typeof type === 'string') {
autodiscoveryTag += `<link rel="alternate" href="${url_for.call(this, path)}" `
+ `title="${config.title}" type="application/${type.replace(/2$/, '')}+xml">\n`;
} else {
type.forEach((feedType, i) => {
autodiscoveryTag += `<link rel="alternate" href="${url_for.call(this, path[i])}" `
+ `title="${config.title}" type="application/${feedType.replace(/2$/, '')}+xml">\n`;
});
}

return data.replace(/<head>(?!<\/head>).+?<\/head>/s, (str) => str.replace('</head>', `${autodiscoveryTag}</head>`));
}
Expand Down
18 changes: 17 additions & 1 deletion test/index.js
Expand Up @@ -364,10 +364,26 @@ describe('Autodiscovery', () => {
const content = '<head><link></head>';
const result = autoDiscovery(content).trim();

const $ = cheerio.load(result);
$('link[type="application/atom+xml"]').length.should.eql(1);
$('link[type="application/atom+xml"]').attr('href').should.eql(urlConfig.root + hexo.config.feed.path[0]);
$('link[type="application/atom+xml"]').attr('title').should.eql(hexo.config.title);
});

it('default - string', () => {
hexo.config.feed.type = 'atom';
hexo.config.feed.path = 'atom.xml';

const content = '<head><link></head>';
const result = autoDiscovery(content).trim();

const $ = cheerio.load(result);
$('link[type="application/atom+xml"]').length.should.eql(1);
$('link[type="application/atom+xml"]').attr('href').should.eql(urlConfig.root + hexo.config.feed.path);
$('link[type="application/atom+xml"]').attr('title').should.eql(hexo.config.title);

hexo.config.feed.type = ['atom'];
hexo.config.feed.path = ['atom.xml'];
});

it('disable', () => {
Expand All @@ -387,7 +403,7 @@ describe('Autodiscovery', () => {
const result = autoDiscovery(content);

const $ = cheerio.load(result);
$('link[type="application/atom+xml"]').attr('href').should.eql(hexo.config.root + hexo.config.feed.path);
$('link[type="application/atom+xml"]').attr('href').should.eql(hexo.config.root + hexo.config.feed.path[0]);

hexo.config.root = '/';
});
Expand Down

0 comments on commit c090938

Please sign in to comment.