forked from shuding/innsbruck-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss.js
95 lines (82 loc) · 2.27 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Created by shuding on 5/24/16.
*/
const fs = require('fs');
const path = require('path');
const marked = require('marked');
const FEED_CNT = 10;
function gen() {
let data = this.db.object;
if (!data.blog.plugin.rss) {
return;
}
let feed_url = data.blog.plugin.rss;
let xml_name = feed_url.split('/').reverse()[0];
let site_url = feed_url.split('/').reverse().filter((it, i) => i).reverse().join('/') + '/';
let feed = new this.rss({
title: data.blog.name,
feed_url,
site_url
});
let posts = this.db('posts').chain().orderBy('link', 'desc').slice(0, FEED_CNT).value();
posts && posts.forEach(post => {
feed.item({
title: post.title,
description: marked(post.content),
url: site_url + 'post/' + post.link,
guid: post.link,
date: post.time
});
});
fs.writeFileSync(path.join(__dirname, '..', xml_name), feed.xml({indent: true}));
}
module.exports = {
db: null,
rss: null,
init: _db => {
this.db = _db;
try {
require.resolve('rss');
this.rss = require('rss');
} catch (error) {
console.log('[rss] npm module rss is not installed, downloading...');
const exec = require('child_process').exec;
exec('npm i rss -S', (error, stdout, stderr) => {
if (error !== null) {
console.log('[rss] ' + stderr);
} else {
console.log('[rss] npm module rss installed');
this.rss = require('rss');
}
});
}
},
render: (template, options) => {
let context = {};
let rss = options.blog.plugin ? options.blog.plugin['rss'] || '' : '';
if (template == 'settings') {
// settings page
context.settings = `<div class="input-group">
<h5>RSS URL</h5>
<p><input type="text" name="plugin.rss" placeholder="http(s)://your.blog/rss.xml" value="${rss}"></p>
</div>`;
// All <input name='plugin.xxx'> will write the data into DB automatically
}
return context;
},
hook: {
onNewPost: () => {
gen.call(this);
},
onEditPost: () => {
gen.call(this);
},
onRemovePost: () => {
gen.call(this);
},
onSetting: () => {
gen.call(this);
}
}
};