add /json route

This commit is contained in:
Raven Scott 2024-10-05 04:20:17 -04:00
parent 0472fb4c71
commit a89e84fd38

22
app.js
View File

@ -319,6 +319,28 @@ app.get('/rss', (req, res) => {
res.send(rssFeed);
});
// Route to return all blog content in plain text JSON format
app.get('/json', (req, res) => {
const blogFiles = fs.readdirSync(path.join(__dirname, 'markdown')).filter(file => file.endsWith('.md'));
const blogPosts = blogFiles.map(file => {
const title = file.replace('.md', '').replace(/-/g, ' ');
const slug = titleToSlug(title);
const markdownContent = fs.readFileSync(path.join(__dirname, 'markdown', file), 'utf-8');
// Strip all formatting and return plain text
const plainTextContent = markdownContent.replace(/[#*>\-`_~[\]]/g, '').replace(/\n+/g, ' ').trim();
return {
title,
slug,
content: plainTextContent
};
});
res.json(blogPosts);
});
// Create a URL object from the environment variable
const blog_URL = new URL(process.env.BLOG_URL);