From 91b8c267e6a2664917c8cd0ad567271f428519cb Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Tue, 2 Jul 2024 22:52:56 -0400 Subject: [PATCH] Start fleshing out the route of reading a post. --- src/index.tsx | 11 ++++++++++- src/templates/Pages/FourOhFour.tsx | 10 ++++++++++ src/{ => util}/readPostMarkdown.ts | 0 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/templates/Pages/FourOhFour.tsx rename src/{ => util}/readPostMarkdown.ts (100%) diff --git a/src/index.tsx b/src/index.tsx index f1de0e3..4164123 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -2,6 +2,7 @@ import { Hono } from 'hono'; import { jsxRenderer, useRequestContext } from 'hono/jsx-renderer'; import { Page } from '@blog/templates/Page'; import { Home } from '@blog/templates/Pages/Home'; +import { FourOhFour } from '@blog/templates/Pages/FourOhFour'; import { readdir } from 'node:fs/promises'; const app = new Hono(); @@ -28,8 +29,16 @@ app.get('/', async (c) => { return c.render(); }); -app.get('/posts/:slug', (c) => { +app.get('/posts/:slug', async (c) => { const postSlug: string = c.req.param("slug"); + const fileName: string = postSlug + '.md'; + const files = await readdir(__dirname + "/../posts"); + + const postFile = files.find(f => f === fileName); + if (!postFile) { + c.status(404); + return c.render(); + } // render post // send to Post layout diff --git a/src/templates/Pages/FourOhFour.tsx b/src/templates/Pages/FourOhFour.tsx new file mode 100644 index 0000000..b4f2027 --- /dev/null +++ b/src/templates/Pages/FourOhFour.tsx @@ -0,0 +1,10 @@ +import { Page } from '@blog/templates/Page'; + +export function FourOhFour() { + return ( + <> +

404 Error

+
Unable to find what you're looking for.
+ + ); +} diff --git a/src/readPostMarkdown.ts b/src/util/readPostMarkdown.ts similarity index 100% rename from src/readPostMarkdown.ts rename to src/util/readPostMarkdown.ts