Allow sidebar to associate subpages with parent pages without explicitly listing them #3275
sdht0
started this conversation in
Feature Requests
Replies: 1 comment 8 replies
-
|
Hi @sdht0! Thanks for sharing that VitePress API — always interesting to see how other tools approach these things. Currently there’s no dedicated API to highlight parent pages like you want, but you can achieve this with route middleware. For example, based on your developer guide structure: import { defineRouteMiddleware } from '@astrojs/starlight/route-data';
export const onRequest = defineRouteMiddleware((context) => {
const { sidebar } = context.locals.starlightRoute;
for (const entry of sidebar) {
if (
// Find the top-level link to the developer guide parent page
entry.type === 'link' &&
entry.href === '/developer-guide/' &&
// If this page is in the developer-guide/ subdirectory, mark the parent page as current.
context.url.pathname.startsWith('/developer-guide/')
) {
entry.isCurrent = true;
}
}
});Here’s a small example of that running in context: https://stackblitz.com/edit/github-xugkbk2l?file=astro.config.mjs,src%2Froute-data.ts |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
What version of
starlightare you using?0.32.5
What is your idea?
Say I have a sidebar entry:
{ label: 'Developer Guide', link: '/developer-guide' },. Say I also have pages'/developer-guide/build-from-source'and'/developer-guide/formatting-rules'. I want the sidebar to highlight "Developer Guide" when any of these pages are open, without listing these subpages in the sidebar.Is this currently possible in Starlight? Did not find a way in the docs.
Why is this feature necessary?
I don't want to explicitly list all the subpages, to avoid too deep hierarchies in the sidebar, but still highlight the first parent page that has an entry in the sidebar.
Do you have examples of this feature in other projects?
activeMatchhttps://vitepress.dev/reference/default-theme-nav#customize-link-s-active-stateParticipation
Beta Was this translation helpful? Give feedback.
All reactions