{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "markdown-solid",
  "title": "Markdown (Solid)",
  "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/solid@0.1.0-beta.17"
  ],
  "registryDependencies": [
    "@kind-ui/agent-ui-setup",
    "@kind-ui/code-block-solid",
    "@kind-ui/markdown-table-solid"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/solid/markdown.tsx",
      "content": "import {\n  type Highlight,\n  type MarkdownBlock,\n  type MdNode,\n  markdownViewPrimitive,\n} from \"@kind-ui/agent-ui/core\";\nimport { type ComponentProps, Dynamic, type JSX } from \"@solidjs/web\";\nimport { createParts } from \"@ui-sdk/solid/primitive\";\nimport { createMemo, For, untrack } from \"solid-js\";\nimport { CodeBlock } from \"./code-block\";\nimport { MarkdownTable } from \"./markdown-table\";\n\nconst parts = createParts(markdownViewPrimitive, { root: \"div\" });\n\ntype Context = { readonly highlight: Highlight | undefined; readonly streaming: boolean };\ntype Parent = { children: MdNode[] };\ntype Align = Extract<MdNode, { type: \"table\" }>[\"align\"];\ntype Of<T extends MdNode[\"type\"]> = MdNode & { type: T };\n\nconst plain: Context = { highlight: undefined, streaming: false };\n\n// Nodes have no identity but their place in a parsed block, so they're keyed by place: a streamed token updates\n// the text it lands in, and a node is built again only when its kind changes.\nfunction Children(props: { node: Parent; context: Context }) {\n  return (\n    <For each={props.node.children} keyed={false}>\n      {(child) => <Node node={child()} context={props.context} />}\n    </For>\n  );\n}\n\nfunction Row(props: { node: Parent; align: Align; header: boolean }) {\n  return (\n    <tr>\n      <For each={props.node.children} keyed={false}>\n        {(cell, index) => (\n          <Dynamic component={props.header ? \"th\" : \"td\"} data-align={props.align[index] ?? undefined}>\n            {cell().type === \"tableCell\" ? <Children node={cell() as Parent} context={plain} /> : null}\n          </Dynamic>\n        )}\n      </For>\n    </tr>\n  );\n}\n\nfunction kindOf(node: MdNode): string {\n  switch (node.type) {\n    case \"link\":\n    case \"image\":\n      return `${node.type}:${node.url === null}`;\n    case \"list\":\n      return `list:${node.ordered}`;\n    case \"listItem\":\n      return `listItem:${node.checked === undefined}`;\n    default:\n      return node.type;\n  }\n}\n\nfunction Node(props: { node: MdNode; context: Context }) {\n  const kind = createMemo(() => kindOf(props.node));\n  // Tracks the kind alone: a node that changes kind is built again, one that keeps it updates in place.\n  const element = createMemo(() => {\n    const current = kind();\n    return untrack(() => view(current, props));\n  });\n  return <>{element()}</>;\n}\n\nfunction view(kind: string, props: { node: MdNode; context: Context }): JSX.Element {\n  const node = <T extends MdNode[\"type\"]>() => props.node as Of<T>;\n  const children = () => <Children node={props.node as Parent} context={props.context} />;\n  switch (kind) {\n    case \"text\":\n      return <>{node<\"text\">().value}</>;\n    case \"paragraph\":\n      return <p>{children()}</p>;\n    case \"heading\":\n      return <Dynamic component={`h${node<\"heading\">().depth}`}>{children()}</Dynamic>;\n    case \"emphasis\":\n      return <em>{children()}</em>;\n    case \"strong\":\n      return <strong>{children()}</strong>;\n    case \"delete\":\n      return <del>{children()}</del>;\n    case \"blockquote\":\n      return <blockquote>{children()}</blockquote>;\n    case \"inlineCode\":\n      return <code>{node<\"inlineCode\">().value}</code>;\n    case \"code\":\n      return (\n        <CodeBlock\n          aria-label=\"Code\"\n          code={node<\"code\">().value}\n          language={node<\"code\">().lang}\n          highlight={props.context.highlight}\n          streaming={props.context.streaming}\n        />\n      );\n    case \"link:true\":\n      return <span>{children()}</span>;\n    case \"link:false\":\n      return <a href={node<\"link\">().url ?? undefined}>{children()}</a>;\n    case \"image:true\":\n      return <span>{node<\"image\">().alt}</span>;\n    case \"image:false\":\n      return <img src={node<\"image\">().url ?? undefined} alt={node<\"image\">().alt} />;\n    case \"list:true\":\n      return <ol start={node<\"list\">().start}>{children()}</ol>;\n    case \"list:false\":\n      return <ul>{children()}</ul>;\n    case \"listItem:true\":\n      return <li>{children()}</li>;\n    case \"listItem:false\":\n      return (\n        <li data-task=\"\">\n          <input type=\"checkbox\" checked={node<\"listItem\">().checked} disabled readonly />\n          {children()}\n        </li>\n      );\n    case \"table\":\n      return (\n        <MarkdownTable\n          aria-label=\"Table\"\n          style={{ \"--kind-markdown-table-columns\": node<\"table\">().align.length }}\n        >\n          <thead>\n            {node<\"table\">().children[0] && (\n              <Row node={node<\"table\">().children[0] as Parent} align={node<\"table\">().align} header />\n            )}\n          </thead>\n          <tbody>\n            <For each={node<\"table\">().children.slice(1)} keyed={false}>\n              {(row) => <Row node={row() as Parent} align={node<\"table\">().align} header={false} />}\n            </For>\n          </tbody>\n        </MarkdownTable>\n      );\n    case \"thematicBreak\":\n      return <hr />;\n    case \"break\":\n      return <br />;\n    default:\n      return null;\n  }\n}\n\nfunction Block(props: { block: MarkdownBlock; context: Context }) {\n  return (\n    <div data-block-key={props.block.key}>\n      <Children node={props.block.node as Parent} context={props.context} />\n    </div>\n  );\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      {(state) => (\n        // Keyed by place, not `block.key`, which changes with the streaming tail's text, so the tail updates in place.\n        <For each={state.blocks} keyed={false}>\n          {(block) => (\n            <Block\n              block={block()}\n              context={{\n                get highlight() {\n                  return state.highlight;\n                },\n                get streaming() {\n                  return state.streaming && !block().final;\n                },\n              }}\n            />\n          )}\n        </For>\n      )}\n    </parts.Root>\n  );\n}\n",
      "type": "registry:ui",
      "target": "@ui/solid/markdown.tsx"
    }
  ],
  "meta": {
    "layer": "solid"
  },
  "type": "registry:ui"
}
