57 lines
1.9 KiB
JavaScript
57 lines
1.9 KiB
JavaScript
import { escapeHtml } from './html.js'
|
|
import { siteName, siteUrl } from './site.config.js'
|
|
|
|
export const toSitemapDate = (iso) => (iso ? iso.slice(0, 10) : '')
|
|
|
|
const toRssDate = (iso) => {
|
|
if (!iso) return ''
|
|
const date = new Date(iso)
|
|
return Number.isNaN(date.valueOf()) ? '' : date.toUTCString()
|
|
}
|
|
|
|
export const renderFeed = (items) => {
|
|
const lastBuild = toRssDate(items.find((item) => item.date)?.date) || new Date().toUTCString()
|
|
const itemXml = items
|
|
.map((item) => {
|
|
const loc = `${siteUrl}/${item.dir}/${item.name}/`
|
|
const description = item.description
|
|
const pubDate = toRssDate(item.date)
|
|
return `<item>
|
|
<title>${escapeHtml(item.title)}</title>
|
|
<link>${escapeHtml(loc)}</link>
|
|
<guid isPermaLink="true">${escapeHtml(loc)}</guid>
|
|
${pubDate ? `<pubDate>${escapeHtml(pubDate)}</pubDate>` : ''}
|
|
<category>${escapeHtml(item.dir)}</category>
|
|
${description ? `<description>${escapeHtml(description)}</description>` : ''}
|
|
</item>`
|
|
})
|
|
.join('\n ')
|
|
|
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
|
<channel>
|
|
<title>${escapeHtml(siteName)}</title>
|
|
<link>${escapeHtml(`${siteUrl}/`)}</link>
|
|
<description>${escapeHtml(`Notes and posts from ${siteName}`)}</description>
|
|
<language>en-US</language>
|
|
<atom:link href="${escapeHtml(`${siteUrl}/feed.xml`)}" rel="self" type="application/rss+xml"/>
|
|
<lastBuildDate>${escapeHtml(lastBuild)}</lastBuildDate>
|
|
${itemXml}
|
|
</channel>
|
|
</rss>
|
|
`
|
|
}
|
|
|
|
export const renderSitemap = (urls) => `<?xml version="1.0" encoding="UTF-8"?>
|
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
${urls
|
|
.map(({ loc, lastmod }) => {
|
|
const modified = lastmod ? `\n <lastmod>${escapeHtml(lastmod)}</lastmod>` : ''
|
|
return ` <url>
|
|
<loc>${escapeHtml(loc)}</loc>${modified}
|
|
</url>`
|
|
})
|
|
.join('\n')}
|
|
</urlset>
|
|
`
|