Skip to content

Commit

Permalink
fix(theme): Overview component cannot be used when using base (#1642)
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter authored Dec 6, 2024
1 parent 86a80be commit 0a140dc
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .changeset/gold-spiders-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@rspress/plugin-auto-nav-sidebar': patch
'@rspress/theme-default': patch
'@rspress/shared': patch
---

fix the empty overview
1 change: 1 addition & 0 deletions e2e/fixtures/nested-overview/rspress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import * as path from 'node:path';
import { defineConfig } from 'rspress/config';

export default defineConfig({
base: '/base-url',
root: path.join(__dirname, 'doc'),
});
13 changes: 8 additions & 5 deletions e2e/tests/nested-overview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ test.describe('Nested overview page', async () => {
test('Should load nested overview page correctly - level 1', async ({
page,
}) => {
await page.goto(`http://localhost:${appPort}/basic-level-1/index.html`, {
waitUntil: 'networkidle',
});
await page.goto(
`http://localhost:${appPort}/base-url/basic-level-1/index.html`,
{
waitUntil: 'networkidle',
},
);

const h2 = await page.$$('.overview-index h2');
const h2Texts = await Promise.all(h2.map(element => element.textContent()));
Expand All @@ -39,7 +42,7 @@ test.describe('Nested overview page', async () => {
page,
}) => {
await page.goto(
`http://localhost:${appPort}/basic-level-1/level-2/index.html`,
`http://localhost:${appPort}/base-url/basic-level-1/level-2/index.html`,
{
waitUntil: 'networkidle',
},
Expand All @@ -58,7 +61,7 @@ test.describe('Nested overview page', async () => {
page,
}) => {
await page.goto(
`http://localhost:${appPort}/basic-level-1/level-2/level-3/index.html`,
`http://localhost:${appPort}/base-url/basic-level-1/level-2/level-3/index.html`,
{
waitUntil: 'networkidle',
},
Expand Down
8 changes: 7 additions & 1 deletion packages/plugin-auto-nav-sidebar/src/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,15 @@ export async function scanSideMeta(
tag,
} satisfies SidebarSectionHeader;
}

return {
text: label ?? '',
link: link && isExternalUrl(link) ? link : withBase(link, routePrefix),
link:
typeof link === 'undefined'
? ''
: isExternalUrl(link)
? link
: withBase(link, routePrefix),
tag,
context,
} satisfies SidebarItem;
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/src/runtime-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ export function withoutLang(path: string, langs: string[]) {
return addLeadingSlash(path.replace(langRegexp, ''));
}

export function withoutBase(path: string, base = '') {
export function withoutBase(path: string, base: string) {
return addLeadingSlash(path).replace(normalizeSlash(base), '');
}

export function withBase(url = '/', base = ''): string {
export function withBase(url: string, base: string): string {
const normalizedUrl = addLeadingSlash(url);
const normalizedBase = normalizeSlash(base);
// Avoid adding base path repeatly
Expand Down
10 changes: 5 additions & 5 deletions packages/theme-default/src/components/Overview/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
type NormalizedSidebarGroup,
type SidebarDivider,
type SidebarItem,
withBase,
import type {
NormalizedSidebarGroup,
SidebarDivider,
SidebarItem,
} from '@rspress/shared';
import { isSidebarDivider } from '../Sidebar/utils';
import { withBase } from '@rspress/runtime';

function removeIndex(link: string) {
if (link.endsWith('/index')) {
Expand Down

0 comments on commit 0a140dc

Please sign in to comment.