diff --git a/e2e/fixtures/auto-nav-sidebar/doc/api/_meta.json b/e2e/fixtures/auto-nav-sidebar/doc/api/_meta.json index ccafaabf0..1520f9be0 100644 --- a/e2e/fixtures/auto-nav-sidebar/doc/api/_meta.json +++ b/e2e/fixtures/auto-nav-sidebar/doc/api/_meta.json @@ -14,5 +14,6 @@ "name": "client-api", "label": "Client API" }, - "commands" + "commands", + "single-page" ] diff --git a/e2e/fixtures/auto-nav-sidebar/doc/api/single-page.mdx b/e2e/fixtures/auto-nav-sidebar/doc/api/single-page.mdx new file mode 100644 index 000000000..c38eed3e7 --- /dev/null +++ b/e2e/fixtures/auto-nav-sidebar/doc/api/single-page.mdx @@ -0,0 +1,11 @@ +--- +overviewHeaders: [] +--- + +# Single + +## Single-1 + +### Single-11 + +## Single-2 diff --git a/e2e/tests/auto-nav-sidebar.test.ts b/e2e/tests/auto-nav-sidebar.test.ts index a6dd879f4..815ebec9b 100644 --- a/e2e/tests/auto-nav-sidebar.test.ts +++ b/e2e/tests/auto-nav-sidebar.test.ts @@ -44,7 +44,7 @@ test.describe('Auto nav and sidebar test', async () => { const h2 = await page.$$('.overview-index h2'); const h2Texts = await Promise.all(h2.map(element => element.textContent())); expect(h2Texts.join(',')).toEqual( - ['Config', 'Client API', 'Others'].join(','), + ['Config', 'Client API', '命令', 'Single'].join(','), ); const h3 = await page.$$('.overview-group_8f375 h3'); @@ -59,6 +59,7 @@ test.describe('Auto nav and sidebar test', async () => { 'Runtime API', '内置组件', '命令', + 'Single', ].join(','), ); diff --git a/packages/theme-default/src/components/Overview/index.tsx b/packages/theme-default/src/components/Overview/index.tsx index 21951a2d4..def605566 100644 --- a/packages/theme-default/src/components/Overview/index.tsx +++ b/packages/theme-default/src/components/Overview/index.tsx @@ -120,6 +120,10 @@ export function Overview(props: { ) || [], }; } + + const isSingleFile = (item: SidebarItem | NormalizedSidebarGroup) => + !('items' in item) && 'link' in item && '_fileKey' in item; + const groups = customGroups ?? useMemo(() => { @@ -131,34 +135,47 @@ export function Overview(props: { .length > 0 ); } + if ( + isSingleFile(sidebarGroup) && + subFilter(getChildLink(sidebarGroup)) + ) { + return true; + } return false; }) - .map(sidebarGroup => ({ - name: sidebarGroup.text || '', - items: (sidebarGroup as NormalizedSidebarGroup).items - .map(item => + .map(sidebarGroup => { + let items = []; + if ((sidebarGroup as NormalizedSidebarGroup)?.items) { + items = (sidebarGroup as NormalizedSidebarGroup)?.items + ?.map(item => + normalizeSidebarItem( + item, + sidebarGroup as NormalizedSidebarGroup, + frontmatter, + ), + ) + .filter(Boolean); + } else if (isSingleFile(sidebarGroup)) { + items = [ normalizeSidebarItem( - item, - sidebarGroup as NormalizedSidebarGroup, + { + link: sidebarGroup.link, + text: sidebarGroup.text || '', + tag: sidebarGroup.tag, + _fileKey: sidebarGroup._fileKey, + overviewHeaders: sidebarGroup.overviewHeaders, + }, + undefined, frontmatter, ), - ) - .filter(Boolean), - })) as Group[]; - const singleLinks = overviewSidebarGroups.filter( - item => !('items' in item) && subFilter(item.link), - ); - return [ - ...group, - ...(singleLinks.length > 0 - ? [ - { - name: defaultGroupTitle, - items: singleLinks.map(item => normalizeSidebarItem(item)), - }, - ] - : []), - ]; + ]; + } + return { + name: sidebarGroup.text || '', + items, + }; + }) as Group[]; + return group; }, [overviewSidebarGroups, routePath, frontmatter]); return (