import { getNextDocumentBySlug, getDocumentBySlug, getPreviousDocumentBySlug, documentFilePaths, } from '../../utils/mdx-utils' import Layout from '../../components/Layout' import SEO from '../../components/SEO' import { GetStaticProps } from 'next' import Link from 'next/link' import { unified } from 'unified' import rehypeReact from 'rehype-react' import * as prod from 'react/jsx-runtime' import rehypeParse from 'rehype-parse' import { createElement, Fragment, useEffect, useState } from 'react' // @ts-expect-error: the react types are missing. const production = { Fragment: prod.Fragment, jsx: prod.jsx, jsxs: prod.jsxs } interface FrontMatter { title: string description: string } interface DocumentPageProps { source: string frontMatter: FrontMatter } export default function DocumentPage({ source, frontMatter, }: DocumentPageProps) { const [children, setChildren] = useState(createElement(Fragment)) useEffect( function () { ;(async function () { const file = await unified() .use(rehypeParse, { fragment: true }) // @ts-expect-error: the react types are missing. .use(rehypeReact, { ...production, components: { a: Link } }) .process(source) setChildren(file.result) })() }, [source] ) return (

{frontMatter.title}

{frontMatter.description && (

{frontMatter.description}

)}
{children}
) } export const getStaticProps: GetStaticProps = async ({ params }) => { if (params) { const { source, data } = await getDocumentBySlug(params.slug as string) const prevDocument = getPreviousDocumentBySlug(params.slug as string) const nextDocument = getNextDocumentBySlug(params.slug as string) return { props: { source, frontMatter: data, prevDocument, nextDocument, }, } } return { props: {} } } export const getStaticPaths = async () => { const paths = documentFilePaths // Remove file extensions for page paths .map((path) => path.replace(/\.mdx?$/, '')) // Map the path into the static paths object required by Next.js .map((slug) => ({ params: { slug } })) return { paths, fallback: false, } }