aboutsummaryrefslogtreecommitdiff
path: root/website/src/components/Sidebar.astro
blob: 8ded83bb0d640cded93343937af526fbdd2e4e0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
---
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 ? addPreviewOnlyItems(rewriteSidebar(sidebar)) : sidebar;

function addPreviewOnlyItems(entries) {
  return entries.map((entry) => {
    if (entry.type !== 'group' || entry.label !== 'Start here') return entry;

    const windowsBeta = {
      type: 'link',
      label: 'Windows beta',
      href: '/docs/preview/windows-beta/',
      badge: undefined,
      isCurrent: samePath('/docs/preview/windows-beta/', pathname),
      attrs: {},
    };
    const nextEntries = [];
    for (const child of entry.entries) {
      nextEntries.push(child);
      if (child.type === 'link' && child.label === 'Install') {
        nextEntries.push(windowsBeta);
      }
    }

    return {
      ...entry,
      entries: nextEntries,
    };
  });
}

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>