2022-11-10 04:40:32 +03:00
|
|
|
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'
|
2022-11-10 04:40:32 +03:00
|
|
|
|
|
|
|
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>
|
2022-11-10 04:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getStaticPaths: GetStaticPaths<ParsedUrlQuery> = async () => {
|
2022-11-16 20:18:04 +03:00
|
|
|
// 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 }> =>
|
2022-11-16 20:18:04 +03:00
|
|
|
fs
|
2022-11-16 22:39:02 +03:00
|
|
|
.readdirSync(path.join('docs', ...pathBase), { withFileTypes: true })
|
2022-11-16 22:32:16 +03:00
|
|
|
.flatMap((dirent: fs.Dirent) => {
|
|
|
|
if (dirent.isDirectory()) {
|
2022-11-16 22:39:02 +03:00
|
|
|
return loadFolder([...pathBase, dirent.name])
|
2022-11-16 22:32:16 +03:00
|
|
|
}
|
|
|
|
if (!dirent.name.includes('.mdx')) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
params: {
|
2022-11-16 22:39:02 +03:00
|
|
|
listPathPage: [...pathBase, dirent.name.replace('.mdx', '')],
|
2022-11-16 22:32:16 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
2022-11-16 20:18:04 +03:00
|
|
|
|
2022-11-10 04:40:32 +03:00
|
|
|
return {
|
2022-11-16 20:18:04 +03:00
|
|
|
paths: loadFolder(),
|
2022-11-10 04:40:32 +03:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
2022-11-10 04:40:32 +03:00
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|