Allow to parse subfolders for pages

This commit is contained in:
Marcus Blättermann 2022-11-16 20:39:02 +01:00
parent 7c31de6c34
commit 6fa2bf2fff
No known key found for this signature in database
GPG Key ID: A1E1F04008AC450D

View File

@ -21,17 +21,17 @@ const PostPage = ({ mdx: mdx }: PropsPage) => {
export default PostPage export default PostPage
type ParsedUrlQuery = { type ParsedUrlQuery = {
slug: string listPathPage: Array<string>
} }
export const getStaticPaths: GetStaticPaths<ParsedUrlQuery> = async () => { export const getStaticPaths: GetStaticPaths<ParsedUrlQuery> = async () => {
// This function needs to be defined inside `getStaticPath` to be executed in executed in the correct context // This function needs to be defined inside `getStaticPath` to be executed in executed in the correct context
const loadFolder = (): Array<{ params: ParsedUrlQuery }> => const loadFolder = (pathBase: Array<string> = []): Array<{ params: ParsedUrlQuery }> =>
fs fs
.readdirSync(path.join('docs'), { withFileTypes: true }) .readdirSync(path.join('docs', ...pathBase), { withFileTypes: true })
.flatMap((dirent: fs.Dirent) => { .flatMap((dirent: fs.Dirent) => {
if (dirent.isDirectory()) { if (dirent.isDirectory()) {
return [] return loadFolder([...pathBase, dirent.name])
} }
if (!dirent.name.includes('.mdx')) { if (!dirent.name.includes('.mdx')) {
return [] return []
@ -39,7 +39,7 @@ export const getStaticPaths: GetStaticPaths<ParsedUrlQuery> = async () => {
return { return {
params: { params: {
slug: dirent.name.replace('.mdx', ''), listPathPage: [...pathBase, dirent.name.replace('.mdx', '')],
}, },
} }
}) })
@ -56,9 +56,8 @@ export const getStaticProps: GetStaticProps<PropsPage, ParsedUrlQuery> = async (
} }
return { return {
props: { props: {
slug: args.params.slug,
mdx: await serialize( mdx: await serialize(
fs.readFileSync(`${path.join('docs', args.params.slug)}.mdx`, 'utf-8'), fs.readFileSync(`${path.join('docs', ...args.params.listPathPage)}.mdx`, 'utf-8'),
{ {
parseFrontmatter: true, parseFrontmatter: true,
mdxOptions: { mdxOptions: {