Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter committed Dec 20, 2024
1 parent cde1cb3 commit 0aa24ea
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions packages/plugin-auto-nav-sidebar/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined> {
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
Expand All @@ -14,19 +33,22 @@ export async function detectFilePath(
rawPath: string,
extensions: string[],
): Promise<string | undefined> {
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;
}

Expand Down

0 comments on commit 0aa24ea

Please sign in to comment.