Test for both md and mdx

This commit is contained in:
Terry Sutton authored and Joshen Lim committed 2022-10-26 10:16:33 +07:00
1 parent 006656b8aa
commit ead80901a0
1 file changed
+16 -1
+16 -1
View File
@@ -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')