From 186c1d1f37320967d25e0d96c19d39f958595793 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/[...slug].tsx | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/website/pages/[...slug].tsx b/website/pages/[...slug].tsx index eab8e2a71..473c702d8 100644 --- a/website/pages/[...slug].tsx +++ b/website/pages/[...slug].tsx @@ -39,7 +39,10 @@ export const getStaticPaths: GetStaticPaths = async () => { return { params: { - slug: [...pathBase, dirent.name.replace('.mdx', '')], + slug: + dirent.name === 'index.mdx' + ? pathBase + : [...pathBase, dirent.name.replace('.mdx', '')], }, } }) @@ -50,16 +53,22 @@ export const getStaticPaths: GetStaticPaths = async () => { } } -const getPathFull = (slug: ReadonlyArray): ReadonlyArray => slug +const getPathWithExtension = (slug: ReadonlyArray) => `${path.join(...slug)}.mdx` + +const getIsIndex = (slug: ReadonlyArray): boolean => + fs.existsSync(getPathWithExtension(slug)) !== true + +const getPathFull = (slug: ReadonlyArray): ReadonlyArray => + getIsIndex(slug) ? [...slug, 'index'] : slug export const getStaticProps: GetStaticProps = async (args) => { if (!args.params) { return { notFound: true } } - const pathFull = getPathFull(args.params.slug) + const pathFull = getPathFull(['docs', ...args.params.slug]) - const mdx = await serialize(fs.readFileSync(`${path.join('docs', ...pathFull)}.mdx`, 'utf-8'), { + const mdx = await serialize(fs.readFileSync(getPathWithExtension(pathFull), 'utf-8'), { parseFrontmatter: true, mdxOptions: { remarkPlugins,