From ead80901a0d93106a48a582a822aaa18f7eefd86 Mon Sep 17 00:00:00 2001 From: Terry Sutton Date: Tue, 25 Oct 2022 11:38:22 -0230 Subject: [PATCH] Test for both md and mdx --- apps/temp-docs/lib/docs.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/apps/temp-docs/lib/docs.ts b/apps/temp-docs/lib/docs.ts index 05c2fa79f4c..5f048630b4d 100644 --- a/apps/temp-docs/lib/docs.ts +++ b/apps/temp-docs/lib/docs.ts @@ -5,9 +5,24 @@ import matter from 'gray-matter' const docsDirectory = process.cwd() export function getDocsBySlug(slug: string) { + console.log('getDocsBySlug', { slug }) const realSlug = slug.replace(/\.mdx$/, '') - let fullPath = join(docsDirectory, `${realSlug}.mdx`) + // files can either be .md or .mdx + // we need to check which one exists + let fullPath + let fullPathMD = join(docsDirectory, `${realSlug}.md`) + let fullPathMDX = join(docsDirectory, `${realSlug}.mdx`) + + if (fs.existsSync(fullPathMD)) { + fullPath = fullPathMD + } + + if (fs.existsSync(fullPathMDX)) { + fullPath = fullPathMDX + } + + // if no match, 404 if (!fs.existsSync(fullPath)) { console.log(`\nfile ${fullPath} not found, redirect to 404\n`) fullPath = join(docsDirectory, 'docs/404.mdx')