From 448c7dad83c816cd5ecbc07399e56f2a33191ee5 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Wed, 3 Jul 2024 22:13:04 -0400 Subject: [PATCH] Add the meta to the context renderer --- src/index.tsx | 13 ++++++++++--- src/routes/home.tsx | 2 +- src/routes/posts.tsx | 6 +++--- src/templates/components/MetaTags.tsx | 12 ++++++------ 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 33b3b7e..7436dbd 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -2,7 +2,14 @@ import { Hono } from 'hono'; import { jsxRenderer, useRequestContext } from 'hono/jsx-renderer'; import { Page } from '@blog/templates/Page'; import home from '@blog/routes/home'; -import posts from '@blog/routes/post'; +import posts from '@blog/routes/posts'; +import type { SiteMeta } from '@blog/model/SiteMeta'; + +declare module 'hono' { + interface ContextRenderer { + (content: string|Promise, props: { meta: SiteMeta }): Response; + } +} const app = new Hono(); @@ -10,8 +17,8 @@ const app = new Hono(); app.get( '*', jsxRenderer( - ({ children }) => { - return ({children}); + ({ children, meta }) => { + return ({children}); }, { docType: true diff --git a/src/routes/home.tsx b/src/routes/home.tsx index 0334bf3..d0adf2c 100644 --- a/src/routes/home.tsx +++ b/src/routes/home.tsx @@ -5,7 +5,7 @@ import { Home } from '@blog/templates/Pages/Home'; const app = new Hono(); app.get('/', async (c) => { - const files: string[] = await readdir(__dirname + '/../posts', { recursive: true }); + const files: string[] = await readdir(__dirname + '/../../posts', { recursive: true }); const posts: string[] = files.map(f => f.slice(0, -3)); return c.render(); }); diff --git a/src/routes/posts.tsx b/src/routes/posts.tsx index 33c2c5a..c14d1d2 100644 --- a/src/routes/posts.tsx +++ b/src/routes/posts.tsx @@ -7,10 +7,10 @@ import { readPostMarkdown } from '@blog/util/post-reader'; const app = new Hono(); -app.get('/posts/:slug', async (c) => { +app.get('/:slug', async (c) => { const postSlug: string = c.req.param("slug"); const fileName: string = postSlug + '.md'; - const files = await readdir(__dirname + "/../posts"); + const files = await readdir(__dirname + "/../../posts"); const postFile = files.find(f => f === fileName); if (!postFile) { @@ -18,7 +18,7 @@ app.get('/posts/:slug', async (c) => { return c.render(); } - const post = await readPostMarkdown(postFile); + const post = await readPostMarkdown(__dirname + "/../../posts/" + postFile); const meta = { description: post.meta.description, tags: post.meta.tags, diff --git a/src/templates/components/MetaTags.tsx b/src/templates/components/MetaTags.tsx index 36ff03e..9316307 100644 --- a/src/templates/components/MetaTags.tsx +++ b/src/templates/components/MetaTags.tsx @@ -1,24 +1,24 @@ import { SiteMeta } from '@blog/model/SiteMeta'; export function Description({ meta }: { meta: SiteMeta }) { - if (!meta.description) { - return null; + if (!meta?.description) { + return (<>); } return (); } export function Tags({ meta }: { meta: SiteMeta }) { - if (!meta.tags) { - return null; + if (!meta?.tags) { + return (<>); } return (); } export function Author({ meta }: { meta: SiteMeta }) { - if (!meta.author) { - return null; + if (!meta?.author) { + return (<>); } return ();