{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "markdown-code-block",
  "title": "Markdown Code Block",
  "description": "Fence renderer for the markdown item: the shadcn/ui code block with a language title and copy, highlighted with shiki. A fence that streams is tokenized line by line, so finished lines are never re-highlighted.",
  "dependencies": [
    "cn",
    "shiki"
  ],
  "registryDependencies": [
    "litingyes/blocove/markdown-parse",
    "code-block"
  ],
  "files": [
    {
      "path": "src/registry/blocove/lib/markdown-shiki.tsx",
      "content": "/* eslint-disable react/no-array-index-key -- tokens are positional within their line. */\n\nimport type { ReactNode } from 'react'\nimport type { GrammarState, HighlighterCore } from 'shiki/core'\nimport { createHighlighterCore } from 'shiki/core'\nimport { createJavaScriptRegexEngine } from 'shiki/engine/javascript'\n\n/**\n * Shiki tokenizer for the markdown code fences.\n *\n * It mirrors the tokenizer the docs panels use, with one difference that\n * matters here: `highlight` returns the grammar state after the last line, so a\n * fence that grows line by line only tokenizes the new lines instead of the\n * whole block on every token.\n */\n\n// Languages load the first time they are used, so a page that renders one fence\n// does not pay for every grammar.\nconst languages = {\n  bash: () => import('shiki/langs/shellscript.mjs'),\n  css: () => import('shiki/langs/css.mjs'),\n  diff: () => import('shiki/langs/diff.mjs'),\n  html: () => import('shiki/langs/html.mjs'),\n  javascript: () => import('shiki/langs/javascript.mjs'),\n  json: () => import('shiki/langs/json.mjs'),\n  jsx: () => import('shiki/langs/jsx.mjs'),\n  markdown: () => import('shiki/langs/markdown.mjs'),\n  python: () => import('shiki/langs/python.mjs'),\n  sql: () => import('shiki/langs/sql.mjs'),\n  tsx: () => import('shiki/langs/tsx.mjs'),\n  typescript: () => import('shiki/langs/typescript.mjs'),\n  yaml: () => import('shiki/langs/yaml.mjs'),\n}\n\nconst aliases: Record<string, keyof typeof languages> = {\n  js: 'javascript',\n  md: 'markdown',\n  py: 'python',\n  sh: 'bash',\n  shell: 'bash',\n  shellscript: 'bash',\n  ts: 'typescript',\n  yml: 'yaml',\n  zsh: 'bash',\n}\n\nconst themes = {\n  light: 'github-light',\n  dark: 'github-dark',\n}\n\nlet highlighter: Promise<HighlighterCore> | undefined\n\nfunction getHighlighter() {\n  highlighter ??= createHighlighterCore({\n    themes: [\n      import('shiki/themes/github-light.mjs'),\n      import('shiki/themes/github-dark.mjs'),\n    ],\n    langs: [],\n    engine: createJavaScriptRegexEngine({ forgiving: true }),\n  })\n\n  return highlighter\n}\n\nfunction resolveLanguage(language: string): keyof typeof languages | null {\n  const name = language.toLowerCase()\n\n  if (name in languages) {\n    return name as keyof typeof languages\n  }\n\n  return aliases[name] ?? null\n}\n\n/** A highlighted block: one entry per line, each entry holding the token spans. */\nexport interface MarkdownFenceHighlight {\n  lines: ReactNode[][]\n  state?: GrammarState\n}\n\nexport interface MarkdownFenceHighlighter {\n  /** Highlights more code, continuing from the grammar state of the last call. */\n  highlight: (code: string, state?: GrammarState) => MarkdownFenceHighlight\n}\n\n/**\n * Loads a fence highlighter for `language`. A language that is not listed\n * resolves to `null`, which the renderer shows as plain text.\n */\nexport async function loadMarkdownFenceHighlighter(language: string): Promise<MarkdownFenceHighlighter | null> {\n  const name = resolveLanguage(language)\n\n  if (!name) {\n    return null\n  }\n\n  const shiki = await getHighlighter()\n\n  if (!shiki.getLoadedLanguages().includes(name)) {\n    await shiki.loadLanguage(languages[name])\n  }\n\n  return {\n    highlight(code, state) {\n      const tokens = shiki.codeToTokens(code, {\n        lang: name,\n        themes,\n        defaultColor: false,\n        grammarState: state,\n      }).tokens\n\n      return {\n        lines: tokens.map(line =>\n          line.map((token, index) => (\n            <span\n              key={index}\n              style={token.htmlStyle as React.CSSProperties}\n              className=\"text-(--shiki-light) dark:text-(--shiki-dark)\"\n            >\n              {token.content}\n            </span>\n          )),\n        ),\n        state: shiki.getLastGrammarState(tokens),\n      }\n    },\n  }\n}\n",
      "type": "registry:lib",
      "target": "lib/blocove/markdown-shiki.tsx"
    },
    {
      "path": "src/registry/blocove/ui/markdown/code-block.tsx",
      "content": "/* eslint-disable react/no-array-index-key -- a fence only ever appends lines, so the line index is the identity. */\n\nimport type { ReactNode } from 'react'\nimport type { GrammarState } from 'shiki/core'\nimport type { MarkdownCodeProps } from '@/lib/blocove/markdown-parse'\nimport type { MarkdownFenceHighlighter } from '@/lib/blocove/markdown-shiki'\nimport { cn } from 'cn'\nimport { useEffect, useMemo, useState } from 'react'\nimport {\n  CodeBlock,\n  CodeBlockActions,\n  CodeBlockContent,\n  CodeBlockCopyButton,\n  CodeBlockHeader,\n  CodeBlockLine,\n  CodeBlockTitle,\n} from '@/components/ui/code-block'\nimport { loadMarkdownFenceHighlighter } from '@/lib/blocove/markdown-shiki'\n\ninterface HighlightState {\n  highlighter: MarkdownFenceHighlighter | null\n  /** The code already highlighted into `lines`. */\n  text: string\n  lines: ReactNode[][]\n  state?: GrammarState\n}\n\n/**\n * Highlights a fence incrementally. While the fence streams, only the lines\n * that completed since the last render are tokenized; the last line is still\n * being written, so it is tokenized on every render, starting from the grammar\n * state of the line before it.\n */\nfunction useFenceLines(code: string, language: string): ReactNode[][] {\n  const [loaded, setLoaded] = useState<{\n    language: string\n    highlighter: MarkdownFenceHighlighter | null\n  } | null>(null)\n  const highlighter = loaded?.language === language\n    ? loaded.highlighter\n    : null\n\n  useEffect(() => {\n    let cancelled = false\n\n    loadMarkdownFenceHighlighter(language)\n      .then((next) => {\n        if (!cancelled) {\n          setLoaded({ language, highlighter: next })\n        }\n      })\n      // An unknown or unavailable language renders as plain text; the failure is\n      // not retried on every render.\n      .catch(() => {\n        if (!cancelled) {\n          setLoaded({ language, highlighter: null })\n        }\n      })\n\n    return () => {\n      cancelled = true\n    }\n  }, [language])\n\n  const [done, setDone] = useState<HighlightState>({\n    highlighter: null,\n    text: '',\n    lines: [],\n  })\n\n  let next = done\n\n  if (next.highlighter !== highlighter || !code.startsWith(next.text)) {\n    next = { highlighter, text: '', lines: [] }\n  }\n\n  const end = code.lastIndexOf('\\n') + 1\n\n  if (highlighter && end > next.text.length) {\n    const result = highlighter.highlight(code.slice(next.text.length, end - 1), next.state)\n\n    next = {\n      highlighter,\n      text: code.slice(0, end),\n      lines: [\n        ...next.lines,\n        ...result.lines,\n      ],\n      state: result.state,\n    }\n  }\n\n  if (next !== done) {\n    setDone(next)\n  }\n\n  const tail = code.slice(end)\n\n  return useMemo(() => {\n    if (!highlighter) {\n      return code.split('\\n').map(line => [line])\n    }\n\n    return [...next.lines, highlighter.highlight(tail, next.state).lines[0]]\n  }, [code, highlighter, next, tail])\n}\n\nexport interface MarkdownCodeBlockProps extends MarkdownCodeProps {\n  className?: string\n}\n\n/**\n * Fence renderer for the markdown shell, highlighted with shiki and framed by the\n * shadcn/ui code block.\n *\n * Register it through the shell's capability map:\n *\n * ```tsx\n * <Markdown source={source} components={{ code: MarkdownCodeBlock }} />\n * ```\n *\n * The card carries `data-language` on its root; the header shows the language and\n * copies the current source, including while the fence streams. The card is a\n * typeset component, so typeset supplies the block spacing and the code block\n * itself styles the fence.\n */\nexport function MarkdownCodeBlock({\n  language,\n  code,\n  streaming,\n  className,\n}: MarkdownCodeBlockProps) {\n  const lines = useFenceLines(code, language)\n\n  return (\n    <CodeBlock\n      data-language={language || undefined}\n      data-streaming={streaming || undefined}\n      className={cn(className)}\n    >\n      <CodeBlockHeader>\n        <CodeBlockTitle className=\"text-muted-foreground\">{language || 'text'}</CodeBlockTitle>\n        <CodeBlockActions>\n          <CodeBlockCopyButton\n            value={code}\n            aria-label={language\n              ? `Copy ${language} code`\n              : 'Copy code'}\n          />\n        </CodeBlockActions>\n      </CodeBlockHeader>\n      <CodeBlockContent>\n        {lines.map((line, index) => (\n          <CodeBlockLine key={index}>\n            {line.length > 0\n              ? line\n              : '\\n'}\n          </CodeBlockLine>\n        ))}\n      </CodeBlockContent>\n    </CodeBlock>\n  )\n}\n",
      "type": "registry:ui",
      "target": "components/blocove/markdown/code-block.tsx"
    }
  ],
  "categories": [
    "markdown",
    "capability"
  ],
  "type": "registry:ui"
}