Update the models and and add a post reader module full of helper functions

This commit is contained in:
Dave Smith-Hayes 2024-07-03 20:56:54 -04:00
parent 79a0bf0f18
commit a70e461a94
6 changed files with 60 additions and 37 deletions

View File

@ -1,11 +1,12 @@
import { Hono } from 'hono';
import { jsxRenderer, useRequestContext } from 'hono/jsx-renderer';
import { Page } from '@blog/templates/Page';
import { renderHomePage } from '@blog/routes/home';
import { renderPostPage } from '@blog/routes/post';
import home from '@blog/routes/home';
import posts from '@blog/routes/post';
const app = new Hono();
// Render the JSX views
app.get(
'*',
jsxRenderer(
@ -21,12 +22,7 @@ app.get(
// read all posts
// create listing of posts
app.get('/', async (c) => {
return renderHomePage(c);
});
app.get('/posts/:slug', async (c) => {
return renderPostPage(c);
});
app.route('/', home);
app.route('/posts', posts);
export default app;

View File

@ -1,9 +1,7 @@
import { PostMeta } from '@blog/model/PostMeta';
export type Post = {
title: string,
slug: string,
description?: string,
meta: PostMeta,
content: string,
html?: string,
date: Date,
tags?: string[]
html?: string
};

9
src/model/PostMeta.ts Normal file
View File

@ -0,0 +1,9 @@
export type PostMeta = {
title: string,
slug?: string,
description?: string,
html?: string,
date: Date,
tags?: string[],
draft?: boolean
};

42
src/util/post-reader.ts Normal file
View File

@ -0,0 +1,42 @@
import { marked } from 'marked';
import type { Post } from '@blog/model/Post';
import type { PostMeta } from '@blog/model/PostMeta';
import * as yamlFront from 'yaml-front-matter';
export async function openPostMarkdownFile(filename: string): Promise<string> {
const file = Bun.file(filename);
return file.text();
}
export function parsePostMetadata(post: string): PostMeta {
const parsedData = yamlFront.loadFront(post);
return {
title: parsedData.title,
description: parsedData.description,
date: new Date(parsedData.date),
draft: parsedData?.draft || false,
tags: parsedData.tags
};
}
export async function readPostMarkdown(filename: string): Promise<Post> {
const file = Bun.file(filename);
const contents = await file.text();
const parsedData = yamlFront.loadFront(contents);
const parsedPost = await marked.parse(parsedData.__content);
return {
meta: {
title: parsedData.title,
slug: filename.slice(0, -3),
description: parsedData.description,
date: new Date(parsedData.date),
tags: parsedData.tags,
},
content: parsedData.__content,
html: parsedPost
};
}

View File

@ -1,22 +0,0 @@
import { marked } from 'marked';
import type { Post } from '@blog/Model/Post';
import * as yamlFront from 'yaml-front-matter';
export async function readPostMarkdown(filename: string): Promise<Post> {
const file = Bun.file(filename);
const contents = await file.text();
const parsedData = yamlFront.loadFront(contents);
const parsedPost = await marked.parse(parsedData.__content);
return {
title: parsedData.title,
slug: filename.slice(0, -3),
description: parsedData.description,
date: new Date(parsedData.date),
tags: parsedData.tags,
content: parsedData.__content,
html: parsedPost
};
}