Autgenerate Sidebar from Custom Pages #2959
-
I am curious if there is a way to autogenerate Sidebar links for custom pages in the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
👋 There is no such built-in feature, it could also be quite tricky even if it was possible, e.g. if only some of them should be included, etc. What I personally usually end up doing is to extract the list of pages into a dedicated function and reuse it in both the Let's say I have a list of pages generated like this: // src/pages/dogs/[dog].astro
export function getStaticPaths() {
return [
{ params: { dog: "clifford" } },
{ params: { dog: "rover" } },
{ params: { dog: "spot" } },
];
} I would extract this list into a dedicated function: // src/libs/content.ts
export function getPages() {
return ["clifford", "rover", "spot"];
} Rewrite my // src/pages/dogs/[dog].astro
import { getPages } from "../../libs/content.ts";
export function getStaticPaths() {
return getPages().map((dog) => ({ params: { dog } }));
} And I would have an additional function generating sidebar links: // src/libs/content.ts
export function getSidebarLinks() {
return getPages().map((dog) => ({ label: dog, link: `/dogs/${dog}` }));
} That I would use in my configuration: import { getSidebarLinks } from "./src/libs/content.ts";
starlight({
sidebar: [
{
label: "Guides",
items: [{ label: "Example Guide", slug: "guides/example" }],
},
{ label: "Reference", autogenerate: { directory: "reference" } },
// Add the sidebar links
{ label: "Dogs", items: getSidebarLinks() },
],
}); |
Beta Was this translation helpful? Give feedback.
The example uses a literal list of strings but it's mostly for the example purposes only. If it was fetched from the network, it would be the same and the network call could be done in
getPages()
.Regarding the approach when using
getCollection()
, you could still do the same and do the call ingetPages()
but instead of usinggetSidebarLinks()
in your config, you can use it in a route data middleware to append your links: