digital-gardeners/pages/[slug].js

83 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-09-05 16:37:21 +02:00
import fs from "fs";
import matter from "gray-matter";
import { MDXRemote } from "next-mdx-remote";
import { serialize } from "next-mdx-remote/serialize";
import imageSize from "rehype-img-size";
2021-09-05 16:37:21 +02:00
import dynamic from "next/dynamic";
import Head from "next/head";
import Link from "next/link";
2021-09-05 17:09:03 +02:00
import Image from "next/image";
2021-09-05 16:37:21 +02:00
import path from "path";
2021-09-06 16:41:00 +02:00
import Layout from "../components/Layout";
import { gardenFilePath, GARDENS_PATH } from "../utils/mdxUtils";
2021-09-05 16:37:21 +02:00
// Custom components/renderers to pass to MDX.
// Since the MDX files aren't loaded by webpack, they have no knowledge of how
// to handle import statements. Instead, you must include components in scope
// here.
const components = {
img: (props) => <Image {...props} layout="responsive" loading="lazy" />,
2021-09-05 16:37:21 +02:00
// It also works with dynamically-imported components, which is especially
// useful for conditionally loading components for certain routes.
// See the notes in README.md for more details.
2021-09-05 17:09:03 +02:00
// TestComponent: dynamic(() => import("../../components/TestComponent")),
2021-09-05 16:37:21 +02:00
Head,
};
export default function GardenPage({ source, frontMatter }) {
return (
<Layout>
<header>
<nav>
<Link href="/">
<a>Go back home</a>
</Link>
</nav>
</header>
<div>
<h1>{frontMatter.title}</h1>
{frontMatter.description && <p>{frontMatter.description}</p>}
</div>
<main>
<MDXRemote {...source} components={components} />
</main>
</Layout>
);
}
export const getStaticProps = async ({ params }) => {
2021-09-05 17:09:03 +02:00
const gardenFilePath = path.join(GARDENS_PATH, `${params.slug}.mdx`);
2021-09-05 16:37:21 +02:00
const source = fs.readFileSync(gardenFilePath);
const { content, data } = matter(source);
const mdxSource = await serialize(content, {
// Optionally pass remark/rehype plugins
mdxOptions: {
remarkPlugins: [],
rehypePlugins: [[imageSize, { dir: "public" }]],
2021-09-05 16:37:21 +02:00
},
scope: data,
});
return {
props: {
source: mdxSource,
frontMatter: data,
},
};
};
export const getStaticPaths = async () => {
2021-09-05 17:09:03 +02:00
const paths = gardenFilePath
2021-09-05 16:37:21 +02:00
// Remove file extensions for page paths
2021-09-05 17:09:03 +02:00
.map((path) => path.replace(/\.mdx?$/, ""))
2021-09-05 16:37:21 +02:00
// Map the path into the static paths object required by Next.js
.map((slug) => ({ params: { slug } }));
return {
paths,
fallback: false,
};
};