{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "markdown-mermaid",
  "title": "Markdown Mermaid",
  "description": "Mermaid renderer for the markdown item, drawn with Pele. A fence renders as a diagram once it closes; while the source streams, or when Pele rejects it, the fence stays a code block.",
  "dependencies": [
    "cn",
    "pele@^1.13.0"
  ],
  "registryDependencies": [
    "litingyes/blocove/markdown-parse",
    "code-block"
  ],
  "files": [
    {
      "path": "src/registry/blocove/lib/markdown-mermaid.ts",
      "content": "import type { MountOptions } from 'pele'\n\nexport interface MarkdownMermaidInstance {\n  destroy: () => void\n}\n\nexport interface MarkdownMermaidOptions {\n  onError?: (cause: unknown) => void\n}\n\nexport interface MarkdownMermaidModule {\n  mountAsync: (\n    element: HTMLElement,\n    source: string,\n    options: MarkdownMermaidOptions & Omit<MountOptions, 'onError' | 'onRender' | 'sanitize'>,\n  ) => Promise<MarkdownMermaidInstance>\n}\n\n/** Turns a React id into an SVG id prefix Pele accepts (`[A-Za-z0-9_-]`). */\nexport function markdownMermaidIdPrefix(id: string): string {\n  return `diagram-${id.replace(/[^\\w-]/g, '')}`\n}\n\n/**\n * The static mount options the item owns: a unique id prefix per diagram, zoom\n * controls when the diagram shrinks, and no images, so model-authored image URLs\n * cannot become tracking pixels.\n */\nexport function buildMarkdownMermaidOptions(\n  idPrefix: string,\n): Omit<MountOptions, 'onError' | 'onRender' | 'sanitize'> {\n  return { idPrefix, zoom: 'auto', images: false }\n}\n\nlet modulePromise: Promise<MarkdownMermaidModule> | undefined\n\n/**\n * Loads `pele/lazy` once and reuses the module. The lazy entry point loads each\n * diagram type on first use, so a page that never renders a diagram does not pay\n * for the renderer.\n */\nexport function loadMarkdownMermaid(): Promise<MarkdownMermaidModule> {\n  modulePromise ??= import('pele/lazy') as unknown as Promise<MarkdownMermaidModule>\n\n  return modulePromise\n}\n",
      "type": "registry:lib",
      "target": "lib/blocove/markdown-mermaid.ts"
    },
    {
      "path": "src/registry/blocove/ui/markdown/mermaid.tsx",
      "content": "import type { CSSProperties } from 'react'\nimport type { MarkdownMermaidInstance } from '@/lib/blocove/markdown-mermaid'\nimport type { MarkdownCodeProps } from '@/lib/blocove/markdown-parse'\nimport { cn } from 'cn'\nimport { useEffect, useId, useRef, useState, useSyncExternalStore } from 'react'\nimport {\n  CodeBlock,\n  CodeBlockActions,\n  CodeBlockContent,\n  CodeBlockCopyButton,\n  CodeBlockHeader,\n  CodeBlockTitle,\n} from '@/components/ui/code-block'\nimport {\n  buildMarkdownMermaidOptions,\n  loadMarkdownMermaid,\n  markdownMermaidIdPrefix,\n} from '@/lib/blocove/markdown-mermaid'\n\nexport interface MarkdownMermaidProps extends MarkdownCodeProps {\n  className?: string\n}\n\n// Pele paints with CSS variables, so mapping them onto the shadcn tokens makes\n// the diagram follow the theme without re-rendering. The series palette repeats\n// the five chart colors for the eighth Pele categories.\nconst mermaidTheme = {\n  '--pele-bg': 'var(--background)',\n  '--pele-fg': 'var(--foreground)',\n  '--pele-muted': 'var(--muted-foreground)',\n  '--pele-line': 'var(--border)',\n  '--pele-surface': 'var(--muted)',\n  '--pele-surface-alt': 'var(--muted)',\n  '--pele-border': 'var(--border)',\n  '--pele-accent': 'var(--primary)',\n  '--pele-critical': 'var(--destructive)',\n  '--pele-error': 'var(--destructive)',\n  '--pele-radius': 'var(--radius)',\n  '--pele-series-1': 'var(--chart-1)',\n  '--pele-series-2': 'var(--chart-2)',\n  '--pele-series-3': 'var(--chart-3)',\n  '--pele-series-4': 'var(--chart-4)',\n  '--pele-series-5': 'var(--chart-5)',\n  '--pele-series-6': 'var(--chart-1)',\n  '--pele-series-7': 'var(--chart-2)',\n  '--pele-series-8': 'var(--chart-3)',\n} as CSSProperties\n\nconst subscribeToNothing = () => () => {}\nconst getClientSnapshot = () => true\nconst getServerSnapshot = () => false\n\n/** The client boundary without a state write in an effect. */\nfunction useMounted(): boolean {\n  return useSyncExternalStore(subscribeToNothing, getClientSnapshot, getServerSnapshot)\n}\n\nfunction errorMessage(cause: unknown): string {\n  return cause instanceof Error && cause.message !== ''\n    ? cause.message\n    : 'The diagram could not be rendered.'\n}\n\n/**\n * Mermaid renderer for the markdown shell, drawn with Pele.\n *\n * Register it through the shell's fence map:\n *\n * ```tsx\n * <Markdown source={source} components={{ fences: { mermaid: MarkdownMermaid } }} />\n * ```\n *\n * While the fence is open, or when Pele rejects it, the fence stays the source\n * code block. Once the closing fence arrives (or the source is final), the\n * diagram mounts client-side: Pele reads the container width, follows resizes\n * and owns the error display. Colors come from the shadcn CSS variables mapped\n * onto `--pele-*` in the card, so a theme switch does not re-render the diagram.\n */\nexport function MarkdownMermaid({\n  code,\n  streaming,\n  complete,\n  className,\n}: MarkdownMermaidProps) {\n  const mounted = useMounted()\n  const [ready, setReady] = useState(false)\n  const [error, setError] = useState<string | null>(null)\n  const containerRef = useRef<HTMLDivElement>(null)\n  const id = useId()\n  const idPrefix = markdownMermaidIdPrefix(id)\n\n  useEffect(() => {\n    if (!complete || !mounted) {\n      return\n    }\n\n    let cancelled = false\n\n    loadMarkdownMermaid()\n      .then(() => {\n        if (!cancelled) {\n          setReady(true)\n        }\n      })\n      .catch((cause: unknown) => {\n        if (!cancelled) {\n          setError(errorMessage(cause))\n        }\n      })\n\n    return () => {\n      cancelled = true\n    }\n  }, [complete, mounted])\n\n  useEffect(() => {\n    if (!ready || !complete || error) {\n      return\n    }\n\n    const element = containerRef.current\n\n    if (!element) {\n      return\n    }\n\n    let cancelled = false\n    let instance: MarkdownMermaidInstance | undefined\n\n    element.replaceChildren()\n\n    loadMarkdownMermaid()\n      .then(pele => pele.mountAsync(element, code, {\n        ...buildMarkdownMermaidOptions(idPrefix),\n        onError: (cause: unknown) => {\n          if (!cancelled) {\n            setError(errorMessage(cause))\n          }\n        },\n      }))\n      .then((next) => {\n        if (cancelled) {\n          next.destroy()\n        }\n        else {\n          instance = next\n        }\n      })\n      .catch((cause: unknown) => {\n        if (!cancelled) {\n          setError(errorMessage(cause))\n        }\n      })\n\n    return () => {\n      cancelled = true\n      instance?.destroy()\n      element.replaceChildren()\n    }\n  }, [ready, complete, code, idPrefix, error])\n\n  const failed = error !== null\n  const showSource = !complete || !mounted || !ready || failed\n\n  return (\n    <CodeBlock\n      data-language=\"mermaid\"\n      data-streaming={streaming || undefined}\n      className={cn(className)}\n      style={mermaidTheme}\n    >\n      <CodeBlockHeader>\n        <CodeBlockTitle className=\"text-muted-foreground\">mermaid</CodeBlockTitle>\n        <CodeBlockActions>\n          <CodeBlockCopyButton value={code} aria-label=\"Copy mermaid source\" />\n        </CodeBlockActions>\n      </CodeBlockHeader>\n      {showSource\n        ? (\n            <>\n              <CodeBlockContent>{code}</CodeBlockContent>\n              {failed\n                ? (\n                    <p role=\"status\" className=\"px-3 pb-3 text-xs text-destructive\">\n                      {error}\n                    </p>\n                  )\n                : null}\n            </>\n          )\n        : (\n            <div\n              ref={containerRef}\n              data-slot=\"markdown-mermaid-surface\"\n              className=\"min-h-24 p-3\"\n            />\n          )}\n    </CodeBlock>\n  )\n}\n",
      "type": "registry:ui",
      "target": "components/blocove/markdown/mermaid.tsx"
    }
  ],
  "categories": [
    "markdown",
    "capability"
  ],
  "type": "registry:ui"
}