hexo-generator-feed
Advanced tools
Comparing version 2.0.0 to 2.1.0
61
index.js
/* global hexo */ | ||
'use strict'; | ||
const pathFn = require('path'); | ||
const { extname } = require('path'); | ||
@@ -13,24 +13,55 @@ const config = hexo.config.feed = Object.assign({ | ||
content_limit_delim: '', | ||
order_by: '-date' | ||
order_by: '-date', | ||
autodiscovery: true | ||
}, hexo.config.feed); | ||
const type = config.type.toLowerCase(); | ||
let type = config.type; | ||
let path = config.path; | ||
const feedFn = require('./lib/generator'); | ||
// Check feed type | ||
if (type !== 'atom' && type !== 'rss2') { | ||
config.type = 'atom'; | ||
} else { | ||
config.type = type; | ||
if (typeof type === 'string') type = [type]; | ||
if (!type || !Array.isArray(type)) { | ||
type = ['atom']; | ||
} | ||
// Set default feed path | ||
if (!config.path) { | ||
config.path = config.type + '.xml'; | ||
if (Array.isArray(type) && type.length > 2) { | ||
type = type.slice(0, 2); | ||
} | ||
// Add extension name if don't have | ||
if (!pathFn.extname(config.path)) { | ||
config.path += '.xml'; | ||
type = type.map((str, i) => { | ||
str = str.toLowerCase(); | ||
if (str !== 'atom' && str !== 'rss2') { | ||
if (i === 0) str = 'atom'; | ||
else str = 'rss2'; | ||
} | ||
return str; | ||
}); | ||
if (!path || typeof path === 'string' || !Array.isArray(path)) { | ||
path = type.map(str => str.concat('.xml')); | ||
} | ||
hexo.extend.generator.register('feed', require('./lib/generator')); | ||
if (Array.isArray(path) && path.length > 2) { | ||
path = path.slice(0, 2); | ||
} | ||
path = path.map(str => { | ||
if (!extname(str)) return str.concat('.xml'); | ||
return str; | ||
}); | ||
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 config.autodiscovery === 'undefined') config.autodiscovery = true; | ||
if (config.autodiscovery === true) { | ||
hexo.extend.filter.register('after_render:html', require('./lib/autodiscovery')); | ||
} |
@@ -5,8 +5,8 @@ 'use strict'; | ||
const env = new nunjucks.Environment(); | ||
const pathFn = require('path'); | ||
const fs = require('fs'); | ||
const gravatar = require('hexo/lib/plugins/helper/gravatar'); // eslint-disable-line node/no-unpublished-require | ||
const { join } = require('path'); | ||
const { readFileSync } = require('fs'); | ||
const { encodeURL, gravatar, full_url_for } = require('hexo-util'); | ||
env.addFilter('uriencode', str => { | ||
return encodeURI(str); | ||
return encodeURL(str); | ||
}); | ||
@@ -18,11 +18,11 @@ | ||
const atomTmplSrc = pathFn.join(__dirname, '../atom.xml'); | ||
const atomTmpl = nunjucks.compile(fs.readFileSync(atomTmplSrc, 'utf8'), env); | ||
const rss2TmplSrc = pathFn.join(__dirname, '../rss2.xml'); | ||
const rss2Tmpl = nunjucks.compile(fs.readFileSync(rss2TmplSrc, 'utf8'), env); | ||
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) { | ||
module.exports = function(locals, type, path) { | ||
const config = this.config; | ||
const feedConfig = config.feed; | ||
const template = feedConfig.type === 'rss2' ? rss2Tmpl : atomTmpl; | ||
const template = type === 'atom' ? atomTmpl : rss2Tmpl; | ||
@@ -40,17 +40,17 @@ let posts = locals.posts.sort(feedConfig.order_by || '-date'); | ||
let icon = ''; | ||
if (feedConfig.icon) icon = url + encodeURI(feedConfig.icon); | ||
if (feedConfig.icon) icon = full_url_for.call(this, feedConfig.icon); | ||
else if (config.email) icon = gravatar(config.email); | ||
const xml = template.render({ | ||
config: config, | ||
url: url, | ||
icon: icon, | ||
posts: posts, | ||
feed_url: config.root + feedConfig.path | ||
config, | ||
url, | ||
icon, | ||
posts, | ||
feed_url: config.root + path | ||
}); | ||
return { | ||
path: feedConfig.path, | ||
path, | ||
data: xml | ||
}; | ||
}; |
{ | ||
"name": "hexo-generator-feed", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Feed generator plugin for Hexo", | ||
@@ -35,2 +35,3 @@ "main": "index", | ||
"dependencies": { | ||
"hexo-util": "^1.3.0", | ||
"nunjucks": "^3.0.0" | ||
@@ -44,3 +45,3 @@ }, | ||
"eslint-config-hexo": "^3.0.0", | ||
"hexo": "^3.3.1", | ||
"hexo": "^4.0.0", | ||
"mocha": "^6.0.2", | ||
@@ -50,4 +51,4 @@ "nyc": "14.1.1" | ||
"engines": { | ||
"node": ">=8.6.0" | ||
"node": ">=8.10.0" | ||
} | ||
} |
@@ -37,6 +37,21 @@ # hexo-generator-feed | ||
icon: icon.png | ||
autodiscovery: true | ||
``` | ||
- **type** - Feed type. (atom/rss2) | ||
- **path** - Feed path. (Default: atom.xml/rss2.xml) | ||
- **type** - Feed type. `atom` or `rss2`. Specify `['atom', 'rss2']` to output both types. (Default: `atom`) | ||
* Example: | ||
``` yaml | ||
feed: | ||
# Generate atom feed | ||
type: atom | ||
# Generate both atom and rss2 feeds | ||
type: | ||
- atom | ||
- rss2 | ||
path: | ||
- atom.xml | ||
- rss2.xml | ||
``` | ||
- **path** - Feed path. When both types are specified, path must follow the order of type value. (Default: atom.xml/rss2.xml) | ||
- **limit** - Maximum number of posts in the feed (Use `0` or `false` to show all posts) | ||
@@ -49,1 +64,3 @@ - **hub** - URL of the PubSubHubbub hubs (Leave it empty if you don't use it) | ||
- **icon** - (optional) Custom feed icon. Defaults to a gravatar of email specified in the main config. | ||
- **autodiscovery** - Add feed [autodiscovery](http://www.rssboard.org/rss-autodiscovery). (Default: `true`) | ||
* Many themes already offer this feature, so you may also need to adjust the theme's config if you wish to disable it. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
12742
8
110
65
0
2
+ Addedhexo-util@^1.3.0
+ Addedbluebird@3.7.2(transitive)
+ Addedcamel-case@4.1.2(transitive)
+ Addedcross-spawn@7.0.3(transitive)
+ Addeddeepmerge@4.3.1(transitive)
+ Addeddom-serializer@1.4.1(transitive)
+ Addeddomelementtype@2.3.0(transitive)
+ Addeddomhandler@3.3.04.3.1(transitive)
+ Addeddomutils@2.8.0(transitive)
+ Addedentities@2.2.0(transitive)
+ Addedhexo-util@1.9.1(transitive)
+ Addedhighlight.js@9.18.5(transitive)
+ Addedhtmlparser2@4.1.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedlower-case@2.0.2(transitive)
+ Addedmin-indent@1.0.1(transitive)
+ Addedno-case@3.0.4(transitive)
+ Addedpascal-case@3.1.2(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedprismjs@1.29.0(transitive)
+ Addedpunycode.js@2.3.1(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedstrip-indent@3.0.0(transitive)
+ Addedstriptags@3.2.0(transitive)
+ Addedtslib@2.6.3(transitive)
+ Addedwhich@2.0.2(transitive)