import { Feed } from 'feed'; import parseMarkdown from '../src/parseMarkdown.js'; import fetch from 'node-fetch'; export default async function ({ app, route, res }) { if (!process.server || route.path !== '/blog.atom') { return; } const posts = await (await fetch(`${process.env.BASE_URL}/api/blog`)).json(); const feed = new Feed({ title: `${app.$t('title')} • ${app.$t('links.blog')}`, description: app.$t('description'), id: process.env.BASE_URL, link: `${process.env.BASE_URL}/blog.atom`, language: app.$config.locale, image: `${process.env.BASE_URL}/icon.png`, favicon: `${process.env.BASE_URL}/icon.png`, updated: new Date(posts[0].date), }); for (const post of posts) { const parsed = await parseMarkdown( (await import(`../locale/${app.$config.locale}/blog/${post.slug}.md`)).default, app.$translator, ); feed.addItem({ title: post.title, id: `${process.env.BASE_URL}/${app.$config.links.blogRoute}/${post.slug}`, link: `${process.env.BASE_URL}/${app.$config.links.blogRoute}/${post.slug}`, description: parsed.intro, content: parsed.content, author: post.authors.map((author) => ({ name: author, link: author.startsWith('@') ? `${process.env.BASE_URL}/${author}` : undefined, })), date: new Date(post.date), image: post.hero ? `${process.env.BASE_URL}${post.hero}` : undefined, }); } res.setHeader('Content-Type', 'application/rss+xml; charset=utf-8'); res.end(feed.atom1()); return new Promise(() => {}); // halt execution }