From 76066ddae18e3f4fe68aa18b44d5c55d9f31a091 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Wed, 3 Jul 2024 21:30:11 -0400 Subject: [PATCH] Create meta tags components --- src/model/SiteMeta.ts | 3 +- src/templates/Page.tsx | 4 +-- src/templates/components/MetaTags.tsx | 34 +++++++++++++++++++++++ src/util/post-reader.ts | 40 +++++++++++++-------------- 4 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 src/templates/components/MetaTags.tsx diff --git a/src/model/SiteMeta.ts b/src/model/SiteMeta.ts index 0c25bd6..1228754 100644 --- a/src/model/SiteMeta.ts +++ b/src/model/SiteMeta.ts @@ -1,4 +1,5 @@ export type SiteMeta = { description: string, - tags: string[] + tags: string[], + author?: string, }; diff --git a/src/templates/Page.tsx b/src/templates/Page.tsx index 9031e86..ffb0404 100644 --- a/src/templates/Page.tsx +++ b/src/templates/Page.tsx @@ -1,11 +1,11 @@ import { css, Style } from 'hono/css'; -improt { SiteMeta } from '@blog/model/SiteMeta'; +import { SiteMeta } from '@blog/model/SiteMeta'; const logoClass = css` font-size: 16pt; `; -export function Page({ children, meta }: { children: any, meta: Record }) { +export function Page({ children, meta }: { children: any, meta: SiteMeta }) { return ( diff --git a/src/templates/components/MetaTags.tsx b/src/templates/components/MetaTags.tsx new file mode 100644 index 0000000..8488524 --- /dev/null +++ b/src/templates/components/MetaTags.tsx @@ -0,0 +1,34 @@ +import { SiteMeta } from '@blog/model/SiteMeta'; + +export function Description({ meta }: { meta: SiteMeta }) { + if (!meta.description) { + return null; + } + + return (); +} + +export function Tags({ meta }: { meta: SiteMeta }) { + if (!meta.tags) { + return null; + } + + return (); +} + +export function Author({ meta }: { meta: SiteMeta }) { + if (!meta.author) { + return null; + } + + return (); +} + +export function MetaTags(meta: SiteMeta) { + return ( + <> + + + + ) +} diff --git a/src/util/post-reader.ts b/src/util/post-reader.ts index 083c3f6..e9ea633 100644 --- a/src/util/post-reader.ts +++ b/src/util/post-reader.ts @@ -8,34 +8,34 @@ export async function openPostMarkdownFile(filename: string): Promise { return file.text(); } -export function parsePostMetadata(post: string): PostMeta { +type PostMetaWithRawContent = { + meta: PostMeta, + content: string +}; + +export function parsePostMetadata(post: string): PostMetaWithRawContent { 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, + draft: parsedData?.draft || false, + tags: parsedData.tags }, - content: parsedData.__content, + content: parsedData.__content + }; +} + +export async function readPostMarkdown(filename: string): Promise { + const contents = await openPostMarkdownFile(filename); + const post = parsePostMetadata(contents); + const parsedPost = await marked.parse(post.content); + + return { + meta: post.meta, + content: post.content, html: parsedPost }; }