Make it easier to add sidebar entries dynamically #3452
Replies: 3 comments 5 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 |
|
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 |
|
I have been thinking about this a bit more today and I wanted to share... The use-case that @rzane and I have is mostly that we want to be able to have a custom docs schema for subsets of the files in the docs directory. e.g. we have docs/rules/ and docs/blips/ and docs/guides/ each wanting slightly different frontmatter schemas with some required fields. So, if we had the ability to have custom schemas for certain globs in the docs/ directory then we'd likely not this for our main use-case. To work around that, we have to move the docs out of docs/ and then add custom rendering logic which makes them incompatible with the default sidebar, etc. It would still be a useful feature though. |
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
All reactions