-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.js
63 lines (58 loc) · 1.96 KB
/
feed.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
const Feed = require('feed').Feed;
const fs = require('fs');
const dataFilePath = '../assets/data/search.json';
const storagePath = '../feed/';
const siteDomain = 'https://ravelloh.top';
const authorINFO = {
name: 'RavelloH',
email: '[email protected]',
link: 'https://ravelloh.top/',
};
function HTMLDecode(str) {
var s = '';
if (str.length == 0) return '';
s = str.replace(/&/g, '&');
s = s.replace(/</g, '<');
s = s.replace(/>/g, '>');
s = s.replace(/ /g, ' ');
s = s.replace(/'/g, "'");
s = s.replace(/"/g, '"');
s = s.replace(/<br\/>/g, '\n');
return s;
}
const feed = new Feed({
title: "RavelloH's Blog / RavelloH的博客",
description: 'RSS - 博客文章订阅更新',
id: 'http://ravelloh.top/',
link: 'http://ravelloh.top/',
language: 'zh',
image: 'https://ravelloh.top/assets/images/avatar.jpg',
favicon: 'https://ravelloh.top/favicon.ico',
copyright: `Copyright © 2019 - ${new Date().getFullYear()} RavelloH. All rights reserved.`,
generator: 'https://github.com/RavelloH/local-feed-generation',
feedLinks: {
json: 'https://ravelloh.top/feed/feed.json',
atom: 'https://ravelloh.top/feed/atom.xml',
rss: 'https://ravelloh.top/feed/rss.xml',
},
author: authorINFO,
});
const posts = fs.readFileSync(dataFilePath, 'utf-8');
JSON.parse(posts).forEach((post) => {
feed.addItem({
title: post.name,
id: post.url,
link: siteDomain + post.url,
content: HTMLDecode(post.context),
author: authorINFO,
date: new Date(post.time),
titleList: post.title,
tag: post.tag,
category: post.class,
classification: post.class,
image: post.img[0] ? siteDomain + post.img[0] : null
});
});
fs.writeFileSync(storagePath + 'rss.xml', feed.rss2());
fs.writeFileSync(storagePath + 'atom.xml', feed.atom1());
fs.writeFileSync(storagePath + 'feed.json', feed.json1());