Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter committed Nov 14, 2024
1 parent 3333cff commit fd07b18
Show file tree
Hide file tree
Showing 26 changed files with 228 additions and 98 deletions.
5 changes: 0 additions & 5 deletions e2e/fixtures/auto-nav-sidebar/doc/api/client-api/_meta.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
[
{
"type": "file",
"name": "index",
"label": "Client API Overview"
},
{
"type": "file",
"name": "api-runtime",
Expand Down
11 changes: 8 additions & 3 deletions e2e/fixtures/nested-overview/doc/_meta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
[
{
"text": "Guide",
"link": "/level1/",
"activeMatch": "/level1/"
"text": "Basic",
"link": "/basic-level-1/",
"activeMatch": "/basic-level-1"
},
{
"text": "Index Convention",
"link": "/index-convention-level-1/",
"activeMatch": "/index-convention-level-1"
}
]
8 changes: 8 additions & 0 deletions e2e/fixtures/nested-overview/doc/basic-level-1/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
"index",
{
"type": "dir",
"label": "Level 2",
"name": "level-2"
}
]
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
title: Level 1
overview: true
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
"index",
"two",
{
"type": "dir",
"name": "level-3",
"label": "Level 3"
}
]
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
title: Level 2
overview: true
---
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["index", "three"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Level 3
overview: true
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
"index",
{
"type": "dir",
"label": "Level 2",
"name": "level-2"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Level 1
overview: true
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
"two",
{
"type": "dir",
"name": "level-3",
"label": "Level 3"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Level 2
overview: true
---
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["three", "four"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Four
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Three
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Two
17 changes: 13 additions & 4 deletions e2e/tests/auto-nav-sidebar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ test.describe('Auto nav and sidebar test', async () => {
'Build Config',
'Extname Config',
'Nested',
'Client API Overview',
'Runtime API',
'Components',
'Commands',
Expand Down Expand Up @@ -90,7 +89,16 @@ 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'].join(','));
expect(h2Texts.join(',')).toEqual(
[
'Basic Config',
'Theme Config',
'Front Matter Config',
'Build Config',
'Extname Config',
'Nested',
].join(','),
);

const h3 = await page.$$('.overview-group_8f375 h3');
const h3Texts = await Promise.all(h3.map(element => element.textContent()));
Expand All @@ -101,7 +109,7 @@ test.describe('Auto nav and sidebar test', async () => {
'Front Matter Config',
'Build Config',
'Extname Config',
'Nested',
'Nested Config',
].join(','),
);

Expand All @@ -117,14 +125,15 @@ test.describe('Auto nav and sidebar test', async () => {
'Default Config',
'markdown',
'markdown.remarkPlugins',
'Nested H2',
].join(','),
);
});

test('Should load subpage API Overview correctly - index.md', async ({
page,
}) => {
await page.goto(`http://localhost:${appPort}/api/client-api/index.html`, {
await page.goto(`http://localhost:${appPort}/api/client-api.html`, {
waitUntil: 'networkidle',
});

Expand Down
21 changes: 12 additions & 9 deletions e2e/tests/nested-overview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,43 @@ test.describe('Nested overview page', async () => {
test('Should load nested overview page correctly - level 1', async ({
page,
}) => {
await page.goto(`http://localhost:${appPort}/level1/index.html`, {
await page.goto(`http://localhost:${appPort}/basic-level-1/index.html`, {
waitUntil: 'networkidle',
});

const h2 = await page.$$('.overview-index h2');
const h2Texts = await Promise.all(h2.map(element => element.textContent()));
expect(h2Texts.join(',')).toEqual(['level2'].join(','));
expect(h2Texts.join(',')).toEqual(['Level 2'].join(','));

const h3 = await page.$$('.overview-group_8f375 h3');
const h3Texts = await Promise.all(h3.map(element => element.textContent()));
expect(h3Texts.join(',')).toEqual(['index', 'level3', 'two'].join(','));
expect(h3Texts.join(',')).toEqual(['Level 2', 'two', 'Level 3'].join(','));
});

test('Should load nested overview page correctly - level 2', async ({
page,
}) => {
await page.goto(`http://localhost:${appPort}/level1/level2/index.html`, {
waitUntil: 'networkidle',
});
await page.goto(
`http://localhost:${appPort}/basic-level-1/level-2/index.html`,
{
waitUntil: 'networkidle',
},
);

const h2 = await page.$$('.overview-index h2');
const h2Texts = await Promise.all(h2.map(element => element.textContent()));
expect(h2Texts.join(',')).toEqual(['level3', 'two'].join(','));
expect(h2Texts.join(',')).toEqual(['two', 'Level 3'].join(','));

const h3 = await page.$$('.overview-group_8f375 h3');
const h3Texts = await Promise.all(h3.map(element => element.textContent()));
expect(h3Texts.join(',')).toEqual(['index', 'three', 'two'].join(','));
expect(h3Texts.join(',')).toEqual(['two', 'Level 3', 'three'].join(','));
});

test('Should load nested overview page correctly - level 3', async ({
page,
}) => {
await page.goto(
`http://localhost:${appPort}/level1/level2/level3/index.html`,
`http://localhost:${appPort}/basic-level-1/level-2/level-3/index.html`,
{
waitUntil: 'networkidle',
},
Expand Down
52 changes: 11 additions & 41 deletions packages/theme-default/src/components/Overview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
useLocaleSiteData,
} from '../../logic';
import styles from './index.module.scss';
import { findItemByRoutePath } from './utils';

interface GroupItem {
text?: string;
Expand Down Expand Up @@ -133,36 +134,6 @@ export function Overview(props: {
return '';
};

const removeIndex = (link: string) => {
if (link.endsWith('/index')) {
return link.slice(0, -5);
}
return link;
};

const findItemByRoutePath = (items, routePath, originalItems) => {
for (const item of items) {
if (withBase(item.link) === routePath) {
return [item];
}

if (removeIndex(withBase(item.link)) === routePath) {
return items;
}

if (item.items) {
const foundItem = findItemByRoutePath(
item.items,
routePath,
originalItems,
);
if (foundItem) {
return foundItem;
}
}
}
return originalItems;
};
const { pages } = siteData;
const overviewModules = pages.filter(page => subFilter(page.routePath));
let { items: overviewSidebarGroups } = useSidebarData() as {
Expand All @@ -181,7 +152,6 @@ export function Overview(props: {
overviewSidebarGroups = findItemByRoutePath(
overviewSidebarGroups,
routePath,
overviewSidebarGroups,
);
}

Expand Down Expand Up @@ -227,8 +197,11 @@ export function Overview(props: {
customGroups ??
useMemo(() => {
const group = overviewSidebarGroups
.filter(sidebarGroup => {
if ('items' in sidebarGroup && sidebarGroup.items) {
.filter(normalizedSidebarGroup => {
const sidebarGroup = normalizedSidebarGroup as NormalizedSidebarGroup;
if (Array.isArray(sidebarGroup?.items)) {
console.log(sidebarGroup, 2222);

return (
sidebarGroup.items.filter(item => subFilter(getChildLink(item)))
.length > 0
Expand All @@ -242,16 +215,13 @@ export function Overview(props: {
}
return false;
})
.map(sidebarGroup => {
.map(normalizedSidebarGroup => {
const sidebarGroup = normalizedSidebarGroup as NormalizedSidebarGroup;
let items = [];
if ((sidebarGroup as NormalizedSidebarGroup)?.items) {
items = (sidebarGroup as NormalizedSidebarGroup)?.items
if (sidebarGroup?.items) {
items = sidebarGroup?.items
?.map(item =>
normalizeSidebarItem(
item,
sidebarGroup as NormalizedSidebarGroup,
frontmatter,
),
normalizeSidebarItem(item, sidebarGroup, frontmatter),
)
.filter(Boolean);
} else if (isSingleFile(sidebarGroup)) {
Expand Down
88 changes: 88 additions & 0 deletions packages/theme-default/src/components/Overview/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {
type NormalizedSidebarGroup,
type SidebarDivider,
type SidebarItem,
withBase,
} from '@rspress/shared';
import { isSidebarDivider } from '../Sidebar/utils';

function removeIndex(link: string) {
if (link.endsWith('/index')) {
return link.slice(0, -5);
}
return link;
}

/**
* @zh_CN 如果 index 在 sidebar items 中, 则返回所有平级 item, 如果 index 在 dir 上, 则返回 items
* @example
*/
export function findItemByRoutePath(
items: (SidebarItem | NormalizedSidebarGroup | SidebarDivider)[],
routePath: string,
): (SidebarItem | NormalizedSidebarGroup)[] {
function isRoutePathMatch(
item: SidebarItem | NormalizedSidebarGroup | SidebarDivider,
) {
if (isSidebarDivider(item)) {
return false;
}
const withBaseUrl = withBase(item.link);
const removeIndexUrl = removeIndex(withBaseUrl);
const removeBackSlashedRoutePath = routePath.replace(/\/$/, '');
return (
// FIXME: 😅 we should refactor all the path logic, /index.html / or no /, l
withBaseUrl === routePath ||
removeIndexUrl === routePath ||
withBaseUrl === removeBackSlashedRoutePath ||
removeIndexUrl === removeBackSlashedRoutePath
);
}

const matchRoutePathItemIndex = items.findIndex(item => {
return isRoutePathMatch(item);
});

if (matchRoutePathItemIndex === -1) {
return items
.map(item => {
if (!('items' in item)) {
return [];
}
return findItemByRoutePath(item.items, routePath);
})
.flat();
}

const matchRoutePathItem = items[matchRoutePathItemIndex] as
| SidebarItem
| NormalizedSidebarGroup;

const isArray = (i: unknown): i is Array<unknown> =>
Array.isArray(i) && i.length >= 1;

// 1. if isDir(item) return item.items
if ('items' in matchRoutePathItem && isArray(matchRoutePathItem.items)) {
// 2. if isDir(item) && item.items is all files, style is different
if (matchRoutePathItem.items.every(item => !('items' in item))) {
return [matchRoutePathItem];
}

return matchRoutePathItem.items.filter(item => !isSidebarDivider(item)) as (
| SidebarItem
| NormalizedSidebarGroup
)[];
}

// 3. if matchRoutePathItem is a item, return other items in same level (/api/index.md is in the child sidebar)
const result = [...items];
if (!('items' in matchRoutePathItem)) {
result.splice(matchRoutePathItemIndex, 1);
}
const res = result.filter(item => !isSidebarDivider(item)) as (
| SidebarItem
| NormalizedSidebarGroup
)[];

return res;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ import { Tag } from '@theme';
import styles from './index.module.scss';
import { SidebarItem as SidebarItemComp } from './SidebarItem';
import { SidebarDivider } from './SidebarDivider';
import {
highlightTitleStyle,
isSidebarDivider,
type SidebarItemProps,
} from '.';
import { highlightTitleStyle, type SidebarItemProps } from '.';
import { isSidebarDivider } from './utils';
import { SvgWrapper } from '../SvgWrapper';
import { renderInlineMarkdown } from '../../logic';

Expand Down
Loading

0 comments on commit fd07b18

Please sign in to comment.