-
Notifications
You must be signed in to change notification settings - Fork 1
/
rss.js
60 lines (54 loc) · 1.46 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
import { postLoader } from './helpers/post-loader'
import { js2xml } from 'xml-js'
const baseUrl = 'https://cuchi.me'
const siteName = 'Cuchi'
const baseRss = {
_declaration: {
_attributes: {
version: '1.0',
encoding: 'utf-8'
}
},
rss: {
_attributes: {
version: '2.0',
'xmlns:atom': 'http://www.w3.org/2005/Atom'
},
channel: {
title: { _text: siteName },
language: { _text: 'en-us'},
link: { _text: `${baseUrl}/rss.xml` },
'atom:link': { _attributes: {
href: `${baseUrl}/rss.xml`,
rel: 'self',
type: 'application/rss+xml'
}},
item: []
}
}
}
function buildItem(post) {
return {
title: { _text: post.title },
guid: { _text: `${baseUrl}/posts/${post.name}` },
pubDate: { _text: post.createdAt },
}
}
function RssBuilder() {
let builtRss
return function build(posts) {
if (!builtRss) {
for (const post of posts) {
baseRss.rss.channel.item.push(buildItem(post))
}
builtRss = baseRss
}
return builtRss
}
}
const buildRss = RssBuilder()
export default async function (req, res) {
const posts = await postLoader.getAll()
res.writeHead(200, { 'Content-Type': 'text/xml' })
.end(js2xml(buildRss(posts), { compact: true }))
}