forked from snxraven/ravenscott-blog
add /json route
This commit is contained in:
parent
0472fb4c71
commit
a89e84fd38
22
app.js
22
app.js
@ -319,6 +319,28 @@ app.get('/rss', (req, res) => {
|
|||||||
res.send(rssFeed);
|
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
|
// Create a URL object from the environment variable
|
||||||
const blog_URL = new URL(process.env.BLOG_URL);
|
const blog_URL = new URL(process.env.BLOG_URL);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user