diff --git a/app.js b/app.js index cbdeda6..6d6cfe9 100644 --- a/app.js +++ b/app.js @@ -255,6 +255,46 @@ app.get('/sitemap.xml', (req, res) => { res.send(sitemap); }); +// RSS Feed Route +app.get('/rss', (req, res) => { + const hostname = req.headers.host || 'http://localhost'; // Adjust for production if needed + const blogFiles = fs.readdirSync(path.join(__dirname, 'markdown')).filter(file => file.endsWith('.md')); + + // Build RSS feed + let rssFeed = `\n\n\n`; + rssFeed += `Raven Scott Blog\n`; + rssFeed += `https://${hostname}\n`; + rssFeed += `This is the RSS feed for Raven Scott's blog.\n`; + + // Generate RSS items for each blog post + blogFiles.forEach(file => { + const title = file.replace('.md', ''); + const slug = titleToSlug(title); + + // Get the last modified date of the markdown file + const stats = fs.statSync(path.join(__dirname, 'markdown', file)); + const lastModifiedDate = new Date(stats.mtime).toUTCString(); // Use UTC date for RSS + + // Load and parse markdown content to extract a lead or description + const { lead } = loadMarkdownWithLead(file); + + // RSS item for each post + rssFeed += `\n`; + rssFeed += `${title}\n`; + rssFeed += `https://${hostname}/blog/${slug}\n`; + rssFeed += `${lead || 'Read the full post on the blog.'}\n`; + rssFeed += `${lastModifiedDate}\n`; + rssFeed += `https://${hostname}/blog/${slug}\n`; + rssFeed += `\n`; + }); + + rssFeed += `\n`; + + // Set content type to XML and send the RSS feed + res.header('Content-Type', 'application/rss+xml'); + res.send(rssFeed); +}); + // Global 404 handler for any other unmatched routes app.use((req, res) => { res.redirect('/'); // Redirect to the home page for any 404 error diff --git a/package.json b/package.json index f0e86bf..626ad6c 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "express": "^4.21.0", "highlight.js": "^11.10.0", "marked": "^14.1.2", - "nodemailer": "^6.9.15" + "nodemailer": "^6.9.15", + "rss": "^1.2.2" } }