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

fix: compatibility with existing theme #114

Merged
merged 4 commits into from
Nov 27, 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
67 changes: 42 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
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 === 'undefined') config.autodiscovery = true;
Expand Down
16 changes: 10 additions & 6 deletions lib/autodiscovery.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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