Skip to content

Instantly share code, notes, and snippets.

@riccardobevilacqua
Created November 29, 2020 17:22
Show Gist options
  • Save riccardobevilacqua/d3820b80718517448d8ad6c8151fc9ac to your computer and use it in GitHub Desktop.
Save riccardobevilacqua/d3820b80718517448d8ad6c8151fc9ac to your computer and use it in GitHub Desktop.
RSS generation in Next.js 10
import { BLOG_URL, BLOG_TITLE, BLOG_SUBTITLE} from '@lib/constants'
import markdownToHtml from '@lib/markdownToHtml'
export async function generateRssItem(post) {
const content = await markdownToHtml(post.content || '')
return `
<item>
<guid>${BLOG_URL}/posts/${post.slug}</guid>
<title>${post.title}</title>
<description>${post.excerpt}</description>
<link>${BLOG_URL}/posts/${post.slug}</link>
<pubDate>${new Date(post.date).toUTCString()}</pubDate>
<content:encoded><![CDATA[${content}]]></content:encoded>
</item>
`
}
export async function generateRss(posts) {
const itemsList = await Promise.all(posts.map(generateRssItem))
return `
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<title>${BLOG_TITLE}</title>
<link>${BLOG_URL}</link>
<description>${BLOG_SUBTITLE}</description>
<language>en</language>
<lastBuildDate>${new Date(posts[0].date).toUTCString()}</lastBuildDate>
<atom:link href="${BLOG_URL}" rel="self" type="application/rss+xml"/>
${itemsList.join('')}
</channel>
</rss>
`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment