diff --git a/index.js b/index.js index da42b4d..ac60eda 100644 --- a/index.js +++ b/index.js @@ -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)) { @@ -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; diff --git a/lib/autodiscovery.js b/lib/autodiscovery.js index 764ac7f..5d68be7 100644 --- a/lib/autodiscovery.js +++ b/lib/autodiscovery.js @@ -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 += `\n`; - }); + if (typeof type === 'string') { + autodiscoveryTag += `\n`; + } else { + type.forEach((feedType, i) => { + autodiscoveryTag += `\n`; + }); + } return data.replace(/(?!<\/head>).+?<\/head>/s, (str) => str.replace('', `${autodiscoveryTag}`)); } diff --git a/test/index.js b/test/index.js index c3d33cc..9516f89 100644 --- a/test/index.js +++ b/test/index.js @@ -364,10 +364,26 @@ describe('Autodiscovery', () => { const content = ''; 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 = ''; + 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', () => { @@ -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 = '/'; });