{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "markdown",
  "title": "Markdown",
  "description": "Streaming markdown that parses only the blocks that changed.",
  "dependencies": [
    "@kind-ui/agent-ui@0.9.0",
    "@ui-sdk/core@0.1.0-beta.17",
    "@ui-sdk/react@0.1.0-beta.17"
  ],
  "registryDependencies": [
    "@kind-ui/agent-ui-setup",
    "@kind-ui/code-block",
    "@kind-ui/markdown-table"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/react/markdown.tsx",
      "content": "\"use client\";\n\nimport {\n  type Highlight,\n  type MarkdownBlock,\n  type MdNode,\n  markdownViewPrimitive,\n} from \"@kind-ui/agent-ui/core\";\nimport { createParts } from \"@ui-sdk/react/primitive\";\nimport { type ComponentProps, type CSSProperties, memo, type ReactNode } from \"react\";\nimport { CodeBlock } from \"./code-block\";\nimport { MarkdownTable } from \"./markdown-table\";\n\nconst parts = createParts(markdownViewPrimitive, { root: \"div\" });\n\ntype Context = { highlight: Highlight | undefined; streaming: boolean };\ntype Row = { children: MdNode[] };\ntype Align = Extract<MdNode, { type: \"table\" }>[\"align\"];\n\n// Nodes have no identity but their place in a parsed block, so they're keyed by index.\nconst children = (node: { children: MdNode[] }, context: Context): ReactNode[] =>\n  node.children.map((child, index) => render(child, index, context));\n\nconst row = (node: Row, align: Align, Cell: \"th\" | \"td\", key: number) => (\n  <tr key={key}>\n    {node.children.map((cell, index) => (\n      <Cell key={index} data-align={align[index] ?? undefined}>\n        {cell.type === \"tableCell\" ? children(cell, { highlight: undefined, streaming: false }) : null}\n      </Cell>\n    ))}\n  </tr>\n);\n\nfunction render(node: MdNode, key: number, context: Context): ReactNode {\n  switch (node.type) {\n    case \"text\":\n      return node.value;\n    case \"paragraph\":\n      return <p key={key}>{children(node, context)}</p>;\n    case \"heading\": {\n      const Heading = `h${node.depth}` as const;\n      return <Heading key={key}>{children(node, context)}</Heading>;\n    }\n    case \"emphasis\":\n      return <em key={key}>{children(node, context)}</em>;\n    case \"strong\":\n      return <strong key={key}>{children(node, context)}</strong>;\n    case \"delete\":\n      return <del key={key}>{children(node, context)}</del>;\n    case \"blockquote\":\n      return <blockquote key={key}>{children(node, context)}</blockquote>;\n    case \"inlineCode\":\n      return <code key={key}>{node.value}</code>;\n    case \"code\":\n      return (\n        <CodeBlock\n          key={key}\n          aria-label=\"Code\"\n          code={node.value}\n          language={node.lang}\n          highlight={context.highlight}\n          streaming={context.streaming}\n        />\n      );\n    case \"link\":\n      return node.url === null ? (\n        <span key={key}>{children(node, context)}</span>\n      ) : (\n        <a key={key} href={node.url}>\n          {children(node, context)}\n        </a>\n      );\n    case \"image\":\n      return node.url === null ? (\n        <span key={key}>{node.alt}</span>\n      ) : (\n        <img key={key} src={node.url} alt={node.alt} />\n      );\n    case \"list\":\n      return node.ordered ? (\n        <ol key={key} start={node.start}>\n          {children(node, context)}\n        </ol>\n      ) : (\n        <ul key={key}>{children(node, context)}</ul>\n      );\n    case \"listItem\":\n      return (\n        <li key={key} data-task={node.checked === undefined ? undefined : \"\"}>\n          {node.checked === undefined ? null : (\n            <input type=\"checkbox\" checked={node.checked} disabled readOnly />\n          )}\n          {children(node, context)}\n        </li>\n      );\n    case \"table\": {\n      const [head, ...body] = node.children as Row[];\n      return (\n        <MarkdownTable\n          key={key}\n          aria-label=\"Table\"\n          style={{ \"--kind-markdown-table-columns\": node.align.length } as CSSProperties}\n        >\n          <thead>{head && row(head, node.align, \"th\", 0)}</thead>\n          <tbody>{body.map((bodyRow, index) => row(bodyRow, node.align, \"td\", index))}</tbody>\n        </MarkdownTable>\n      );\n    }\n    case \"thematicBreak\":\n      return <hr key={key} />;\n    case \"break\":\n      return <br key={key} />;\n    default:\n      return null;\n  }\n}\n\n// A final block is the same object on every append, so it never renders again. Code blocks read `highlight`\n// when they ask for tokens, so a new function each render doesn't render them again either.\nconst Block = memo(\n  function Block({ block, ...context }: { block: MarkdownBlock } & Context) {\n    return <div data-block-key={block.key}>{children(block.node as { children: MdNode[] }, context)}</div>;\n  },\n  (before, after) => before.block === after.block && before.streaming === after.streaming,\n);\n\nexport type MarkdownProps = Omit<ComponentProps<typeof parts.Root>, \"children\">;\n\n/** Renders `source` as it streams in, with code blocks and tables. */\nexport function Markdown(props: MarkdownProps) {\n  return (\n    <parts.Root {...props}>\n      {({ blocks, highlight, streaming }) =>\n        blocks.map((block, index) => (\n          // Keyed by place, not `block.key`, which changes with the streaming tail's text, so the tail updates in place.\n          <Block key={index} block={block} highlight={highlight} streaming={streaming && !block.final} />\n        ))\n      }\n    </parts.Root>\n  );\n}\n",
      "type": "registry:ui",
      "target": "@ui/markdown.tsx"
    }
  ],
  "meta": {
    "layer": "react"
  },
  "type": "registry:ui"
}
