Implement an RSS feed.

This commit is contained in:
Dave Smith-Hayes 2024-12-11 03:00:35 +00:00
parent db0836a499
commit c999e7feab
5 changed files with 17 additions and 20 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -7,11 +7,11 @@
"@types/yaml-front-matter": "^4.1.3",
"feed": "^4.2.2",
"highlight.js": "^11.10.0",
"hono": "^4.4.13",
"marked": "^13.0.2",
"marked-highlight": "^2.1.3",
"hono": "^4.6.13",
"marked": "^13.0.3",
"marked-highlight": "^2.2.1",
"remark": "^15.0.1",
"smol-toml": "^1.3.0",
"smol-toml": "^1.3.1",
"yaml-front-matter": "^4.1.1"
},
"devDependencies": {

View File

@ -1,25 +1,20 @@
import { Hono, Context } from 'hono';
import { PostService } from '@blog/services/post-file';
import { getFeed } from '@blog/services/feed-generator';
import { Feed } from 'feed';
const feed = new Hono<{ Variables: { postService: PostService }}>();
let feedBuffer: string = "";
export async function getFeedFile(c: Context) {
c.header('Content-Type', 'text/xml');
if (!feedBuffer) {
const postService: PostService = c.get('postService');
const posts = postService.getPublishedPosts();
// get the site meta
// create the feed
// get the feed data
feedBuffer = "";
const feed: Feed = await getFeed(postService);
feedBuffer = feed.rss2();
}
c.body(feedBuffer);
c.header('Content-Type', 'text/xml');
return c.body(feedBuffer);
}
feed.get('/rss.xml', getFeedFile);

View File

@ -4,6 +4,7 @@ import { serveStatic } from 'hono/bun';
import { Page } from '@blog/templates/Page';
import home from '@blog/handlers/home';
import posts from '@blog/handlers/posts';
import feed from '@blog/handlers/feed';
import type { SiteMeta } from '@blog/models/SiteMeta';
import { postFileMiddleware } from '@blog/middleware/post-service';
import { logger } from 'hono/logger';
@ -36,6 +37,7 @@ app.use('/static/*', serveStatic({
}));
app.route('/', home);
app.route('/feed', feed)
app.route('/posts', posts);
console.log("Starting the blog");

View File

@ -6,7 +6,7 @@ import type { Post } from '@blog/models/post';
const updatedDate = new Date();
export async function getFeed(postService: PostService): Feed {
const config = getSiteConfig();
const config = await getSiteConfig();
const feed = new Feed({
title: config.title,
description: config.site.description,
@ -22,20 +22,20 @@ export async function getFeed(postService: PostService): Feed {
}
});
postService.getAllPosts().map((post: Post) => {
postService.getPublishedPosts().map((post: Post) => {
feed.addItem({
title: post.meta.title,
id: post.meta.slug,
link: post.meta.slug,
description: post.meta.description,
content: post.html,
author: [
author: {
name: config.author,
email: config.email
]
}
});
});
return feed;
}
};