Redirect index.mdx to parent directory

This commit is contained in:
Marcus Blättermann 2022-11-16 20:39:38 +01:00
parent dcf8455f77
commit 978045b36b
No known key found for this signature in database
GPG Key ID: A1E1F04008AC450D

View File

@ -39,7 +39,10 @@ export const getStaticPaths: GetStaticPaths<ParsedUrlQuery> = async () => {
return { return {
params: { 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<ParsedUrlQuery> = async () => {
} }
} }
const getPathFileWithExtension = (listPathFile: ReadonlyArray<string>) =>
`${path.join(...listPathFile)}.mdx`
export const getStaticProps: GetStaticProps<PropsPage, ParsedUrlQuery> = async (args) => { export const getStaticProps: GetStaticProps<PropsPage, ParsedUrlQuery> = async (args) => {
if (!args.params) { if (!args.params) {
return { notFound: true } return { notFound: true }
} }
const mdx = await serialize( const listPathFile = ['docs', ...args.params.listPathPage]
fs.readFileSync(`${path.join('docs', ...args.params.listPathPage)}.mdx`, 'utf-8'), 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, parseFrontmatter: true,
mdxOptions: { mdxOptions: {
remarkPlugins, remarkPlugins,
}, },
} })
)
return { return {
props: { props: {