Compare commits

...

9 Commits

10 changed files with 118 additions and 3 deletions

BIN
bun.lockb

Binary file not shown.

12
config/site.toml Normal file
View File

@ -0,0 +1,12 @@
title = "davesmithhayes.com"
[owner]
name = "Dave Smith-Hayes"
email = "me@davesmithhayes.com"
[site]
description = "Personal website of Dave Smith-Hayes - a father, developer, and musician."
url = "https://davesmithhayes.com"
repo = "https://git.davesmithhayes.com/dsh/blog"
language = "en"
copyright = "All rights reserved, Dave Smith-Hayes"

View File

@ -5,11 +5,13 @@
},
"dependencies": {
"@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.1",
"yaml-front-matter": "^4.1.1"
},
"devDependencies": {

View File

@ -1,3 +1,4 @@
export const POST_PATH: string = __dirname + '/../posts';
export const CONFIG_PATH: string = __dirname + '/../config';
export const STATIC_PATH: string = __dirname + '/assets';
export const POST_ROUTE_PREFIX: string = '/posts'

21
src/handlers/feed.ts Normal file
View File

@ -0,0 +1,21 @@
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) {
if (!feedBuffer) {
const postService: PostService = c.get('postService');
const feed: Feed = await getFeed(postService);
feedBuffer = feed.rss2();
}
c.header('Content-Type', 'text/xml');
return c.body(feedBuffer);
}
feed.get('/rss.xml', getFeedFile);
export default feed;

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

@ -1,3 +1,6 @@
/**
* SiteMeta is often used for the `<meta />` tags in the document header
*/
export type SiteMeta = {
description?: string,
tags?: string[],

View File

@ -0,0 +1,9 @@
import { describe, expect, test } from 'bun:test'
import { getSiteConfig } from '@blog/services/config';
describe("Test the global config for the site", () => {
test("Parses the Owner", async () => {
const siteConfig = await getSiteConfig();
expect(siteConfig.owner.email).toBe("me@davesmithhayes.com");
})
})

24
src/services/config.ts Normal file
View File

@ -0,0 +1,24 @@
import TOML from 'smol-toml';
import { CONFIG_PATH } from '@blog/config';
export type SiteConfig = {
title: string;
owner: {
name: string;
email: string;
};
site: {
description: string;
url: string;
repo: string;
language: string;
copyright: string;
}
}
export async function getSiteConfig(): Promise<SiteConfig> {
const siteConfig = CONFIG_PATH + '/site.toml';
const file = Bun.file(siteConfig);
const data = await file.text();
return TOML.parse(data) as SiteConfig;
}

View File

@ -0,0 +1,41 @@
import { Feed, FeedOptions } from 'feed';
import { getSiteConfig } from '@blog/services/config';
import { PostService } from '@blog/services/post-file';
import type { Post } from '@blog/models/post';
const updatedDate = new Date();
export async function getFeed(postService: PostService): Feed {
const config = await getSiteConfig();
const feed = new Feed({
title: config.title,
description: config.site.description,
link: config.site.link,
language: config.site.language,
copyright: config.site.copyright,
generator: "dsh feed gen",
updated: updatedDate,
author: {
name: "Dave Smith-Hayes",
email: "me@davesmithhayes.com",
link: "davesmithhayes.com"
}
});
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: {
name: config.author,
email: config.email
}
});
});
return feed;
};