Skip to content

Incremental engine for long horizon agents 🌟 Star if you like it!

3.0 KB
Raw Blame
---
import { sidebar } from '../data/docs-sidebar';
import { langHref, pageRegistry, sidebarFor, type Lang } from '../lib/lang';
import LangSwitch from './LangSwitch.astro';
import Search from './Search.astro';

export interface Props {
  /** Route of the currently-rendered doc, e.g. `getting_started/quickstart`. */
  currentSlug: string;
  /** SDK language of the page: the sidebar lists that language's pages. */
  lang: Lang;
}

const { currentSlug, lang } = Astro.props;
const items = sidebarFor(sidebar, lang, await pageRegistry());

// Trailing slash matches the canonical URL Astro emits under
// `build.format: 'directory'` β€” saves a 301 round-trip per click.
const href = (slug: string): string => langHref(lang, slug);
---

<aside class="side" aria-label="Documentation pages">
  <LangSwitch lang={lang} route={currentSlug} />
  <Search />
  <div class="side-group">
    <ul>
      <li>
        <a
          href={href('')}
          class={currentSlug === '' ? 'on' : ''}
          data-current={currentSlug === '' || null}
          aria-current={currentSlug === '' ? 'page' : undefined}
        >
          About CocoIndex
        </a>
      </li>
    </ul>
  </div>
  {items.map((item) => (
    item.type === 'doc' ? (
      <div class="side-group">
        <ul>
          <li>
            <a
              href={href(item.slug)}
              class={currentSlug === item.slug ? 'on' : ''}
              data-current={currentSlug === item.slug || null}
              aria-current={currentSlug === item.slug ? 'page' : undefined}
            >
              {item.label ?? item.slug}
            </a>
          </li>
        </ul>
      </div>
    ) : (
      <div class="side-group">
        <p class="side-h">
          {item.slug ? (
            <a href={href(item.slug)}>{item.label}</a>
          ) : (
            <span>{item.label}</span>
          )}
        </p>
        <ul>
          {item.items.map((child) => (
            child.type === 'doc' ? (
              <li>
                <a
                  href={href(child.slug)}
                  class={currentSlug === child.slug ? 'on' : ''}
                  data-current={currentSlug === child.slug || null}
                  aria-current={currentSlug === child.slug ? 'page' : undefined}
                >
                  {child.label ?? child.slug}
                </a>
              </li>
            ) : null
          ))}
        </ul>
      </div>
    )
  ))}
</aside>

<script>
  // On load, bring the active sidebar entry into view by scrolling the rail
  // itself. scrollIntoView would also scroll the window (the rail is sticky),
  // which left pages low in the tree (CLI, FAQ, About) opening mid-page.
  const current = document.querySelector<HTMLAnchorElement>('aside.side a[data-current]');
  const rail = current?.closest<HTMLElement>('aside.side');
  if (current && rail) {
    const target = current.offsetTop - rail.clientHeight / 2 + current.offsetHeight / 2;
    if (target > 0) rail.scrollTop = target;
  }
</script>