Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"@vercel/analytics": "^2.0.1",
"@vercel/og": "^1.0.0",
"@vercel/speed-insights": "^2.0.0",
"feed": "^5.2.1",
"github-slugger": "^2.0.0",
"gray-matter": "^4.0.3",
"next": "^16.3.0",
Expand Down
72 changes: 51 additions & 21 deletions pages/feed.xml.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { GetServerSideProps } from 'next'
import { Feed } from 'feed'
import { getAllPosts } from '@/lib/blog'
import { SITE_URL, SITE_NAME, BLOG_TITLE, BLOG_DESCRIPTION } from '@/lib/site'

Expand All @@ -8,36 +7,67 @@ export default function Feeds() {
return null
}

// Escape text for an XML element body or a double-quoted attribute value.
function escapeXml(s: string): string {
return s
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
}

// Titles and descriptions are free text from post frontmatter, so they go in
// CDATA. A literal "]]>" would end the section early, so we split it across two.
function cdata(s: string): string {
return `<![CDATA[${s.replace(/]]>/g, ']]]]><![CDATA[>')}]]>`
}

export const getServerSideProps: GetServerSideProps = async ({ res }) => {
const posts = getAllPosts()

const feed = new Feed({
title: BLOG_TITLE,
description: BLOG_DESCRIPTION,
id: `${SITE_URL}/blog`,
link: `${SITE_URL}/blog`,
language: 'en',
favicon: `${SITE_URL}/icon.png`,
copyright: `© ${new Date().getFullYear()} The Modelplane Authors. ${SITE_NAME}™ is a trademark.`,
feedLinks: { rss2: `${SITE_URL}/feed.xml` },
})
const channel = [
`<title>${escapeXml(BLOG_TITLE)}</title>`,
`<link>${escapeXml(`${SITE_URL}/blog`)}</link>`,
`<description>${escapeXml(BLOG_DESCRIPTION)}</description>`,
`<lastBuildDate>${new Date().toUTCString()}</lastBuildDate>`,
`<docs>https://validator.w3.org/feed/docs/rss2.html</docs>`,
`<language>en</language>`,
`<copyright>${escapeXml(
`© ${new Date().getFullYear()} The Modelplane Authors. ${SITE_NAME}™ is a trademark.`
)}</copyright>`,
]

for (const post of posts) {
const url = `${SITE_URL}/blog/${post.slug}`
feed.addItem({
title: post.title,
id: url,
link: url,
description: post.description,
date: new Date(post.date),
image: post.cover ? `${SITE_URL}${post.cover}` : undefined,
author: post.authors.map((a) => ({ name: a.name, link: a.url })),
})
const item = [
`<title>${cdata(post.title)}</title>`,
`<link>${escapeXml(url)}</link>`,
`<guid isPermaLink="false">${escapeXml(url)}</guid>`,
`<pubDate>${new Date(post.date).toUTCString()}</pubDate>`,
]
if (post.description) item.push(`<description>${cdata(post.description)}</description>`)
for (const a of post.authors) item.push(`<author>${escapeXml(a.name)}</author>`)
if (post.cover) {
// The enclosure type comes from the cover's file extension (cover.png -> image/png).
const image = `${SITE_URL}${post.cover}`
const ext = new URL(image).pathname.split('.').pop()
item.push(`<enclosure url="${escapeXml(image)}" length="0" type="image/${ext}"/>`)
}
channel.push('<item>', ...item.map((line) => ` ${line}`), '</item>')
}

const xml = [
'<?xml version="1.0" encoding="utf-8"?>',
'<rss version="2.0">',
' <channel>',
...channel.map((line) => ` ${line}`),
' </channel>',
'</rss>',
].join('\n')

res.setHeader('Content-Type', 'application/rss+xml; charset=utf-8')
res.setHeader('Cache-Control', 'public, s-maxage=3600, stale-while-revalidate=86400')
res.write(feed.rss2())
res.write(xml)
res.end()

return { props: {} }
Expand Down
Loading