const express = require('express'); const router = express.Router(); const { Feed } = require('feed'); const contentService = require('../services/content'); function generateFeed(site) { const feed = new Feed({ title: site.title, description: site.tagline, id: site.url, link: site.url, language: 'en', image: `${site.url}/images/og-default.png`, favicon: `${site.url}/favicon.ico`, copyright: `All rights reserved ${new Date().getFullYear()}, ${site.author ? site.author.name : site.title}`, author: { name: site.author ? site.author.name : site.title, link: site.url } }); const posts = contentService.getAllPosts(); for (const post of posts) { const postUrl = `${site.url}/${post.slug}`; feed.addItem({ title: post.title, id: postUrl, link: postUrl, description: post.excerpt, content: post.contentHtml, author: [ { name: post.author, link: site.url } ], date: post.date, image: post.cover ? (post.cover.startsWith('http') ? post.cover : `${site.url}${post.cover}`) : undefined }); } return feed; } router.get(['/feed', '/rss.xml', '/feed.xml'], (req, res) => { const feed = generateFeed(res.locals.site); res.header('Content-Type', 'application/xml; charset=utf-8'); res.send(feed.rss2()); }); router.get('/atom.xml', (req, res) => { const feed = generateFeed(res.locals.site); res.header('Content-Type', 'application/atom+xml; charset=utf-8'); res.send(feed.atom1()); }); module.exports = router;