import { Link, useNavigate, useParams } from "@tanstack/react-router"; import { useEffect, type MouseEvent } from "react"; import type { Guide } from "../docs/guides"; import { guides } from "virtual:guides"; import { NotFound } from "./NotFound"; const to = (guide: Guide) => (guide.slug ? `/docs/${guide.slug}/` : "/docs/"); async function copy(button: HTMLButtonElement) { const code = button.parentElement?.querySelector("code")?.textContent; if (code == null) throw new Error("docs: a copy button without code"); try { await navigator.clipboard.writeText(code); button.textContent = "Copied"; } catch (error) { console.error("docs: copy failed", error); button.textContent = "Copy failed"; } setTimeout(() => (button.textContent = "Copy"), 1600); } export function Docs() { const { page } = useParams({ strict: false }); const navigate = useNavigate(); const at = guides.findIndex((g) => g.slug === (page ?? "")); const guide = guides[at]; useEffect(() => { if (guide) document.title = `${guide.title} · Docs · Ely`; }, [guide]); if (!guide) return ; const [prev, next] = [guides[at - 1], guides[at + 1]]; // Links and copy buttons inside the rendered Markdown. const press = (e: MouseEvent) => { const target = e.target as HTMLElement; const button = target.closest("button.copy"); if (button) return void copy(button); const link = target.closest("a[href^='/']:not([href^='//'])"); if (!link || e.metaKey || e.ctrlKey || e.shiftKey || e.button !== 0) return; e.preventDefault(); const url = new URL(link.href); navigate({ to: url.pathname, hash: url.hash.slice(1) || undefined }); }; return (

{guide.title}

{guide.toc.length > 0 && ( )}
); }