Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support single file overview in order #1024

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading