2021-07-23 15:45:09 +00:00
|
|
|
const SITE_URL = 'https://stacker.news'
|
|
|
|
const SITE_TITLE = 'Stacker News'
|
2021-08-09 20:52:41 +00:00
|
|
|
const SITE_SUBTITLE = 'Like Hacker News, but we pay you Bitcoin.'
|
|
|
|
|
|
|
|
function escapeXml (unsafe) {
|
|
|
|
return unsafe.replace(/[<>&'"]/g, function (c) {
|
|
|
|
switch (c) {
|
|
|
|
case '<': return '<'
|
|
|
|
case '>': return '>'
|
|
|
|
case '&': return '&'
|
|
|
|
case '\'': return '''
|
|
|
|
case '"': return '"'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-07-23 15:45:09 +00:00
|
|
|
|
|
|
|
const generateRssItem = (item) => {
|
2021-08-19 21:19:35 +00:00
|
|
|
const guid = `${SITE_URL}/items/${item.id}`
|
|
|
|
const link = item.url || guid
|
2021-07-23 15:45:09 +00:00
|
|
|
return `
|
|
|
|
<item>
|
|
|
|
<guid>${SITE_URL}/items/${item.id}</guid>
|
2021-08-09 20:52:41 +00:00
|
|
|
<title>${escapeXml(item.title)}</title>
|
2021-08-24 19:44:00 +00:00
|
|
|
<link>${escapeXml(link)}</link>
|
2021-08-19 21:19:35 +00:00
|
|
|
<comments>${guid}</comments>
|
|
|
|
<description><![CDATA[<a href="${guid}">Comments</a>]]></description>
|
2021-07-23 15:45:09 +00:00
|
|
|
<pubDate>${new Date(item.createdAt).toUTCString()}</pubDate>
|
|
|
|
</item>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function generateRssFeed (items) {
|
|
|
|
const itemsList = items.map(generateRssItem)
|
2022-01-07 21:29:38 +00:00
|
|
|
return `<?xml version="1.0" encoding="UTF-8" ?>
|
2021-08-24 19:44:00 +00:00
|
|
|
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
2021-07-23 15:45:09 +00:00
|
|
|
<channel>
|
|
|
|
<title>${SITE_TITLE}</title>
|
|
|
|
<link>${SITE_URL}</link>
|
|
|
|
<description>${SITE_SUBTITLE}</description>
|
|
|
|
<language>en</language>
|
|
|
|
<lastBuildDate>${new Date().toUTCString()}</lastBuildDate>
|
2021-08-24 19:44:00 +00:00
|
|
|
<atom:link href="${SITE_URL}/rss" rel="self" type="application/rss+xml" />
|
2021-07-23 15:45:09 +00:00
|
|
|
${itemsList.join('')}
|
|
|
|
</channel>
|
|
|
|
</rss>
|
|
|
|
`
|
|
|
|
}
|