1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-10-02 06:59:09 +00:00
conky/web/pages/documents/[slug].tsx

110 lines
2.8 KiB
TypeScript
Raw Normal View History

import {
getNextDocumentBySlug,
getDocumentBySlug,
getPreviousDocumentBySlug,
documentFilePaths,
} from '../../utils/mdx-utils'
import Layout from '../../components/Layout'
import SEO from '../../components/SEO'
import { GetStaticProps } from 'next'
2022-10-01 20:32:01 +00:00
import { getSearchIndex, SearchIndex } from '../../utils/search'
import Link from 'next/link'
2024-02-22 23:44:00 +00:00
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'
2024-02-22 23:44:00 +00:00
// @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 {
2024-02-22 23:44:00 +00:00
source: string
frontMatter: FrontMatter
2022-10-01 20:32:01 +00:00
searchIndex: SearchIndex
}
export default function DocumentPage({
source,
frontMatter,
2022-10-01 20:32:01 +00:00
searchIndex,
}: DocumentPageProps) {
2024-02-22 23:44:00 +00:00
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 (
2022-10-01 20:32:01 +00:00
<Layout searchIndex={searchIndex}>
<SEO
title={`${frontMatter.title}`}
description={frontMatter.description}
/>
<article className="px-6 md:px-0">
<header>
<h1 className="text-3xl md:text-5xl dark:text-white mb-12">
{frontMatter.title}
</h1>
{frontMatter.description && (
<p className="text-xl mb-4">{frontMatter.description}</p>
)}
</header>
<main>
2024-02-22 23:44:00 +00:00
<article className="prose dark:prose-invert">{children}</article>
</main>
</article>
</Layout>
)
}
export const getStaticProps: GetStaticProps = async ({ params }) => {
if (params) {
2024-02-22 23:44:00 +00:00
const { source, data } = await getDocumentBySlug(params.slug as string)
const prevDocument = getPreviousDocumentBySlug(params.slug as string)
const nextDocument = getNextDocumentBySlug(params.slug as string)
2022-10-01 20:32:01 +00:00
const searchIndex = getSearchIndex()
return {
props: {
2022-10-01 20:32:01 +00:00
searchIndex,
2024-02-22 23:44:00 +00:00
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,
}
}