personal-website/lib/generateRssFeed.tsx
Filipe Medeiros 1aa2b2c599
fix: RSS feed
Signed-off-by: Filipe Medeiros <hello@filipesm.eu>
2023-12-03 12:25:55 +01:00

74 lines
2.2 KiB
TypeScript

import { Feed } from 'feed'
import { mkdir, writeFile } from 'fs/promises'
import { RenderBlockList } from '@/components/server/renderNotionContent'
import { getAllBlogPosts } from './notion/content/blogPosts'
import blockContentToPlainText from './notion/utils/blockContentToPlainText'
import getBlockRecursively from './notion/utils/getBlockRecursively'
import richTextAsPlainText from './notion/utils/richTextToPlainText'
export default async function generateRssFeed() {
const blogPosts = await getAllBlogPosts()
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL!
const author = {
name: 'Filipe Medeiros',
email: 'hello@filipesm.eu',
link: 'https://filipesm.eu',
}
const feed = new Feed({
title: author.name,
description:
'Blog pessoal do Filipe. Escreve sobre tópicos diversos, maioritariamente de tecnologia, política, economia e atualidade.',
author,
id: siteUrl,
link: siteUrl,
image: `${siteUrl}/favicon.ico`,
favicon: `${siteUrl}/favicon.ico`,
copyright: `Todos os direitos reservados ${new Date().getFullYear()}`,
feedLinks: {
rss2: `${siteUrl}/rss/feed.xml`,
json: `${siteUrl}/rss/feed.json`,
},
})
await Promise.all(
blogPosts.map(async (blogPost) => {
const url = `${siteUrl}/blog/${richTextAsPlainText(
blogPost.properties.Slug.rich_text,
)}`
const [content, reactDomServer] = await Promise.all([
getBlockRecursively(blogPost.id),
import('react-dom/server'),
])
const html = reactDomServer.renderToStaticMarkup(
<RenderBlockList rss blockList={content} />,
)
feed.addItem({
title: richTextAsPlainText(blogPost.properties.Name.title),
id: url,
link: url,
description: richTextAsPlainText(
blogPost.properties.Description.rich_text,
),
contributor: [author],
content: html,
date: new Date(
blogPost.properties.PublishDate.date?.start ?? blogPost.created_time,
),
})
}),
)
await mkdir('./public/rss', { recursive: true })
await Promise.all([
writeFile('./public/rss/feed.xml', feed.rss2(), 'utf8'),
writeFile('./public/rss/feed.json', feed.json1(), 'utf8'),
])
}