From 978045b36b17e2042143413a935f66906aa5dff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20Bl=C3=A4ttermann?= Date: Wed, 16 Nov 2022 20:39:38 +0100 Subject: [PATCH] Redirect `index.mdx` to parent directory --- website/pages/[...listPathPage].tsx | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/website/pages/[...listPathPage].tsx b/website/pages/[...listPathPage].tsx index af59e6946..eaccf185f 100644 --- a/website/pages/[...listPathPage].tsx +++ b/website/pages/[...listPathPage].tsx @@ -39,7 +39,10 @@ export const getStaticPaths: GetStaticPaths = async () => { return { params: { - listPathPage: [...pathBase, dirent.name.replace('.mdx', '')], + listPathPage: + dirent.name === 'index.mdx' + ? pathBase + : [...pathBase, dirent.name.replace('.mdx', '')], }, } }) @@ -50,20 +53,25 @@ export const getStaticPaths: GetStaticPaths = async () => { } } +const getPathFileWithExtension = (listPathFile: ReadonlyArray) => + `${path.join(...listPathFile)}.mdx` + export const getStaticProps: GetStaticProps = async (args) => { if (!args.params) { return { notFound: true } } - const mdx = await serialize( - fs.readFileSync(`${path.join('docs', ...args.params.listPathPage)}.mdx`, 'utf-8'), - { - parseFrontmatter: true, - mdxOptions: { - remarkPlugins, - }, - } - ) + const listPathFile = ['docs', ...args.params.listPathPage] + const isIndex = fs.existsSync(getPathFileWithExtension(listPathFile)) !== true + const listPathFileWithIndex = isIndex ? [...listPathFile, 'index'] : listPathFile + const listPathFileWithIndexAndExtension = getPathFileWithExtension(listPathFileWithIndex) + + const mdx = await serialize(fs.readFileSync(listPathFileWithIndexAndExtension, 'utf-8'), { + parseFrontmatter: true, + mdxOptions: { + remarkPlugins, + }, + }) return { props: {