{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "message-markdown",
  "type": "registry:component",
  "title": "Message Markdown",
  "description": "GFM markdown renderer for assistant message text with citation chips.",
  "dependencies": [
    "react-markdown",
    "remark-gfm"
  ],
  "registryDependencies": [
    "citation",
    "button"
  ],
  "files": [
    {
      "path": "registry/tonic/message-markdown/message-markdown.tsx",
      "content": "\"use client\"\n\nimport {\n  createContext,\n  memo,\n  useContext,\n  useState,\n  type ComponentPropsWithoutRef,\n  type ReactNode,\n} from \"react\"\nimport ReactMarkdown, { type ExtraProps } from \"react-markdown\"\nimport remarkGfm from \"remark-gfm\"\nimport { CheckIcon, CopyIcon } from \"lucide-react\"\n\nimport { cn } from \"@/lib/utils\"\nimport {\n  CitationChip,\n  childrenToPlainText,\n  isCitationLabel,\n} from \"@/registry/tonic/citation/citation\"\nimport { Button } from \"@/registry/tonic/ui/button\"\n\nconst BlockCodeContext = createContext(false)\n\ntype MarkdownElementProps = ComponentPropsWithoutRef<\"pre\"> & ExtraProps\ntype CodeElementProps = ComponentPropsWithoutRef<\"code\"> & ExtraProps\n\nfunction CodeHeader({ language, code }: { language?: string; code: string }) {\n  const [copied, setCopied] = useState(false)\n  return (\n    <div className=\"border-border/50 bg-muted/50 mt-3 flex items-center justify-between rounded-t-xl border border-b-0 px-3.5 py-1.5 text-xs\">\n      <span className=\"text-muted-foreground font-medium lowercase\">\n        {language || \"text\"}\n      </span>\n      <Button\n        type=\"button\"\n        variant=\"ghost\"\n        size=\"icon\"\n        className=\"size-6\"\n        aria-label=\"Copy code\"\n        onClick={async () => {\n          if (!code || copied) return\n          await navigator.clipboard.writeText(code)\n          setCopied(true)\n          window.setTimeout(() => setCopied(false), 1500)\n        }}\n      >\n        {copied ? (\n          <CheckIcon className=\"size-3.5\" />\n        ) : (\n          <CopyIcon className=\"size-3.5\" />\n        )}\n      </Button>\n    </div>\n  )\n}\n\nconst markdownComponents = {\n  h1: ({ className, ...props }: ComponentPropsWithoutRef<\"h1\">) => (\n    <h1\n      className={cn(\n        \"mt-5 mb-2 scroll-m-20 text-[15px] font-semibold first:mt-0 last:mb-0\",\n        className\n      )}\n      {...props}\n    />\n  ),\n  h2: ({ className, ...props }: ComponentPropsWithoutRef<\"h2\">) => (\n    <h2\n      className={cn(\n        \"mt-5 mb-2 scroll-m-20 text-[15px] font-semibold first:mt-0 last:mb-0\",\n        className\n      )}\n      {...props}\n    />\n  ),\n  h3: ({ className, ...props }: ComponentPropsWithoutRef<\"h3\">) => (\n    <h3\n      className={cn(\n        \"mt-4 mb-1.5 scroll-m-20 text-[15px] font-semibold first:mt-0 last:mb-0\",\n        className\n      )}\n      {...props}\n    />\n  ),\n  h4: ({ className, ...props }: ComponentPropsWithoutRef<\"h4\">) => (\n    <h4\n      className={cn(\n        \"mt-3.5 mb-1 scroll-m-20 text-[15px] font-medium first:mt-0 last:mb-0\",\n        className\n      )}\n      {...props}\n    />\n  ),\n  p: ({ className, ...props }: ComponentPropsWithoutRef<\"p\">) => (\n    <p\n      className={cn(\n        \"my-3 text-[15px] leading-6 first:mt-0 last:mb-0\",\n        className\n      )}\n      {...props}\n    />\n  ),\n  a: function MarkdownLink({\n    className,\n    children,\n    href,\n    title,\n    ...props\n  }: ComponentPropsWithoutRef<\"a\">) {\n    const label = childrenToPlainText(children)\n    if (href && isCitationLabel(label) && /^https?:\\/\\//i.test(href)) {\n      return (\n        <CitationChip\n          index={label.replace(/[\\[\\]]/g, \"\")}\n          href={href}\n          title={typeof title === \"string\" ? title : undefined}\n        />\n      )\n    }\n    return (\n      <a\n        href={href}\n        title={title}\n        target=\"_blank\"\n        rel=\"noreferrer\"\n        className={cn(\"text-foreground underline underline-offset-2\", className)}\n        {...props}\n      >\n        {children}\n      </a>\n    )\n  },\n  ul: ({ className, ...props }: ComponentPropsWithoutRef<\"ul\">) => (\n    <ul\n      className={cn(\"my-3 list-disc space-y-1 ps-5 text-[15px] leading-6\", className)}\n      {...props}\n    />\n  ),\n  ol: ({ className, ...props }: ComponentPropsWithoutRef<\"ol\">) => (\n    <ol\n      className={cn(\n        \"my-3 list-decimal space-y-1 ps-5 text-[15px] leading-6\",\n        className\n      )}\n      {...props}\n    />\n  ),\n  li: ({ className, ...props }: ComponentPropsWithoutRef<\"li\">) => (\n    <li className={cn(\"leading-6\", className)} {...props} />\n  ),\n  blockquote: ({\n    className,\n    ...props\n  }: ComponentPropsWithoutRef<\"blockquote\">) => (\n    <blockquote\n      className={cn(\n        \"border-border text-muted-foreground my-3 border-l-2 ps-3\",\n        className\n      )}\n      {...props}\n    />\n  ),\n  table: ({ className, ...props }: ComponentPropsWithoutRef<\"table\">) => (\n    <div className=\"my-3 overflow-x-auto\">\n      <table\n        className={cn(\"w-full border-collapse text-[13px]\", className)}\n        {...props}\n      />\n    </div>\n  ),\n  th: ({ className, ...props }: ComponentPropsWithoutRef<\"th\">) => (\n    <th\n      className={cn(\n        \"border-border border-b px-2 py-1.5 text-start font-medium\",\n        className\n      )}\n      {...props}\n    />\n  ),\n  td: ({ className, ...props }: ComponentPropsWithoutRef<\"td\">) => (\n    <td\n      className={cn(\"border-border/60 border-b px-2 py-1.5 align-top\", className)}\n      {...props}\n    />\n  ),\n  hr: ({ className, ...props }: ComponentPropsWithoutRef<\"hr\">) => (\n    <hr className={cn(\"border-border my-4\", className)} {...props} />\n  ),\n  pre: ({ children, className, ...props }: MarkdownElementProps) => {\n    const codeChild = Array.isArray(children) ? children[0] : children\n    const codeProps =\n      codeChild &&\n      typeof codeChild === \"object\" &&\n      \"props\" in codeChild\n        ? (codeChild.props as { className?: string; children?: ReactNode })\n        : undefined\n    const language = codeProps?.className?.match(/language-([^\\s]+)/)?.[1]\n    const codeText = childrenToPlainText(codeProps?.children ?? children)\n\n    return (\n      <BlockCodeContext.Provider value={true}>\n        <div className=\"my-3 first:mt-0 last:mb-0\">\n          <CodeHeader language={language} code={codeText} />\n          <pre\n            className={cn(\n              \"border-border/50 bg-muted/30 overflow-x-auto rounded-b-xl border border-t-0 p-3.5 text-[13px] leading-relaxed\",\n              className\n            )}\n            {...props}\n          >\n            {children}\n          </pre>\n        </div>\n      </BlockCodeContext.Provider>\n    )\n  },\n  code: ({ className, children, ...props }: CodeElementProps) => {\n    const isBlock = useContext(BlockCodeContext)\n    return (\n      <code\n        className={cn(\n          !isBlock &&\n            \"bg-muted rounded-md px-1.5 py-0.5 font-mono text-[0.85em]\",\n          className\n        )}\n        {...props}\n      >\n        {children}\n      </code>\n    )\n  },\n}\n\nexport const MessageMarkdown = memo(function MessageMarkdown({\n  text,\n  className,\n}: {\n  readonly text: string\n  readonly className?: string\n}) {\n  return (\n    <div data-slot=\"message-markdown\" className={cn(\"aui-md\", className)}>\n      <ReactMarkdown remarkPlugins={[remarkGfm]} components={markdownComponents}>\n        {text}\n      </ReactMarkdown>\n    </div>\n  )\n})\n",
      "type": "registry:component"
    }
  ]
}