sailfork / t3code-github / .repos/ alchemy-effect/ website/ src/ components/ starlight/ Head.astro

---
/**
 * Starlight `Head` override that wraps the default head and appends our
 * per-page Open Graph image and Twitter card metadata. The image URL is
 * derived from the Starlight route slug and points at the static
 * `/og/<slug>.webp` artifact emitted at build time by
 * `src/pages/og/[...slug].webp.ts`.
 *
 * We intentionally append (rather than replace) so all of Starlight's
 * default `<title>`, description, canonical, RSS, and theme-color tags
 * still ship.
 */
import Default from "@astrojs/starlight/components/Head.astro";
import FontPreloads from "../FontPreloads.astro";
import PostHog from "../analytics/PostHog.astro";
import BrandIcons from "../../brand/Icons.astro";
import StructuredData from "../StructuredData.astro";
import { socialOrigin } from "../../social-origin";

// Starlight injects route data via Astro.locals at runtime; the public
// types aren't ambient-declared in this project, so we cast through any.
// Starlight ≥0.39 exposes the slug as `id` (the old `slug` field is gone
// — reading it yields undefined and produced `/og/undefined.webp`).
const { id } = (Astro.locals as { starlightRoute: { id: string } })
  .starlightRoute;
// Starlight normalizes the index entry's id to an empty string.
const ogSlug = id === "" ? "index" : id;
const ogImage = new URL(`/og/${ogSlug}.webp`, socialOrigin(Astro.site, Astro.url));
---

<Default><slot /></Default>

<FontPreloads />

{/* Brand mark. `src/favicon.ts` strips Starlight's own favicon link. */}
<BrandIcons />

{/* Open Graph image (Starlight's default Head emits og:title/description). */}
<meta property="og:image" content={ogImage.href} />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />

{/* Twitter / X */}
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@alchemy_run" />
<meta name="twitter:image" content={ogImage.href} />

{/* Organization (sameAs → social profiles) + WebSite JSON-LD. */}
<StructuredData />

<PostHog />

<script>
  // Copy the heading link to the clipboard whenever a user clicks one of
  // Starlight's auto-generated anchor buttons. Navigation still happens
  // normally — we just piggyback the copy on top of it.
  document.addEventListener("click", (event) => {
    const target = event.target as Element | null;
    const link = target?.closest?.("a.sl-anchor-link") as HTMLAnchorElement | null;
    if (!link) return;
    // `link.href` resolves to the absolute URL including the hash.
    void navigator.clipboard?.writeText(link.href).catch(() => {});
  });

  // Sidebar entries are CSS-truncated with an ellipsis (see custom.css). Give
  // each link a `title` so hovering reveals the full name in a native tooltip.
  // Runs on first paint and after every Starlight client-side navigation.
  const addSidebarTitles = () => {
    document
      .querySelectorAll<HTMLAnchorElement>(".sidebar-content a")
      .forEach((link) => {
        const text = link.textContent?.trim();
        if (text && !link.title) link.title = text;
      });
  };
  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", addSidebarTitles);
  } else {
    addSidebarTitles();
  }
  document.addEventListener("astro:page-load", addSidebarTitles);
</script>