Skip to content

Commit

Permalink
Fix sidebar issue in custom pages with custom sidebar (#2614)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Swithinbank <[email protected]>
  • Loading branch information
HiDeoo and delucis authored Nov 19, 2024
1 parent a73780f commit 9a31980
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-planets-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Fixes an issue with custom pages using the `<StarlightPage />` component and a custom sidebar missing highlighting for the active page and navigation links.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ test('adds custom frontmatter data to route shape', async () => {
test('uses generated sidebar when no sidebar is provided', async () => {
const data = await generateStarlightPageRouteData({
props: starlightPageProps,
url: starlightPageUrl,
url: new URL('https://example.com/getting-started/'),
});
expect(data.sidebar).toMatchInlineSnapshot(`
[
Expand All @@ -119,7 +119,7 @@ test('uses generated sidebar when no sidebar is provided', async () => {
"attrs": {},
"badge": undefined,
"href": "/getting-started/",
"isCurrent": false,
"isCurrent": true,
"label": "Getting Started",
"type": "link",
},
Expand Down Expand Up @@ -188,7 +188,7 @@ test('uses provided sidebar if any', async () => {
'reference/frontmatter',
],
},
url: starlightPageUrl,
url: new URL('https://example.com/test/2'),
});
expect(data.sidebar).toMatchInlineSnapshot(`
[
Expand All @@ -207,7 +207,7 @@ test('uses provided sidebar if any', async () => {
"attrs": {},
"badge": undefined,
"href": "/test/2",
"isCurrent": false,
"isCurrent": true,
"label": "Custom link 2",
"type": "link",
},
Expand Down Expand Up @@ -245,6 +245,8 @@ test('uses provided sidebar if any', async () => {
},
]
`);
expect(data.pagination.prev?.href).toBe('/test/1');
expect(data.pagination.next?.href).toBe('/guides/authoring-content/');
});

test('throws error if sidebar is malformated', async () => {
Expand Down
12 changes: 11 additions & 1 deletion packages/starlight/utils/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ const intermediateSidebars = new Map<string | undefined, SidebarEntry[]>();
export function getSidebar(pathname: string, locale: string | undefined): SidebarEntry[] {
let intermediateSidebar = intermediateSidebars.get(locale);
if (!intermediateSidebar) {
intermediateSidebar = getSidebarFromConfig(config.sidebar, pathname, locale);
intermediateSidebar = getIntermediateSidebarFromConfig(config.sidebar, pathname, locale);
intermediateSidebars.set(locale, intermediateSidebar);
}
return getSidebarFromIntermediateSidebar(intermediateSidebar, pathname);
Expand All @@ -359,6 +359,16 @@ export function getSidebarFromConfig(
sidebarConfig: StarlightConfig['sidebar'],
pathname: string,
locale: string | undefined
): SidebarEntry[] {
const intermediateSidebar = getIntermediateSidebarFromConfig(sidebarConfig, pathname, locale);
return getSidebarFromIntermediateSidebar(intermediateSidebar, pathname);
}

/** Get the intermediate sidebar for the current page using the specified sidebar config. */
function getIntermediateSidebarFromConfig(
sidebarConfig: StarlightConfig['sidebar'],
pathname: string,
locale: string | undefined
): SidebarEntry[] {
const routes = getLocaleRoutes(locale);
if (sidebarConfig) {
Expand Down

0 comments on commit 9a31980

Please sign in to comment.