-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
generator.js
52 lines (41 loc) · 1.33 KB
/
generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
var nunjucks = require('nunjucks');
var env = new nunjucks.Environment();
var pathFn = require('path');
var fs = require('fs');
var gravatar = require('hexo/lib/plugins/helper/gravatar');
env.addFilter('uriencode', function(str) {
return encodeURI(str);
});
env.addFilter('noControlChars', function(str) {
return str.replace(/[\x00-\x1F\x7F]/g, '');
});
var atomTmplSrc = pathFn.join(__dirname, '../atom.xml');
var atomTmpl = nunjucks.compile(fs.readFileSync(atomTmplSrc, 'utf8'), env);
var rss2TmplSrc = pathFn.join(__dirname, '../rss2.xml');
var rss2Tmpl = nunjucks.compile(fs.readFileSync(rss2TmplSrc, 'utf8'), env);
module.exports = function(locals) {
var config = this.config;
var feedConfig = config.feed;
var template = feedConfig.type === 'rss2' ? rss2Tmpl : atomTmpl;
var posts = locals.posts.sort(feedConfig.order_by);
posts = posts.filter(function(post) {
return post.draft !== true;
});
if (feedConfig.limit) posts = posts.limit(feedConfig.limit || '-date');
var url = config.url;
if (url[url.length - 1] !== '/') url += '/';
var icon;
if (config.email) icon = gravatar(config.email);
var xml = template.render({
config: config,
url: url,
icon: icon,
posts: posts,
feed_url: config.root + feedConfig.path
});
return {
path: feedConfig.path,
data: xml
};
};