hexo-generator-feed
Advanced tools
Comparing version 2.1.1 to 2.2.0
91
index.js
/* global hexo */ | ||
'use strict'; | ||
const { extname } = require('path'); | ||
const { extname, join } = require('path'); | ||
@@ -14,3 +14,4 @@ const config = hexo.config.feed = Object.assign({ | ||
order_by: '-date', | ||
autodiscovery: true | ||
autodiscovery: true, | ||
template: '' | ||
}, hexo.config.feed); | ||
@@ -20,46 +21,84 @@ | ||
let path = config.path; | ||
let template = config.template; | ||
const feedFn = require('./lib/generator'); | ||
if (typeof type === 'string') type = [type]; | ||
if (!type || (typeof type !== 'string' && !Array.isArray(type))) { | ||
type = 'atom'; | ||
} | ||
if (!type || !Array.isArray(type)) { | ||
type = ['atom']; | ||
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; | ||
} | ||
} | ||
if (Array.isArray(type) && type.length > 2) { | ||
type = type.slice(0, 2); | ||
if (typeof type === 'string') { | ||
if (type !== 'atom' && type !== 'rss2') type = 'atom'; | ||
} | ||
type = type.map((str, i) => { | ||
str = str.toLowerCase(); | ||
if (str !== 'atom' && str !== 'rss2') { | ||
if (i === 0) str = 'atom'; | ||
else str = 'rss2'; | ||
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)) { | ||
if (path.length !== type.length) { | ||
if (path.length > type.length) path = path.slice(0, type.length); | ||
else if (path.length === 0) path = type.map(str => str.concat('.xml')); | ||
else path.push(type[1].concat('.xml')); | ||
} | ||
return str; | ||
}); | ||
if (!path || typeof path === 'string' || !Array.isArray(path)) { | ||
path = type.map(str => str.concat('.xml')); | ||
path = path.map(str => { | ||
if (!extname(str)) return str.concat('.xml'); | ||
return str; | ||
}); | ||
} | ||
if (Array.isArray(path) && path.length > 2) { | ||
path = path.slice(0, 2); | ||
if (typeof path === 'string') { | ||
if (!extname(path)) path += '.xml'; | ||
} | ||
path = path.map(str => { | ||
if (!extname(str)) return str.concat('.xml'); | ||
return str; | ||
}); | ||
if (typeof template !== 'string' && !Array.isArray(template)) { | ||
template = null; | ||
} | ||
if (Array.isArray(template)) { | ||
if (template.length >= 1) { | ||
if (template.length > type.length) template = template.slice(0, type.length); | ||
else if (template.length < type.length) template.push(join(__dirname, `${type[1]}.xml`)); | ||
} else { | ||
template = null; | ||
} | ||
} | ||
config.type = type; | ||
config.path = path; | ||
config.template = template; | ||
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; | ||
if (typeof config.autodiscovery !== 'boolean') config.autodiscovery = true; | ||
@@ -66,0 +105,0 @@ if (config.autodiscovery === true) { |
@@ -8,4 +8,3 @@ 'use strict'; | ||
const { feed } = config; | ||
const type = feed.type; | ||
const path = feed.path; | ||
const { path, type } = feed; | ||
let autodiscoveryTag = ''; | ||
@@ -15,6 +14,11 @@ | ||
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`; | ||
}); | ||
} | ||
@@ -21,0 +25,0 @@ return data.replace(/<head>(?!<\/head>).+?<\/head>/s, (str) => str.replace('</head>', `${autodiscoveryTag}</head>`)); |
@@ -17,12 +17,13 @@ 'use strict'; | ||
const atomTmplSrc = join(__dirname, '../atom.xml'); | ||
const atomTmpl = nunjucks.compile(readFileSync(atomTmplSrc, 'utf8'), env); | ||
const rss2TmplSrc = join(__dirname, '../rss2.xml'); | ||
const rss2Tmpl = nunjucks.compile(readFileSync(rss2TmplSrc, 'utf8'), env); | ||
module.exports = function(locals, type, path) { | ||
const config = this.config; | ||
const feedConfig = config.feed; | ||
const template = type === 'atom' ? atomTmpl : rss2Tmpl; | ||
let tmplSrc = join(__dirname, `../${type}.xml`); | ||
if (feedConfig.template) { | ||
if (typeof feedConfig.template === 'string') tmplSrc = feedConfig.template; | ||
else tmplSrc = feedConfig.template[feedConfig.type.indexOf(type)]; | ||
} | ||
const template = nunjucks.compile(readFileSync(tmplSrc, 'utf8'), env); | ||
let posts = locals.posts.sort(feedConfig.order_by || '-date'); | ||
@@ -29,0 +30,0 @@ posts = posts.filter(post => { |
{ | ||
"name": "hexo-generator-feed", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Feed generator plugin for Hexo", | ||
@@ -5,0 +5,0 @@ "main": "index", |
@@ -39,2 +39,3 @@ # hexo-generator-feed | ||
autodiscovery: true | ||
template: | ||
``` | ||
@@ -67,1 +68,18 @@ | ||
* Many themes already offer this feature, so you may also need to adjust the theme's config if you wish to disable it. | ||
- **template** - Custom template path(s). This file will be used to generate feed xml file, see the default templates: [atom.xml](atom.xml) and [rss2.xml](rss2.xml). | ||
* It is possible to specify just one custom template, even when this plugin is configured to output both feed types, | ||
``` yaml | ||
# (Optional) Exclude custom template from being copied into public/ folder | ||
# Alternatively, you could also prepend an underscore to its filename, e.g. _custom.xml | ||
# https://hexo.io/docs/configuration#Include-Exclude-Files-or-Folders | ||
exclude: | ||
- 'custom.xml' | ||
feed: | ||
type: | ||
- atom | ||
- rss2 | ||
template: | ||
- ./source/custom.xml | ||
# atom will be generated using custom.xml | ||
# rss2 will be generated using the default template instead | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14982
156
84