forked from cnodejs/nodeclub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss.js
63 lines (57 loc) · 1.86 KB
/
rss.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
53
54
55
56
57
58
59
60
61
62
63
var config = require('../config');
var convert = require('data2xml')();
var Topic = require('../proxy').Topic;
var cache = require('../common/cache');
var renderHelper = require('../common/render_helper');
var eventproxy = require('eventproxy');
exports.index = function (req, res, next) {
if (!config.rss) {
res.statusCode = 404;
return res.send('Please set `rss` in config.js');
}
res.contentType('application/xml');
var ep = new eventproxy();
ep.fail(next);
cache.get('rss', ep.done(function (rss) {
if (!config.debug && rss) {
res.send(rss);
} else {
var opt = {
limit: config.rss.max_rss_items,
sort: '-create_at',
};
Topic.getTopicsByQuery({tab: {$nin: ['dev']}}, opt, function (err, topics) {
if (err) {
return next(err);
}
var rss_obj = {
_attr: { version: '2.0' },
channel: {
title: config.rss.title,
link: config.rss.link,
language: config.rss.language,
description: config.rss.description,
item: []
}
};
topics.forEach(function (topic) {
rss_obj.channel.item.push({
title: topic.title,
link: config.rss.link + '/topic/' + topic._id,
guid: config.rss.link + '/topic/' + topic._id,
description: renderHelper.markdown(topic.content),
author: topic.author.loginname,
pubDate: topic.create_at.toUTCString()
});
});
var rssContent = convert('rss', rss_obj);
rssContent = utf8ForXml(rssContent)
cache.set('rss', rssContent, 60 * 5); // 五分钟
res.send(rssContent);
});
}
}));
};
function utf8ForXml(inputStr) {
return inputStr.replace(/[^\x09\x0A\x0D\x20-\xFF\x85\xA0-\uD7FF\uE000-\uFDCF\uFDE0-\uFFFD]/gm, '');
}