Skip to content

Commit

Permalink
Stage 7: Setup post page
Browse files Browse the repository at this point in the history
  • Loading branch information
Jihillestad committed Jul 24, 2024
1 parent 90f3617 commit 177f6b8
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
41 changes: 41 additions & 0 deletions app/blog/[...slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { posts } from "#site/content";
import { MDXContent } from "@/components/mdx-components";
import { notFound } from "next/navigation";

interface PostPageProps {
params: {
slug: string[];
};
}

async function getPostFromParams(params: PostPageProps["params"]) {
const slug = params?.slug?.join("/");
const post = posts.find((post) => post.slugAsParams === slug);

return post;
}

export async function generateStaticParams(): Promise<
PostPageProps["params"][]
> {
return posts.map((post) => ({ slug: post.slugAsParams.split("/") }));
}

export default async function PostPage({ params }: PostPageProps) {
const post = await getPostFromParams(params);

if (!post || !post.published) {
notFound();
}

return (
<article className="container py-6 prose dark:prose-invert max-w-3xl mx-auto">
<h1 className="mb 2">{post.title}</h1>
{post.description ? (
<p className="text-xl mt-0 text-muted-foreground">{post.description}</p>
) : null}
<hr className="my-4" />
<MDXContent code={post.body} />
</article>
);
}
20 changes: 20 additions & 0 deletions components/mdx-components.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Image from "next/image";
import * as runtime from "react/jsx-runtime";

const useMDXComponent = (code: string) => {
const fn = new Function(code);
return fn({ ...runtime }).default;
};

const components = {
Image,
};

interface MdxProps {
code: string;
}

export function MDXContent({ code }: MdxProps) {
const Component = useMDXComponent(code);
return <Component components={components} />;
}
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"tailwindcss-animate": "^1.0.7"
},
"devDependencies": {
"@tailwindcss/typography": "^0.5.13",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
Expand Down
3 changes: 1 addition & 2 deletions tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ const config = {
},
},
},
plugins: [require("tailwindcss-animate")],
plugins: [require("tailwindcss-animate"), require("@tailwindcss/typography")],
} satisfies Config;

export default config;

0 comments on commit 177f6b8

Please sign in to comment.