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/Blog.tsx | 38 ++++++++++++---------------- src/pages/Post.tsx | 74 +++++++++++++++++++++++------------------------------- src/vite-env.d.ts | 12 ++++++--- 3 files changed, 56 insertions(+), 68 deletions(-) (limited to 'src') diff --git a/src/pages/Blog.tsx b/src/pages/Blog.tsx index 5db87c8..fa9b609 100644 --- a/src/pages/Blog.tsx +++ b/src/pages/Blog.tsx @@ -1,5 +1,3 @@ -import { useMemo } from "react"; -import matter from "gray-matter"; import { Link } from "react-router-dom"; import CardLink from "../components/CardLink"; @@ -11,40 +9,36 @@ interface PostMeta { cover?: string; } -export default function Blog() { - const posts = useMemo(() => { - const files = import.meta.glob("../blog/*.md", { - eager: true, - query: "?raw", - import: "default", - }) as Record; +const postFiles = import.meta.glob("../blog/*.md", { eager: true }) as Record< + string, + { attributes: Omit } +>; - return Object.entries(files) - .map(([path, raw]) => { - const { data } = matter(raw); - const slug = path.split("/").pop()!.replace(".md", ""); - return { slug, ...data } as PostMeta; - }) - .sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()); - }, []); +const posts: PostMeta[] = Object.entries(postFiles) + .map(([path, mod]) => ({ + slug: path.split("/").pop()!.replace(".md", ""), + ...(mod.attributes as Omit), + })) + .sort( + (a, b) => new Date(b.date).getTime() - new Date(a.date).getTime() + ); +export default function Blog() { return (

Blog

{posts.map((post) => ( - /* 1) Link für internes Routing */ - /* 2) CardLink bekommt genau deine Prop-Namen */ ))}
); -} +} \ No newline at end of file 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 diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 913581e..0eb4024 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -1,5 +1,9 @@ /// -declare module '*.md?raw' { - const content: string; - export default content; -} +declare module "*.md" { + import { FC } from "react"; + const attributes: Record; + const markdown: string; + const ReactComponent: FC; + export { attributes, markdown, ReactComponent }; + export default ReactComponent; +} \ No newline at end of file -- cgit v1.3.1