Skip to content

Commit

Permalink
fix(plugin-auto-nav-sidebar): dir name contain dot
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter committed Dec 19, 2024
1 parent 1312657 commit 997cf4d
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Second sub-directory
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# First sub-directory
7 changes: 7 additions & 0 deletions e2e/fixtures/auto-nav-sidebar-issue-1682/docs/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"text": "010.sub-directory",
"link": "/010.sub-directory/",
"activeMatch": "/010.sub-directory/"
}
]
13 changes: 13 additions & 0 deletions e2e/fixtures/auto-nav-sidebar-issue-1682/docs/index.md
Original file line number Diff line number Diff line change
@@ -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
```
16 changes: 16 additions & 0 deletions e2e/fixtures/auto-nav-sidebar-issue-1682/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
7 changes: 7 additions & 0 deletions e2e/fixtures/auto-nav-sidebar-issue-1682/rspress.config.ts
Original file line number Diff line number Diff line change
@@ -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'),
});
1 change: 1 addition & 0 deletions e2e/fixtures/auto-nav-sidebar-issue-1682/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
36 changes: 36 additions & 0 deletions e2e/tests/auto-nav-sidebar-issue-1682.test.ts
Original file line number Diff line number Diff line change
@@ -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(','),
);
});
});
32 changes: 17 additions & 15 deletions packages/plugin-auto-nav-sidebar/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined> {
// 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(
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 997cf4d

Please sign in to comment.