{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "markdown-web",
  "title": "Markdown (Web Components)",
  "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/web@0.1.0-beta.17"
  ],
  "registryDependencies": [
    "@kind-ui/agent-ui-setup",
    "@kind-ui/code-block-web",
    "@kind-ui/markdown-table-web"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/web/markdown.ts",
      "content": "import {\n  type Highlight,\n  type MarkdownBlock,\n  type MarkdownOptions,\n  type MarkdownState,\n  type MdNode,\n  markdownViewPrimitive,\n} from \"@kind-ui/agent-ui/core\";\nimport { createParts } from \"@ui-sdk/web\";\nimport type { AttributeProperties, UiElement } from \"@ui-sdk/web/events\";\nimport { defineCodeBlock } from \"./code-block\";\nimport { defineMarkdownTable } from \"./markdown-table\";\n\ntype Context = { highlight: Highlight | undefined; streaming: boolean; cell: \"th\" | \"td\" };\n\nconst tags: Partial<Record<MdNode[\"type\"], string>> = {\n  paragraph: \"p\",\n  emphasis: \"em\",\n  strong: \"strong\",\n  delete: \"del\",\n  blockquote: \"blockquote\",\n  listItem: \"li\",\n  inlineCode: \"code\",\n  code: \"kind-code-block\",\n  table: \"kind-markdown-table\",\n  tableRow: \"tr\",\n  thematicBreak: \"hr\",\n  break: \"br\",\n};\n\nfunction tagOf(node: MdNode, context: Context): string | undefined {\n  switch (node.type) {\n    case \"heading\":\n      return `h${node.depth}`;\n    case \"list\":\n      return node.ordered ? \"ol\" : \"ul\";\n    case \"link\":\n      return node.url === null ? \"span\" : \"a\";\n    case \"image\":\n      return node.url === null ? \"span\" : \"img\";\n    case \"tableCell\":\n      return context.cell;\n    default:\n      return tags[node.type];\n  }\n}\n\nconst attribute = (el: Element, name: string, value: string | undefined) => {\n  if (value === undefined) el.removeAttribute(name);\n  else if (el.getAttribute(name) !== value) el.setAttribute(name, value);\n};\n\nfunction patchChildren(parent: Element, nodes: MdNode[], context: Context, offset = 0): void {\n  nodes.forEach((node, index) => {\n    const current = parent.childNodes[offset + index];\n    if (current && patch(current, node, context)) return;\n    const next = create(node, context);\n    if (current) current.replaceWith(next);\n    else parent.append(next);\n  });\n  while (parent.childNodes.length > offset + nodes.length) parent.lastChild?.remove();\n}\n\nfunction patch(dom: ChildNode, node: MdNode, context: Context): boolean {\n  if (node.type === \"text\") {\n    if (!(dom instanceof Text)) return false;\n    if (dom.data !== node.value) dom.data = node.value;\n    return true;\n  }\n  if (!(dom instanceof Element) || dom.localName !== tagOf(node, context)) return false;\n  update(dom, node, context);\n  return true;\n}\n\nfunction create(node: MdNode, context: Context): ChildNode {\n  if (node.type === \"text\") return document.createTextNode(node.value);\n  const el = document.createElement(tagOf(node, context) as string);\n  update(el, node, context);\n  return el;\n}\n\nfunction update(el: Element, node: MdNode, context: Context): void {\n  switch (node.type) {\n    case \"inlineCode\":\n      if (el.textContent !== node.value) el.textContent = node.value;\n      return;\n    case \"code\": {\n      attribute(el, \"aria-label\", \"Code\");\n      attribute(el, \"code\", node.value);\n      attribute(el, \"language\", node.lang);\n      attribute(el, \"streaming\", context.streaming ? \"\" : undefined);\n      const block = el as HTMLElement & { highlight?: Highlight };\n      if (block.highlight !== context.highlight) block.highlight = context.highlight;\n      return;\n    }\n    case \"link\":\n      if (node.url !== null) attribute(el, \"href\", node.url);\n      patchChildren(el, node.children, context);\n      return;\n    case \"image\":\n      if (node.url === null) el.textContent = node.alt;\n      else {\n        attribute(el, \"src\", node.url);\n        attribute(el, \"alt\", node.alt);\n      }\n      return;\n    case \"list\":\n      attribute(el, \"start\", node.ordered && node.start !== undefined ? String(node.start) : undefined);\n      patchChildren(el, node.children, context);\n      return;\n    case \"listItem\": {\n      const task = node.checked !== undefined;\n      let box = el.firstChild instanceof HTMLInputElement ? el.firstChild : undefined;\n      if (task && !box) {\n        box = Object.assign(document.createElement(\"input\"), { type: \"checkbox\", disabled: true });\n        el.prepend(box);\n      } else if (!task && box) {\n        box.remove();\n        box = undefined;\n      }\n      if (box) box.checked = node.checked === true;\n      attribute(el, \"data-task\", task ? \"\" : undefined);\n      patchChildren(el, node.children, context, box ? 1 : 0);\n      return;\n    }\n    case \"table\": {\n      attribute(el, \"aria-label\", \"Table\");\n      (el as HTMLElement).style.setProperty(\"--kind-markdown-table-columns\", String(node.align.length));\n      let table = el.querySelector(\"table\");\n      if (!table) {\n        table = el.appendChild(document.createElement(\"table\"));\n        table.append(document.createElement(\"thead\"), document.createElement(\"tbody\"));\n      }\n      const [head, ...body] = node.children;\n      patchChildren(table.tHead as HTMLElement, head ? [head] : [], { ...context, cell: \"th\" });\n      patchChildren(table.tBodies[0] as HTMLElement, body, { ...context, cell: \"td\" });\n      for (const row of table.rows) {\n        for (const [index, cell] of [...row.cells].entries())\n          attribute(cell, \"data-align\", node.align[index] ?? undefined);\n      }\n      return;\n    }\n    case \"text\":\n    case \"thematicBreak\":\n    case \"break\":\n      return;\n    default:\n      patchChildren(el, node.children, context);\n  }\n}\n\n/** `<kind-markdown source=\"…\" streaming>` renders its blocks into itself. Set `highlight` as a property. */\nexport function defineMarkdown(): void {\n  if (customElements.get(\"kind-markdown\")) return;\n  defineCodeBlock();\n  defineMarkdownTable();\n  createParts(markdownViewPrimitive, {\n    root: {\n      tag: \"kind-markdown\",\n      render: (el, instance) => {\n        // Content written before upgrade, like a fallback, makes way for the blocks.\n        for (const child of [...el.childNodes]) {\n          if (!(child instanceof HTMLElement && child.dataset.blockKey)) child.remove();\n        }\n        let drawn: MarkdownBlock[] = [];\n        const draw = () => {\n          const { blocks, highlight, streaming } = instance.get() as MarkdownState;\n          if (blocks === drawn) return;\n          // A final block is the same object on every append, so only the blocks after it are patched.\n          blocks.forEach((block, index) => {\n            if (block === drawn[index]) return;\n            const wrapper = el.children[index] ?? el.appendChild(document.createElement(\"div\"));\n            attribute(wrapper, \"data-block-key\", block.key);\n            const context = { highlight, streaming: streaming && !block.final, cell: \"td\" } as const;\n            patchChildren(wrapper, (block.node as { children: MdNode[] }).children, context);\n          });\n          while (el.children.length > blocks.length) el.lastElementChild?.remove();\n          drawn = blocks;\n        };\n        draw();\n        return instance.subscribe(draw);\n      },\n    },\n  });\n}\n\nexport interface KindMarkdownElement\n  extends UiElement<Record<never, never>>,\n    AttributeProperties<typeof markdownViewPrimitive, \"engine\" | \"highlight\"> {\n  highlight?: MarkdownOptions[\"highlight\"];\n  engine?: MarkdownOptions[\"engine\"];\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-markdown\": KindMarkdownElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/markdown.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
