From 3a7cb097dfcf0672204fff2a3409a1817a87e5fa Mon Sep 17 00:00:00 2001 From: schererleander Date: Wed, 25 Jun 2025 01:28:12 +0200 Subject: swap gray-matter for vite-plugin-markdown --- src/pages/Post.tsx | 74 +++++++++++++++++++++++------------------------------- 1 file changed, 32 insertions(+), 42 deletions(-) (limited to 'src/pages/Post.tsx') diff --git a/src/pages/Post.tsx b/src/pages/Post.tsx index c17873c..5c0f0ca 100644 --- a/src/pages/Post.tsx +++ b/src/pages/Post.tsx @@ -1,40 +1,36 @@ -import { useEffect, useState } from "react"; import { useParams, Link } from "react-router-dom"; -import matter from "gray-matter"; -import ReactMarkdown from "react-markdown"; import CodeSnippet from "../components/CodeSnippet"; import LinkWithIcon from "../components/LinkWithIcon"; import NotFoundPage from "./404Page"; -interface PostMeta { - title: string; - date: string; - cover?: string; +interface PostFile { + attributes: { + title: string; + date: string; + cover?: string; + }; + markdown: string; + ReactComponent: React.FC; } +const posts = import.meta.glob("../blog/*.md", { + eager: true, +}) as Record; + export default function Post() { const { slug } = useParams<{ slug: string }>(); - const [meta, setMeta] = useState(null); - const [content, setContent] = useState(""); - const [notFound, setNotFound] = useState(false); - useEffect(() => { - import(`../blog/${slug}.md?raw`) - .then((m) => { - const { data, content } = matter(m.default); - setMeta(data as PostMeta); - setContent(content); - }) - .catch(() => setNotFound(true)); - }, [slug]); + const match = Object.entries(posts).find(([path]) => + path.endsWith(`${slug}.md`) + ); - if (!meta) return null; - if (notFound) return ; + if (!match) return ; + const { attributes: meta, ReactComponent: Content } = match[1]; return (
- ← Back + ← Back {meta.cover && ( @@ -49,27 +45,21 @@ export default function Post() {

{new Date(meta.date).toLocaleDateString("de-DE")}

- -
- ; - }, - a({ href = "", children, ...props }) { - return ( - - {children} - - ); - }, - }} - > - {content} - -
+ {/* The Markdown, already a React component */} + + ); + }, + a: LinkWithIcon, + }} + />
); } \ No newline at end of file -- cgit v1.3.1