aboutsummaryrefslogtreecommitdiff
path: root/src/pages/Post.tsx
blob: b2b1d5b99057dbf7a743c5423632f6a2b4f3c12e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { useParams, Navigate } from "react-router-dom";
import type { FC } from "react";

interface Meta {
  title: string;
  date: string;
  cover?: string;
}

interface Post {
  attributes: Meta;
  ReactComponent: FC;
}

const posts = import.meta.glob<Post>("../blog/*.md", { eager: true });

const formDate = new Intl.DateTimeFormat("de-DE", { dateStyle: "medium" });

export default function Post() {
  const { slug } = useParams();
  
  const post = posts[`../blog/${slug}.md`];

  if (!post) return <Navigate to="/404" replace />;

  const { attributes: meta, ReactComponent: Content } = post;

  return (
    <article>
      <a href="/blog" className="no-underline hover:underline">
        ← Back
      </a>

      {meta.cover && (
        <img
          src={meta.cover}
          alt={meta.title}
          className="w-full h-60 object-cover rounded-lg my-6"
        />
      )}

      <h1>{meta.title}</h1>
      <p className="text-sm text-zinc-500 mb-8">{formDate.format(new Date(meta.date))}</p>
      <Content />
    </article>
  );
}