From 0aa24eaeed196426c4be945e5d42844f832a3442 Mon Sep 17 00:00:00 2001 From: SoonIter Date: Fri, 20 Dec 2024 11:17:34 +0800 Subject: [PATCH] chore: update --- packages/plugin-auto-nav-sidebar/src/utils.ts | 44 ++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/packages/plugin-auto-nav-sidebar/src/utils.ts b/packages/plugin-auto-nav-sidebar/src/utils.ts index 59eae1377..b244565c5 100644 --- a/packages/plugin-auto-nav-sidebar/src/utils.ts +++ b/packages/plugin-auto-nav-sidebar/src/utils.ts @@ -4,6 +4,25 @@ import type { NavItem, Sidebar } from '@rspress/shared'; import { logger } from '@rspress/shared/logger'; import { loadFrontMatter } from '@rspress/shared/node-utils'; +/** + * + * @param rawPathWithExtension /usr/rspress-demo/docs/api.md + * @param extensions e.g: [".md"] + */ +export async function detectFilePathWithExtension( + rawPathWithExtension: string, +): Promise { + const exist = await fs.pathExists(rawPathWithExtension); + if (!exist) { + return undefined; + } + const stat = await fs.stat(rawPathWithExtension); + if (stat.isFile()) { + return rawPathWithExtension; + } + return undefined; +} + /** * * @param rawPath e.g: /usr/rspress-demo/docs/api.md or /usr/rspress-demo/docs/api @@ -14,19 +33,22 @@ export async function detectFilePath( rawPath: string, extensions: string[], ): Promise { - const pathWithExtension = extensions.map(ext => `${rawPath}${ext}`); - const pathExistInfo = await Promise.all( - pathWithExtension.map(p => fs.pathExists(p)), - ); - const findPath = pathWithExtension.find((_, i) => pathExistInfo[i]); - - if (!findPath) { - const stat = await fs.stat(rawPath); - if (stat.isFile()) { - return rawPath; + // 1. rawPath: /usr/rspress-demo/docs/api.md + const realPath = await detectFilePathWithExtension(rawPath); + if (realPath) { + const ext = path.extname(realPath); + if (extensions.includes(ext)) { + return realPath; } - return undefined; } + + // 2. rawPath: /usr/rspress-demo/docs/api + // The params doesn't have extension name, so we need to try to find the file with the extension name. + const pathWithExtension = extensions.map(ext => `${rawPath}${ext}`); + const realPaths = await Promise.all( + pathWithExtension.map(p => detectFilePathWithExtension(p)), + ); + const findPath = pathWithExtension.find((_, i) => realPaths[i]); return findPath; }