{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "citation",
  "type": "registry:component",
  "title": "Citation",
  "description": "Citation chips and sources list for numbered markdown references.",
  "registryDependencies": [
    "surfaces"
  ],
  "files": [
    {
      "path": "registry/tonic/citation/citation.tsx",
      "content": "\"use client\"\n\nimport {\n  isValidElement,\n  type ComponentProps,\n  type ReactNode,\n} from \"react\"\n\nimport { cn } from \"@/lib/utils\"\nimport { threadMeta, threadText } from \"@/registry/tonic/lib/surfaces\"\n\nexport type CitationRef = {\n  readonly index: string\n  readonly url: string\n  readonly title?: string\n}\n\nconst CITATION_LINK_RE =\n  /\\[(\\d+)\\]\\((https?:\\/\\/[^\\s)]+)(?:\\s+\"([^\"]*)\")?\\)/g\n\n/** Pull numbered markdown citations `[1](https://…)` out of assistant text. */\nexport function extractCitationRefs(markdown: string): CitationRef[] {\n  const seen = new Set<string>()\n  const refs: CitationRef[] = []\n\n  for (const match of markdown.matchAll(CITATION_LINK_RE)) {\n    const index = match[1]\n    const url = match[2]\n    const title = match[3]?.trim() || undefined\n    if (!index || !url || seen.has(index)) continue\n    seen.add(index)\n    refs.push(title ? { index, url, title } : { index, url })\n  }\n\n  return refs.sort((a, b) => Number(a.index) - Number(b.index))\n}\n\nexport function childrenToPlainText(children: ReactNode): string {\n  if (\n    children === null ||\n    children === undefined ||\n    typeof children === \"boolean\"\n  ) {\n    return \"\"\n  }\n  if (typeof children === \"string\" || typeof children === \"number\") {\n    return String(children)\n  }\n  if (Array.isArray(children)) {\n    return children.map(childrenToPlainText).join(\"\")\n  }\n  if (isValidElement<{ children?: ReactNode }>(children)) {\n    return childrenToPlainText(children.props.children)\n  }\n  return \"\"\n}\n\n/** True when a markdown link label is a bare citation number like `1` or `[1]`. */\nexport function isCitationLabel(text: string): boolean {\n  return /^\\s*\\[?\\d+\\]?\\s*$/.test(text)\n}\n\nfunction hostnameOf(url: string): string {\n  try {\n    return new URL(url).hostname.replace(/^www\\./, \"\")\n  } catch {\n    return url\n  }\n}\n\nexport function CitationChip({\n  index,\n  href,\n  title,\n  className,\n}: {\n  readonly index: string\n  readonly href: string\n  readonly title?: string\n  readonly className?: string\n}) {\n  const label = index.replace(/^\\s*\\[|\\]\\s*$/g, \"\")\n  const displayTitle = title?.trim() || hostnameOf(href)\n\n  return (\n    <a\n      href={href}\n      target=\"_blank\"\n      rel=\"noreferrer\"\n      data-slot=\"citation-chip\"\n      title={`${displayTitle} · ${hostnameOf(href)}`}\n      aria-label={`Source ${label}: ${displayTitle}`}\n      className={cn(\n        \"bg-muted text-muted-foreground hover:bg-muted/80 hover:text-foreground ms-0.5 inline-flex h-4 min-w-4 translate-y-[-1px] items-center justify-center rounded px-1 align-super text-[11px] leading-none font-medium tabular-nums no-underline transition-colors\",\n        className\n      )}\n    >\n      {label}\n    </a>\n  )\n}\n\nexport function MessageSources({\n  refs,\n  className,\n  ...props\n}: ComponentProps<\"div\"> & { refs: readonly CitationRef[] }) {\n  if (refs.length === 0) return null\n\n  return (\n    <div\n      data-slot=\"message-sources\"\n      className={cn(\"flex flex-col gap-1.5 pt-1\", className)}\n      {...props}\n    >\n      <p className={cn(threadMeta, \"font-medium\")}>Sources</p>\n      <ol className=\"flex flex-col gap-1\">\n        {refs.map((ref) => (\n          <li key={ref.index} className=\"flex min-w-0 items-baseline gap-2\">\n            <span className=\"bg-muted text-muted-foreground inline-flex h-4 min-w-4 shrink-0 items-center justify-center rounded px-1 text-[11px] leading-none font-medium tabular-nums\">\n              {ref.index}\n            </span>\n            <a\n              href={ref.url}\n              target=\"_blank\"\n              rel=\"noreferrer\"\n              className={cn(\n                threadMeta,\n                \"hover:text-foreground min-w-0 truncate transition-colors\"\n              )}\n              title={ref.title ?? ref.url}\n            >\n              {ref.title?.trim() || hostnameOf(ref.url)}\n              <span className=\"text-muted-foreground/70\">\n                {\" \"}\n                · {hostnameOf(ref.url)}\n              </span>\n            </a>\n          </li>\n        ))}\n      </ol>\n    </div>\n  )\n}\n\nexport { hostnameOf, threadText }\n",
      "type": "registry:component"
    }
  ]
}