Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter committed Nov 28, 2024
1 parent d6d127f commit 5b9825e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Plugin A

this directory has no meta and no index.mdx, should use directory name
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Plugin B
10 changes: 6 additions & 4 deletions e2e/tests/auto-nav-sidebar-no-meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ test.describe('Auto nav and sidebar test', async () => {
});

const sidebarTexts = await getSidebarTexts(page);
expect(sidebarTexts.length).toBe(3);
expect(sidebarTexts.length).toBe(4);
expect(sidebarTexts.join(',')).toEqual(
[
'API',
'pluginPlugin APlugin B',
'Commands',
'configBasic ConfigBuild ConfigFront Matter ConfigTheme Config',
].join(','),
Expand All @@ -43,10 +44,11 @@ test.describe('Auto nav and sidebar test', async () => {
waitUntil: 'networkidle',
});

const ele = page.locator('h2 span');
const eles = await page.$$('h2 span');

expect(await ele.textContent()).toBe('config');
await ele.click();
const configDir = eles[1];
expect(await configDir.textContent()).toBe('config');
await configDir.click();
expect(page.url()).toBe(
`http://localhost:${appPort}/api/rspress-config.html`,
);
Expand Down
27 changes: 25 additions & 2 deletions packages/plugin-auto-nav-sidebar/src/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function getHmrFileKey(realPath: string | undefined, docsDir: string) {
: '';
}

const DEFAULT_DIRNAME_PREFIX = 'rspress-dir-default-';
export async function scanSideMeta(
workDir: string,
rootDir: string,
Expand Down Expand Up @@ -77,7 +78,7 @@ export async function scanSideMeta(
return {
type: 'dir',
name: item,
label: undefined,
label: `${DEFAULT_DIRNAME_PREFIX}${item}`, // if no _meta.json, use the dir name as default
};
}
return extensions.some(ext => item.endsWith(ext)) ? item : null;
Expand Down Expand Up @@ -202,8 +203,30 @@ export async function scanSideMeta(
? await extractInfoFromFrontmatterWithRealPath(realPath, rootDir)
: ({} as { context: undefined; title: undefined });

function getDirLabelName(): string {
let dirName;

if (
typeof label === 'string' &&
label.startsWith(DEFAULT_DIRNAME_PREFIX)
) {
dirName = label.replace(DEFAULT_DIRNAME_PREFIX, '');
}

// 1. { "label": "DIR", type: "dir" } in _meta.json
if (!dirName && label) {
return label;
}
// 2. H1 or frontmatter title in md
if (title) {
return title;
}
// 3. fallback to fs dirname
return dirName ?? '';
}

return {
text: label ?? title,
text: getDirLabelName(),
collapsible,
collapsed,
items: subSidebar,
Expand Down

0 comments on commit 5b9825e

Please sign in to comment.