spaCy/website/pages/[...listPathPage].tsx

71 lines
2.0 KiB
TypeScript
Raw Normal View History

import type { GetStaticPaths, GetStaticProps } from 'next'
import { serialize } from 'next-mdx-remote/serialize'
import fs from 'fs'
import { MDXRemote, MDXRemoteSerializeResult } from 'next-mdx-remote'
import path from 'path'
import Layout from '../components/layout'
2022-11-13 18:31:26 +03:00
import remarkPlugins from '../plugins/index.mjs'
type PropsPage = {
mdx: MDXRemoteSerializeResult
}
const PostPage = ({ mdx: mdx }: PropsPage) => {
return (
<Layout>
<MDXRemote {...mdx} />
</Layout>
)
}
export default PostPage
type ParsedUrlQuery = {
2022-11-16 22:39:02 +03:00
listPathPage: Array<string>
}
export const getStaticPaths: GetStaticPaths<ParsedUrlQuery> = async () => {
// This function needs to be defined inside `getStaticPath` to be executed in executed in the correct context
2022-11-16 22:39:02 +03:00
const loadFolder = (pathBase: Array<string> = []): Array<{ params: ParsedUrlQuery }> =>
fs
2022-11-16 22:39:02 +03:00
.readdirSync(path.join('docs', ...pathBase), { withFileTypes: true })
.flatMap((dirent: fs.Dirent) => {
if (dirent.isDirectory()) {
2022-11-16 22:39:02 +03:00
return loadFolder([...pathBase, dirent.name])
}
if (!dirent.name.includes('.mdx')) {
return []
}
return {
params: {
2022-11-16 22:39:02 +03:00
listPathPage: [...pathBase, dirent.name.replace('.mdx', '')],
},
}
})
return {
paths: loadFolder(),
fallback: false,
}
}
export const getStaticProps: GetStaticProps<PropsPage, ParsedUrlQuery> = async (args) => {
if (!args.params) {
return { notFound: true }
}
return {
props: {
mdx: await serialize(
2022-11-16 22:39:02 +03:00
fs.readFileSync(`${path.join('docs', ...args.params.listPathPage)}.mdx`, 'utf-8'),
2022-11-13 18:31:26 +03:00
{
parseFrontmatter: true,
mdxOptions: {
remarkPlugins,
},
}
),
},
}
}