diff options
| author | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-06-02 21:58:37 +0300 |
|---|---|---|
| committer | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-06-02 21:58:37 +0300 |
| commit | be69ef9eeb8f8f59b3b26fc60378a7af7ca97cdc (patch) | |
| tree | 4748dce52ce104713607e6aa61c488e242668926 /website/src/components | |
| parent | b751d102f158968aab681cb663d27b4c32197f62 (diff) | |
fix: preserve preview docs navigation
Diffstat (limited to 'website/src/components')
| -rw-r--r-- | website/src/components/Sidebar.astro | 49 | ||||
| -rw-r--r-- | website/src/components/SiteTitle.astro | 4 |
2 files changed, 52 insertions, 1 deletions
diff --git a/website/src/components/Sidebar.astro b/website/src/components/Sidebar.astro new file mode 100644 index 00000000..c875a390 --- /dev/null +++ b/website/src/components/Sidebar.astro @@ -0,0 +1,49 @@ +--- +import MobileMenuFooter from 'virtual:starlight/components/MobileMenuFooter'; +import SidebarPersister from '@astrojs/starlight/components/SidebarPersister.astro'; +import SidebarSublist from '@astrojs/starlight/components/SidebarSublist.astro'; + +const { sidebar } = Astro.locals.starlightRoute; +const pathname = Astro.url.pathname; +const isPreview = pathname === '/docs/preview/' || pathname.startsWith('/docs/preview/'); +const channelSidebar = isPreview ? rewriteSidebar(sidebar) : sidebar; + +function rewriteSidebar(entries) { + return entries.map((entry) => { + if (entry.type === 'link') { + const href = previewHref(entry.href); + return { + ...entry, + href, + isCurrent: samePath(href, pathname), + }; + } + const rewrittenEntries = rewriteSidebar(entry.entries); + return { + ...entry, + entries: rewrittenEntries, + }; + }); +} + +function previewHref(href) { + if (!href.startsWith('/docs/') || href.startsWith('/docs/preview/')) return href; + return href === '/docs/' ? '/docs/preview/' : href.replace(/^\/docs\//, '/docs/preview/'); +} + +function samePath(left, right) { + return stripTrailingSlash(left) === stripTrailingSlash(right); +} + +function stripTrailingSlash(path) { + return path.length > 1 ? path.replace(/\/$/, '') : path; +} +--- + +<SidebarPersister> + <SidebarSublist sublist={channelSidebar} /> +</SidebarPersister> + +<div class="md:sl-hidden"> + <MobileMenuFooter /> +</div> diff --git a/website/src/components/SiteTitle.astro b/website/src/components/SiteTitle.astro index 1173f2b6..d35da5d6 100644 --- a/website/src/components/SiteTitle.astro +++ b/website/src/components/SiteTitle.astro @@ -1,5 +1,7 @@ --- const { siteTitle, siteTitleHref } = Astro.locals.starlightRoute; +const isPreview = Astro.url.pathname === '/docs/preview/' || Astro.url.pathname.startsWith('/docs/preview/'); +const docsHref = isPreview ? '/docs/preview/' : '/docs/'; --- <div class="title-row sl-flex"> @@ -8,7 +10,7 @@ const { siteTitle, siteTitleHref } = Astro.locals.starlightRoute; <span translate="no">{siteTitle}</span> </a> <nav class="title-nav" aria-label="Primary"> - <a href="/docs/">Docs</a> + <a href={docsHref}>Docs</a> <a href="/releases/">Releases</a> <a href="/blog/">Blog</a> </nav> |
