{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "markdown-vue",
  "title": "Markdown (Vue)",
  "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/vue@0.1.0-beta.17"
  ],
  "registryDependencies": [
    "@kind-ui/agent-ui-setup",
    "@kind-ui/code-block-vue",
    "@kind-ui/markdown-table-vue"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/vue/markdown.ts",
      "content": "import {\n  type Highlight,\n  type MarkdownBlock,\n  type MdNode,\n  markdownViewPrimitive,\n} from \"@kind-ui/agent-ui/core\";\nimport { createParts } from \"@ui-sdk/vue/primitive\";\nimport { defineComponent, h, inject, type PropType, provide, type VNodeChild } from \"vue\";\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): VNodeChild[] =>\n  node.children.map((child, index) => render(child, index, context));\n\nconst row = (node: Row, align: Align, cell: \"th\" | \"td\", key: number) =>\n  h(\n    \"tr\",\n    { key },\n    node.children.map((child, index) =>\n      h(\n        cell,\n        { key: index, \"data-align\": align[index] ?? undefined },\n        child.type === \"tableCell\" ? children(child, { highlight: undefined, streaming: false }) : undefined,\n      ),\n    ),\n  );\n\nfunction render(node: MdNode, key: number, context: Context): VNodeChild {\n  switch (node.type) {\n    case \"text\":\n      return node.value;\n    case \"paragraph\":\n      return h(\"p\", { key }, children(node, context));\n    case \"heading\":\n      return h(`h${node.depth}`, { key }, children(node, context));\n    case \"emphasis\":\n      return h(\"em\", { key }, children(node, context));\n    case \"strong\":\n      return h(\"strong\", { key }, children(node, context));\n    case \"delete\":\n      return h(\"del\", { key }, children(node, context));\n    case \"blockquote\":\n      return h(\"blockquote\", { key }, children(node, context));\n    case \"inlineCode\":\n      return h(\"code\", { key }, node.value);\n    case \"code\":\n      return h(CodeBlock, {\n        key,\n        \"aria-label\": \"Code\",\n        code: node.value,\n        language: node.lang,\n        highlight: context.highlight,\n        streaming: context.streaming,\n      });\n    case \"link\":\n      return node.url === null\n        ? h(\"span\", { key }, children(node, context))\n        : h(\"a\", { key, href: node.url }, children(node, context));\n    case \"image\":\n      return node.url === null\n        ? h(\"span\", { key }, node.alt)\n        : h(\"img\", { key, src: node.url, alt: node.alt });\n    case \"list\":\n      return node.ordered\n        ? h(\"ol\", { key, start: node.start }, children(node, context))\n        : h(\"ul\", { key }, children(node, context));\n    case \"listItem\":\n      return h(\"li\", { key, \"data-task\": node.checked === undefined ? undefined : \"\" }, [\n        node.checked === undefined\n          ? null\n          : h(\"input\", { type: \"checkbox\", checked: node.checked, disabled: true, readonly: true }),\n        ...children(node, context),\n      ]);\n    case \"table\": {\n      const [head, ...body] = node.children as Row[];\n      return h(\n        MarkdownTable,\n        { key, \"aria-label\": \"Table\", style: { \"--kind-markdown-table-columns\": node.align.length } },\n        () => [\n          h(\"thead\", head ? [row(head, node.align, \"th\", 0)] : []),\n          h(\n            \"tbody\",\n            body.map((bodyRow, index) => row(bodyRow, node.align, \"td\", index)),\n          ),\n        ],\n      );\n    }\n    case \"thematicBreak\":\n      return h(\"hr\", { key });\n    case \"break\":\n      return h(\"br\", { key });\n    default:\n      return null;\n  }\n}\n\ntype HighlightRef = { current: Highlight | undefined };\nconst HIGHLIGHT = Symbol(\"kind-markdown-highlight\");\n\n// A final block is the same object on every append, so it never renders again. `highlight` comes through a holder,\n// not a prop: code blocks read it when they ask for tokens, so a new function each render doesn't render them again.\nconst Block = defineComponent({\n  name: \"MarkdownBlock\",\n  props: {\n    block: { type: Object as PropType<MarkdownBlock>, required: true },\n    streaming: { type: Boolean, required: true },\n  },\n  setup(props) {\n    const highlight = inject(HIGHLIGHT) as HighlightRef;\n    return () =>\n      h(\n        \"div\",\n        { \"data-block-key\": props.block.key },\n        children(props.block.node as { children: MdNode[] }, {\n          highlight: highlight.current,\n          streaming: props.streaming,\n        }),\n      );\n  },\n});\n\ntype MarkdownSlotState = { blocks: MarkdownBlock[]; highlight: Highlight | undefined; streaming: boolean };\n\n/** Renders `source` as it streams in, with code blocks and tables. */\nexport const Markdown = defineComponent({\n  name: \"Markdown\",\n  inheritAttrs: false,\n  setup(_, { attrs, slots }) {\n    const highlight: HighlightRef = { current: undefined };\n    provide(HIGHLIGHT, highlight);\n    return () =>\n      h(parts.Root, attrs, {\n        ...slots,\n        default: ({ state }: { state: MarkdownSlotState }) => {\n          highlight.current = state.highlight;\n          // Keyed by place, not `block.key`, which changes with the streaming tail's text, so the tail updates in place.\n          return state.blocks.map((block, index) =>\n            h(Block, { key: index, block, streaming: state.streaming && !block.final }),\n          );\n        },\n      });\n  },\n});\n",
      "type": "registry:ui",
      "target": "@ui/vue/markdown.ts"
    }
  ],
  "meta": {
    "layer": "vue"
  },
  "type": "registry:ui"
}
