-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrss.js
More file actions
52 lines (45 loc) · 1.4 KB
/
Copy pathrss.js
File metadata and controls
52 lines (45 loc) · 1.4 KB
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
import * as fs from 'fs';
import { tree } from './.routify/routes.js';
// thanks https://github.com/sveltejs/svelte/blob/master/site/src/routes/blog/rss.xml.js
function escapeHTML(html) {
const chars = {
'"' : 'quot',
"'": '#39',
'&': 'amp',
'<' : 'lt',
'>' : 'gt'
};
return html.replace(/["'&<>]/g, c => `&${chars[c]};`);
}
let rss = `<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Cornell Data Journal</title>
<link>https://cornelldatajourn.al/</link>
<description>Offering data-driven perspectives on current events, academics, politics, and beyond.</description>`;
const articles = tree.children
.find(entry => entry.path === '/articles').children
.map(post => ({
slug: post.path,
...post.children.find(child => child.isIndex).meta.frontmatter,
}));
const base_url = 'https://cornelldatajourn.al';
for (const { slug, title, description, date } of articles) {
rss += `
<item>
<title>${escapeHTML(title)}</title>
<link>${base_url + slug}</link>
<description>${escapeHTML(description)}</description>
<pubDate>${date}</pubDate>
<content><img src="https://cornelldatajourn.al/favicon.svg" /></content>
</item>`
}
rss += `
</channel>
</rss>`;
fs.writeFile("dist/rss.xml", rss, function(err) {
if(err) {
return console.error(err);
}
console.log("Created RSS feed at /rss.xml");
});