From a70e461a948ed1a14d47e6ec9d0ad1af7684efbb Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Wed, 3 Jul 2024 20:56:54 -0400 Subject: [PATCH] Update the models and and add a post reader module full of helper functions --- src/index.tsx | 14 ++++------ src/model/Post.ts | 10 +++---- src/model/PostMeta.ts | 9 +++++++ src/routes/{post.tsx => posts.tsx} | 0 src/util/post-reader.ts | 42 ++++++++++++++++++++++++++++++ src/util/readPostMarkdown.ts | 22 ---------------- 6 files changed, 60 insertions(+), 37 deletions(-) create mode 100644 src/model/PostMeta.ts rename src/routes/{post.tsx => posts.tsx} (100%) create mode 100644 src/util/post-reader.ts delete mode 100644 src/util/readPostMarkdown.ts diff --git a/src/index.tsx b/src/index.tsx index 1aa8e08..33b3b7e 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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; diff --git a/src/model/Post.ts b/src/model/Post.ts index e6d3435..b373990 100644 --- a/src/model/Post.ts +++ b/src/model/Post.ts @@ -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 }; diff --git a/src/model/PostMeta.ts b/src/model/PostMeta.ts new file mode 100644 index 0000000..60f2847 --- /dev/null +++ b/src/model/PostMeta.ts @@ -0,0 +1,9 @@ +export type PostMeta = { + title: string, + slug?: string, + description?: string, + html?: string, + date: Date, + tags?: string[], + draft?: boolean +}; diff --git a/src/routes/post.tsx b/src/routes/posts.tsx similarity index 100% rename from src/routes/post.tsx rename to src/routes/posts.tsx diff --git a/src/util/post-reader.ts b/src/util/post-reader.ts new file mode 100644 index 0000000..083c3f6 --- /dev/null +++ b/src/util/post-reader.ts @@ -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 { + 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 { + 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 + }; +} + diff --git a/src/util/readPostMarkdown.ts b/src/util/readPostMarkdown.ts deleted file mode 100644 index 4459678..0000000 --- a/src/util/readPostMarkdown.ts +++ /dev/null @@ -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 { - 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 - }; -} -