Make it easier to add sidebar entries dynamically #3452
Replies: 2 comments 3 replies
-
|
Hi @rzane 👋
Could you clarify the use case here a bit? What are the collections you’re looking to reference? Are these Astro content collections other than |
Beta Was this translation helpful? Give feedback.
-
|
Unless I've missed another way to do it, I think I have the same need. My use case would be for documenting a software library, such that each public export gets its own page and an entry in the sidebar. I can process the codebase so that the 'types' for the library are in a single flat JSON file... [
{title: 'Gizmo', slug: 'classes/Gizmo', description: '', properties: [], methods: [], ...}
{title: 'Widget', slug: 'classes/Widget', description: '', properties: [], methods: [], ...}
]... but I'm not sure how to make these appear in the sidebar without creating explicit .md pages for each export, or hard-coding the exports in the sidebar. I tried doing something like this in const coreItems = JSON.parse(await readFile('./src/data/api.json', 'utf8'));
export default defineConfig({
integrations: [
starlight({
title: 'My Docs',
sidebar: [
{
label: 'API',
items: coreItems.map((item) => ({
label: item.title,
slug: item.slug,
})),
},
],
}),
],
});...after defining a collection with the same JSON, but see |
Beta Was this translation helpful? Give feedback.
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.36.0
What is your idea?
The
sidebarproperty inastro.config.tsallows you to specify sidebar entries, but doesn't allow you to reference collections.After struggling to find a solution to this problem, I eventually read the source code for the sidebar component and realized I could manipulate the locals in a middleware to add entries to the sidebar.
However, this becomes relatively complicated since:
isCurrentSidebarEntry,SidebarLink, andSidebarGroupare not exported. There's a workaround, but it is a minor inconvenience (see Re-export SidebarLink, SidebarGroup, and SidebarEntry types for consumer use #3411).Points 1 and 2 could be solved by either exporting functions to recompute that data after changing the sidebar, or applying those changes in a middleware that runs after user defined middleware.
Why is this feature necessary?
Since the framework revolves around collections, it's really unintuitive that I can't easily incorporate them into my sidebar.
Do you have examples of this feature in other projects?
No response
Participation
Beta Was this translation helpful? Give feedback.
All reactions