forked from snxraven/ravenscott-blog
add rss feed
This commit is contained in:
parent
8f5ce78b73
commit
5a956cb708
40
app.js
40
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 = `<?xml version="1.0" encoding="UTF-8" ?>\n<rss version="2.0">\n<channel>\n`;
|
||||
rssFeed += `<title>Raven Scott Blog</title>\n`;
|
||||
rssFeed += `<link>https://${hostname}</link>\n`;
|
||||
rssFeed += `<description>This is the RSS feed for Raven Scott's blog.</description>\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 += `<item>\n`;
|
||||
rssFeed += `<title>${title}</title>\n`;
|
||||
rssFeed += `<link>https://${hostname}/blog/${slug}</link>\n`;
|
||||
rssFeed += `<description>${lead || 'Read the full post on the blog.'}</description>\n`;
|
||||
rssFeed += `<pubDate>${lastModifiedDate}</pubDate>\n`;
|
||||
rssFeed += `<guid>https://${hostname}/blog/${slug}</guid>\n`;
|
||||
rssFeed += `</item>\n`;
|
||||
});
|
||||
|
||||
rssFeed += `</channel>\n</rss>`;
|
||||
|
||||
// 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
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user