--- import SidebarPersister from "@astrojs/starlight/components/SidebarPersister.astro"; import SidebarSublist from "@astrojs/starlight/components/SidebarSublist.astro"; import ThemeSelect from "virtual:starlight/components/ThemeSelect"; import SidebarToggle from "./SidebarToggle.astro"; const { sidebar = [] } = Astro.locals.starlightRoute ?? {}; const currentPath = decodeURIComponent(Astro.url.pathname); const getTopSegment = (value) => { const normalized = decodeURIComponent(value || "") .replace(/^\/+|\/+$/g, ""); if (!normalized) return ""; return normalized.split("/")[0]; }; const currentTopSegment = getTopSegment(currentPath); const entryTopSegment = (entry) => { if (entry.type === "link" && entry.href) { try { const pathname = new URL(entry.href, Astro.url.origin).pathname; return getTopSegment(pathname); } catch { return getTopSegment(entry.href); } } if (entry.type === "group" && Array.isArray(entry.entries)) { for (const child of entry.entries) { const segment = entryTopSegment(child); if (segment) return segment; } } return ""; }; const entryContainsPath = (entry) => { if (entry.type === "link" && entry.href) { const linkHref = decodeURIComponent(entry.href); return currentPath.startsWith(linkHref); } if (entry.type === "group" && Array.isArray(entry.entries)) { return entry.entries.some(entryContainsPath); } return false; }; const activeTopLevelGroup = sidebar.find( (entry) => entry.type === "group" && (entryContainsPath(entry) || (currentTopSegment && entryTopSegment(entry) === currentTopSegment)), ); const scopedSidebar = activeTopLevelGroup && Array.isArray(activeTopLevelGroup.entries) ? activeTopLevelGroup.entries : sidebar; ---