From 997cf4d5cb5f033a7d4f687627d64b3d9193e376 Mon Sep 17 00:00:00 2001 From: SoonIter Date: Fri, 20 Dec 2024 02:45:14 +0800 Subject: [PATCH] fix(plugin-auto-nav-sidebar): dir name contain dot --- .../013.second-sub-directory/index.md | 1 + .../013.second-sub-directory/test.md | 0 .../docs/010.sub-directory/index.md | 1 + .../docs/_meta.json | 7 ++++ .../auto-nav-sidebar-issue-1682/docs/index.md | 13 +++++++ .../auto-nav-sidebar-issue-1682/package.json | 16 +++++++++ .../rspress.config.ts | 7 ++++ .../auto-nav-sidebar-issue-1682/tsconfig.json | 1 + e2e/tests/auto-nav-sidebar-issue-1682.test.ts | 36 +++++++++++++++++++ packages/plugin-auto-nav-sidebar/src/utils.ts | 32 +++++++++-------- pnpm-lock.yaml | 10 ++++++ 11 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 e2e/fixtures/auto-nav-sidebar-issue-1682/docs/010.sub-directory/013.second-sub-directory/index.md create mode 100644 e2e/fixtures/auto-nav-sidebar-issue-1682/docs/010.sub-directory/013.second-sub-directory/test.md create mode 100644 e2e/fixtures/auto-nav-sidebar-issue-1682/docs/010.sub-directory/index.md create mode 100644 e2e/fixtures/auto-nav-sidebar-issue-1682/docs/_meta.json create mode 100644 e2e/fixtures/auto-nav-sidebar-issue-1682/docs/index.md create mode 100644 e2e/fixtures/auto-nav-sidebar-issue-1682/package.json create mode 100644 e2e/fixtures/auto-nav-sidebar-issue-1682/rspress.config.ts create mode 100644 e2e/fixtures/auto-nav-sidebar-issue-1682/tsconfig.json create mode 100644 e2e/tests/auto-nav-sidebar-issue-1682.test.ts diff --git a/e2e/fixtures/auto-nav-sidebar-issue-1682/docs/010.sub-directory/013.second-sub-directory/index.md b/e2e/fixtures/auto-nav-sidebar-issue-1682/docs/010.sub-directory/013.second-sub-directory/index.md new file mode 100644 index 000000000..85a8377f5 --- /dev/null +++ b/e2e/fixtures/auto-nav-sidebar-issue-1682/docs/010.sub-directory/013.second-sub-directory/index.md @@ -0,0 +1 @@ +# Second sub-directory diff --git a/e2e/fixtures/auto-nav-sidebar-issue-1682/docs/010.sub-directory/013.second-sub-directory/test.md b/e2e/fixtures/auto-nav-sidebar-issue-1682/docs/010.sub-directory/013.second-sub-directory/test.md new file mode 100644 index 000000000..e69de29bb diff --git a/e2e/fixtures/auto-nav-sidebar-issue-1682/docs/010.sub-directory/index.md b/e2e/fixtures/auto-nav-sidebar-issue-1682/docs/010.sub-directory/index.md new file mode 100644 index 000000000..98e209ecc --- /dev/null +++ b/e2e/fixtures/auto-nav-sidebar-issue-1682/docs/010.sub-directory/index.md @@ -0,0 +1 @@ +# First sub-directory \ No newline at end of file diff --git a/e2e/fixtures/auto-nav-sidebar-issue-1682/docs/_meta.json b/e2e/fixtures/auto-nav-sidebar-issue-1682/docs/_meta.json new file mode 100644 index 000000000..d1e94cd09 --- /dev/null +++ b/e2e/fixtures/auto-nav-sidebar-issue-1682/docs/_meta.json @@ -0,0 +1,7 @@ +[ + { + "text": "010.sub-directory", + "link": "/010.sub-directory/", + "activeMatch": "/010.sub-directory/" + } +] diff --git a/e2e/fixtures/auto-nav-sidebar-issue-1682/docs/index.md b/e2e/fixtures/auto-nav-sidebar-issue-1682/docs/index.md new file mode 100644 index 000000000..a381fe55a --- /dev/null +++ b/e2e/fixtures/auto-nav-sidebar-issue-1682/docs/index.md @@ -0,0 +1,13 @@ +# Rspress trying to read directory error + +To reproduce the error, install the dependencies: + +```bash +npm install +``` + +Then run the following command: + +```bash +rspress dev +``` diff --git a/e2e/fixtures/auto-nav-sidebar-issue-1682/package.json b/e2e/fixtures/auto-nav-sidebar-issue-1682/package.json new file mode 100644 index 000000000..ad740e6aa --- /dev/null +++ b/e2e/fixtures/auto-nav-sidebar-issue-1682/package.json @@ -0,0 +1,16 @@ +{ + "name": "@rspress-fixture/issue-1682", + "version": "1.0.0", + "private": true, + "scripts": { + "dev": "rspress dev", + "build": "rspress build", + "preview": "rspress preview" + }, + "dependencies": { + "rspress": "workspace:*" + }, + "devDependencies": { + "@types/node": "^18.11.17" + } +} diff --git a/e2e/fixtures/auto-nav-sidebar-issue-1682/rspress.config.ts b/e2e/fixtures/auto-nav-sidebar-issue-1682/rspress.config.ts new file mode 100644 index 000000000..0e945ffe0 --- /dev/null +++ b/e2e/fixtures/auto-nav-sidebar-issue-1682/rspress.config.ts @@ -0,0 +1,7 @@ +import * as path from 'node:path'; +import { defineConfig } from 'rspress/config'; + +export default defineConfig({ + title: 'Rspress', + root: path.join(__dirname, 'docs'), +}); diff --git a/e2e/fixtures/auto-nav-sidebar-issue-1682/tsconfig.json b/e2e/fixtures/auto-nav-sidebar-issue-1682/tsconfig.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/e2e/fixtures/auto-nav-sidebar-issue-1682/tsconfig.json @@ -0,0 +1 @@ +{} diff --git a/e2e/tests/auto-nav-sidebar-issue-1682.test.ts b/e2e/tests/auto-nav-sidebar-issue-1682.test.ts new file mode 100644 index 000000000..a1333c571 --- /dev/null +++ b/e2e/tests/auto-nav-sidebar-issue-1682.test.ts @@ -0,0 +1,36 @@ +import { expect, test } from '@playwright/test'; +import path from 'node:path'; +import { getPort, killProcess, runDevCommand } from '../utils/runCommands'; +import { getSidebarTexts } from '../utils/getSideBar'; + +const fixtureDir = path.resolve(__dirname, '../fixtures'); + +test.describe('Auto nav and sidebar dir issue-1682', async () => { + let appPort; + let app; + test.beforeAll(async () => { + const appDir = path.join(fixtureDir, 'auto-nav-sidebar-issue-1682'); + appPort = await getPort(); + app = await runDevCommand(appDir, appPort); + }); + + test.afterAll(async () => { + if (app) { + await killProcess(app); + } + }); + + test('Should render sidebar with index convention correctly', async ({ + page, + }) => { + await page.goto(`http://localhost:${appPort}/010.sub-directory/`, { + waitUntil: 'networkidle', + }); + + const sidebarTexts = await getSidebarTexts(page); + expect(sidebarTexts.length).toBe(2); + expect(sidebarTexts.join(',')).toEqual( + ['Second sub-directorytest', 'First sub-directory'].join(','), + ); + }); +}); diff --git a/packages/plugin-auto-nav-sidebar/src/utils.ts b/packages/plugin-auto-nav-sidebar/src/utils.ts index 8bddd1e5a..59eae1377 100644 --- a/packages/plugin-auto-nav-sidebar/src/utils.ts +++ b/packages/plugin-auto-nav-sidebar/src/utils.ts @@ -4,28 +4,30 @@ import type { NavItem, Sidebar } from '@rspress/shared'; import { logger } from '@rspress/shared/logger'; import { loadFrontMatter } from '@rspress/shared/node-utils'; +/** + * + * @param rawPath e.g: /usr/rspress-demo/docs/api.md or /usr/rspress-demo/docs/api + * @param extensions e.g: [".md"] + * @returns + */ export async function detectFilePath( rawPath: string, extensions: string[], ): Promise { - // The params doesn't have extension name, so we need to try to find the file with the extension name. - let realPath: string | undefined = rawPath; - const fileExtname = path.extname(rawPath); + 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]); - // pathname may contain .json, see issue: https://github.com/web-infra-dev/rspress/issues/951 - if (!extensions.includes(fileExtname)) { - 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]); - // file may be public resource, see issue: https://github.com/web-infra-dev/rspress/issues/1052 - if (!fileExtname || findPath) { - realPath = findPath; + if (!findPath) { + const stat = await fs.stat(rawPath); + if (stat.isFile()) { + return rawPath; } + return undefined; } - - return realPath; + return findPath; } export async function extractInfoFromFrontmatter( diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9e3faeeab..14c1ade31 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -256,6 +256,16 @@ importers: specifier: ^18.11.17 version: 18.11.17 + e2e/fixtures/issue-1682: + dependencies: + rspress: + specifier: workspace:* + version: link:../../../packages/cli + devDependencies: + '@types/node': + specifier: ^18.11.17 + version: 18.11.17 + e2e/fixtures/link: dependencies: rspress: