Skip to content

Commit

Permalink
feat: support single file overview in order (#1024)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 authored Apr 26, 2024
1 parent a87ad3b commit e421433
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
3 changes: 2 additions & 1 deletion e2e/fixtures/auto-nav-sidebar/doc/api/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"name": "client-api",
"label": "Client API"
},
"commands"
"commands",
"single-page"
]
11 changes: 11 additions & 0 deletions e2e/fixtures/auto-nav-sidebar/doc/api/single-page.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
overviewHeaders: []
---

# Single

## Single-1

### Single-11

## Single-2
3 changes: 2 additions & 1 deletion e2e/tests/auto-nav-sidebar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -59,6 +59,7 @@ test.describe('Auto nav and sidebar test', async () => {
'Runtime API',
'内置组件',
'命令',
'Single',
].join(','),
);

Expand Down
63 changes: 40 additions & 23 deletions packages/theme-default/src/components/Overview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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 (
Expand Down

0 comments on commit e421433

Please sign in to comment.