{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "code-block",
  "title": "Code Block",
  "description": "Composable Astro code blocks with line numbers, diff states, copy controls, and server-side syntax highlighting.",
  "dependencies": [
    "@lucide/astro",
    "class-variance-authority",
    "cn",
    "shiki"
  ],
  "registryDependencies": [
    "@gnostic/button",
    "@gnostic/copy-button"
  ],
  "files": [
    {
      "path": "packages/ui/src/components/ui/code-block/code-block-actions.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\nimport { cn } from \"cn\"\n\ntype Props = HTMLAttributes<\"div\">\nconst { class: className, ...props } = Astro.props\n---\n\n<div\n  data-slot=\"code-block-actions\"\n  class={cn(\"-my-1 ms-auto -me-1.5 flex shrink-0 items-center gap-1 [[data-slot=code-block]>&]:absolute [[data-slot=code-block]>&]:end-3 [[data-slot=code-block]>&]:top-3 [[data-slot=code-block]>&]:z-10 [[data-slot=code-block]>&]:m-0 [[data-slot=code-block]>&]:opacity-0 [[data-slot=code-block]>&]:transition-opacity [[data-slot=code-block]>&]:group-focus-within/code-block:opacity-100 [[data-slot=code-block]>&]:group-hover/code-block:opacity-100 group-data-[size=sm]/code-block:[[data-slot=code-block]>&]:end-2 group-data-[size=sm]/code-block:[[data-slot=code-block]>&]:top-2 [[data-slot=code-block]>&]:pointer-coarse:opacity-100 [[data-slot=code-block]>&]:[&_button]:bg-background [[data-slot=code-block]>&]:[&_button]:ring-1 [[data-slot=code-block]>&]:[&_button]:ring-foreground/10\", className)}\n  {...props}\n>\n  <slot />\n</div>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/code-block/code-block-actions.astro"
    },
    {
      "path": "packages/ui/src/components/ui/code-block/code-block-content.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\nimport { cn } from \"cn\"\nimport CodeBlockLine from \"./code-block-line.astro\"\nimport { highlightCode } from \"./highlight-code.ts\"\nimport type { CodeBlockContentProps } from \"./types.ts\"\n\ntype Props = HTMLAttributes<\"pre\"> & CodeBlockContentProps\nconst {\n  class: className,\n  code,\n  language,\n  wrap = false,\n  lineNumbers = false,\n  startLine = 1,\n  style,\n  tabindex = 0,\n  dir: _dir,\n  ...props\n} = Astro.props\nconst content =\n  code === undefined ? await Astro.slots.render(\"default\") : undefined\nconst highlighted =\n  code !== undefined && language\n    ? await highlightCode(code, language)\n    : undefined\n// Rendered slots are already escaped by Astro. Do not decode them as source.\nconst slotLines =\n  lineNumbers &&\n  content !== undefined &&\n  !content.includes('data-slot=\"code-block-line\"')\n    ? content.replace(/\\r?\\n$/, \"\").split(/\\r?\\n/)\n    : undefined\nconst lines =\n  code !== undefined && lineNumbers && !highlighted\n    ? code.replace(/\\r?\\n$/, \"\").split(/\\r?\\n/)\n    : undefined\nconst firstLine = Number.isFinite(startLine)\n  ? Math.max(1, Math.trunc(startLine))\n  : 1\n---\n\n<pre\n  data-slot=\"code-block-content\"\n  data-wrap={wrap || undefined}\n  data-numbered={lineNumbers || undefined}\n  tabindex={tabindex}\n  style={\n    typeof style === \"string\"\n      ? `counter-reset:line ${firstLine - 1};${style}`\n      : { counterReset: `line ${firstLine - 1}`, ...style }\n  }\n  class={cn(\"min-h-0 min-w-0 overflow-auto px-4 py-4 text-start font-mono leading-relaxed outline-none group-has-data-[slot=code-block-footer]/code-block:pb-2 group-has-data-[slot=code-block-header]/code-block:pt-2 group-data-[size=sm]/code-block:px-3 group-data-[size=sm]/code-block:py-3 group-data-[size=sm]/code-block:group-has-data-[slot=code-block-header]/code-block:pt-1.5 data-wrap:wrap-break-word data-wrap:whitespace-pre-wrap m-0 bg-transparent text-inherit whitespace-pre [scrollbar-width:thin]\", className)}\n  {...props}\n  dir=\"ltr\"\n><code data-slot=\"code-block-code\" class=\"grid min-h-lh min-w-max content-start in-data-numbered:grid-cols-[auto_1fr] in-data-wrap:min-w-0 [font-family:inherit] text-inherit\">{highlighted ? highlighted.map((line) => <CodeBlockLine><Fragment set:html={line} /></CodeBlockLine>) : lines ? lines.map((line) => <CodeBlockLine>{line}</CodeBlockLine>) : slotLines ? slotLines.map((line) => <CodeBlockLine><Fragment set:html={line} /></CodeBlockLine>) : code !== undefined ? code : <Fragment set:html={content} />}</code></pre>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/code-block/code-block-content.astro"
    },
    {
      "path": "packages/ui/src/components/ui/code-block/code-block-copy-button.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\nimport { Check, Copy } from \"@lucide/astro\"\nimport { cn } from \"cn\"\nimport Button from \"../button/button.astro\"\nimport type { CodeBlockCopyButtonProps } from \"./types.ts\"\n\ntype Props = Omit<HTMLAttributes<\"button\">, \"value\"> & CodeBlockCopyButtonProps\nconst {\n  class: className,\n  value,\n  variant = \"ghost\",\n  size = \"icon-xs\",\n  \"aria-label\": label = \"Copy\",\n  ...props\n} = Astro.props\n---\n\n<div data-slot=\"copy-button\" class=\"contents\">\n  <Button\n    data-slot=\"code-block-copy-button\"\n    data-copy-control\n    data-value={value}\n    variant={variant}\n    size={size}\n    aria-label={label}\n    class={cn(\"shrink-0 group/code-copy\", className)}\n    {...props}\n  >\n    <slot>\n      <Copy aria-hidden=\"true\" class=\"group-data-copied/code-copy:hidden\" />\n      <Check\n        aria-hidden=\"true\"\n        class=\"hidden group-data-copied/code-copy:block\"\n      />\n    </slot>\n  </Button>\n  <span\n    aria-live=\"polite\"\n    aria-atomic=\"true\"\n    class=\"sr-only\"\n    data-slot=\"copy-button-status\"\n  ></span>\n</div>\n\n<script>\n  import {\n    create,\n    type CopyToClipboardController,\n  } from \"../copy-button/copy-to-clipboard.ts\"\n\n  const controllers: CopyToClipboardController[] = []\n  const initialize = () => controllers.push(...create())\n  initialize()\n  document.addEventListener(\"astro:page-load\", initialize)\n  document.addEventListener(\"astro:before-swap\", () => {\n    for (const controller of controllers) controller.destroy()\n    controllers.length = 0\n  })\n</script>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/code-block/code-block-copy-button.astro"
    },
    {
      "path": "packages/ui/src/components/ui/code-block/code-block-footer.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\nimport { cn } from \"cn\"\n\ntype Props = HTMLAttributes<\"div\">\nconst { class: className, ...props } = Astro.props\n---\n\n<div\n  data-slot=\"code-block-footer\"\n  class={cn(\"flex min-w-0 shrink-0 items-center gap-2 px-4 pb-4 text-muted-foreground group-data-[size=sm]/code-block:px-3 group-data-[size=sm]/code-block:pb-3\", className)}\n  {...props}\n>\n  <slot />\n</div>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/code-block/code-block-footer.astro"
    },
    {
      "path": "packages/ui/src/components/ui/code-block/code-block-header.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\nimport { cn } from \"cn\"\n\ntype Props = HTMLAttributes<\"div\">\nconst { class: className, ...props } = Astro.props\n---\n\n<div\n  data-slot=\"code-block-header\"\n  class={cn(\"flex min-w-0 shrink-0 items-center gap-2 px-4 pt-4 text-muted-foreground group-data-[size=sm]/code-block:px-3 group-data-[size=sm]/code-block:pt-3 group-data-[variant=destructive]/code-block:text-destructive *:has-[>[data-slot=code-block-title]]:min-w-0\", className)}\n  {...props}\n>\n  <slot />\n</div>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/code-block/code-block-header.astro"
    },
    {
      "path": "packages/ui/src/components/ui/code-block/code-block-line.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\nimport { cn } from \"cn\"\nimport { codeBlockLineVariants } from \"./code-block-variants.ts\"\nimport type { CodeBlockLineProps } from \"./types.ts\"\n\ntype Props = HTMLAttributes<\"span\"> & CodeBlockLineProps\nconst { class: className, variant = \"default\", ...props } = Astro.props\nconst content = await Astro.slots.render(\"default\")\n---\n\n<span\n  data-slot=\"code-block-line\"\n  data-variant={variant}\n  class={cn(codeBlockLineVariants({ variant }), className)}\n  {...props}\n>\n  <span\n    data-slot=\"code-block-line-content\"\n    class=\"min-w-0 [&_[style]]:text-(--shiki-light) dark:[&_[style]]:text-(--shiki-dark)\"\n    set:html={content || \"\\n\"}\n  />\n</span>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/code-block/code-block-line.astro"
    },
    {
      "path": "packages/ui/src/components/ui/code-block/code-block-title.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\nimport { cn } from \"cn\"\n\ntype Props = HTMLAttributes<\"div\">\nconst { class: className, ...props } = Astro.props\n---\n\n<div\n  data-slot=\"code-block-title\"\n  class={cn(\"min-w-0 truncate font-sans text-sm font-medium text-foreground group-data-[size=sm]/code-block:text-xs group-data-[variant=destructive]/code-block:text-destructive [&_svg]:pointer-events-none [&_svg]:inline-block [&_svg]:align-middle [&_svg:not([class*='size-'])]:size-4 group-data-[size=sm]/code-block:[&_svg:not([class*='size-'])]:size-3.5 *:[svg]:me-1.5\", className)}\n  {...props}\n>\n  <slot />\n</div>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/code-block/code-block-title.astro"
    },
    {
      "path": "packages/ui/src/components/ui/code-block/code-block-variants.ts",
      "content": "import { cva } from 'class-variance-authority'\n\nexport const codeBlockVariants = cva(\"relative flex w-full min-w-0 flex-col overflow-hidden rounded-lg text-sm has-[>[data-slot=code-block-content]:focus-visible]:ring-3 has-[>[data-slot=code-block-content]:focus-visible]:ring-ring/50 data-[size=sm]:text-xs group/code-block\", {\n    variants: {\n        variant: {\n            default: \"border border-input bg-muted ring-1 ring-foreground/5 text-foreground has-[>[data-slot=code-block-content]:focus-visible]:border-ring dark:ring-foreground/10\",\n            destructive: \"border border-destructive/30 text-destructive\",\n        },\n    },\n    defaultVariants: { variant: 'default' },\n})\n\nexport const codeBlockLineVariants = cva(\"relative -mx-4 block min-h-lh px-4 group-data-[size=sm]/code-block:-mx-3 group-data-[size=sm]/code-block:px-3 before:min-w-6 before:text-end before:text-muted-foreground after:absolute after:start-1 after:top-0 after:text-muted-foreground after:select-none in-data-numbered:col-span-2 in-data-numbered:grid in-data-numbered:grid-cols-subgrid in-data-numbered:gap-x-3 in-data-numbered:[counter-increment:line] in-data-numbered:before:content-[counter(line)] in-data-numbered:before:select-none in-data-numbered:supports-[content:counter(line)_/_'']:before:content-[counter(line)_/_'']\", {\n    variants: {\n        variant: {\n            default: '',\n            added: \"bg-success/10 after:text-success after:content-['+']\",\n            removed: \"bg-destructive/10 after:text-destructive after:content-['-']\",\n            highlighted: \"bg-muted\",\n        },\n    },\n    defaultVariants: { variant: 'default' },\n})\n",
      "type": "registry:file",
      "target": "~/src/components/ui/code-block/code-block-variants.ts"
    },
    {
      "path": "packages/ui/src/components/ui/code-block/code-block.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\nimport { cn } from \"cn\"\nimport { codeBlockVariants } from \"./code-block-variants.ts\"\nimport type { CodeBlockProps } from \"./types.ts\"\n\ntype Props = HTMLAttributes<\"div\"> & CodeBlockProps\nconst {\n  class: className,\n  variant = \"default\",\n  size = \"default\",\n  ...props\n} = Astro.props\n---\n\n<div\n  data-slot=\"code-block\"\n  data-variant={variant}\n  data-size={size}\n  class={cn(codeBlockVariants({ variant }), className)}\n  {...props}\n>\n  <slot />\n</div>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/code-block/code-block.astro"
    },
    {
      "path": "packages/ui/src/components/ui/code-block/highlight-code.ts",
      "content": "// Server-only Astro port of shadcn/ui's highlighter (MIT).\nimport { createHighlighterCore, type HighlighterCore } from 'shiki/core'\nimport { createJavaScriptRegexEngine } from 'shiki/engine/javascript'\n\nconst languages = {\n    astro: () => import('shiki/langs/astro.mjs'),\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\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 getLanguage(language: string) {\n    const name = language.toLowerCase()\n\n    if (name === 'ansi') {\n        return name\n    }\n\n    if (Object.hasOwn(languages, name)) {\n        return name as keyof typeof languages\n    }\n\n    return Object.hasOwn(aliases, name) ? aliases[name]! : null\n}\n\n/** Escape source and token styles before insertion into an Astro HTML slot. */\nfunction escapeHtml(value: string) {\n    return value\n        .replace(/&/g, '&amp;')\n        .replace(/</g, '&lt;')\n        .replace(/>/g, '&gt;')\n        .replace(/\"/g, '&quot;')\n        .replace(/'/g, '&#39;')\n}\n\n/** Return safe HTML for each line. Unknown languages return escaped plain lines. */\nexport async function highlightCode(\n    code: string,\n    language: string,\n): Promise<string[]> {\n    const source = code.replace(/\\r?\\n$/, '')\n    const lang = getLanguage(language)\n    if (!lang) return source.split(/\\r?\\n/).map(escapeHtml)\n    const shiki = await getHighlighter()\n    if (lang !== 'ansi' && !shiki.getLoadedLanguages().includes(lang)) {\n        await shiki.loadLanguage(languages[lang])\n    }\n    const tokenize = (text: string) =>\n        shiki.codeToTokens(text, {\n            lang,\n            themes: { light: 'github-light', dark: 'github-dark' },\n            defaultColor: false,\n        }).tokens\n    const tokens =\n        lang === 'ansi'\n            ? source.split(/\\r?\\n/).flatMap(tokenize)\n            : tokenize(source)\n    return tokens.map((line) =>\n        line\n            .map((token) => {\n                const style = Object.entries(token.htmlStyle ?? {})\n                    .map(([key, value]) => `${key}:${value}`)\n                    .join(';')\n                return `<span style=\"${escapeHtml(style)}\">${escapeHtml(token.content)}</span>`\n            })\n            .join(''),\n    )\n}\n",
      "type": "registry:file",
      "target": "~/src/components/ui/code-block/highlight-code.ts"
    },
    {
      "path": "packages/ui/src/components/ui/code-block/index.ts",
      "content": "export { default as CodeBlock } from './code-block.astro'\nexport { default as CodeBlockHeader } from './code-block-header.astro'\nexport { default as CodeBlockTitle } from './code-block-title.astro'\nexport { default as CodeBlockActions } from './code-block-actions.astro'\nexport { default as CodeBlockCopyButton } from './code-block-copy-button.astro'\nexport { default as CodeBlockContent } from './code-block-content.astro'\nexport { default as CodeBlockLine } from './code-block-line.astro'\nexport { default as CodeBlockFooter } from './code-block-footer.astro'\n\nexport type {\n    CodeBlockProps,\n    CodeBlockContentProps,\n    CodeBlockLineProps,\n    CodeBlockCopyButtonProps,\n} from './types.ts'\n",
      "type": "registry:file",
      "target": "~/src/components/ui/code-block/index.ts"
    },
    {
      "path": "packages/ui/src/components/ui/code-block/types.ts",
      "content": "import type { ButtonVariantProps } from '../button/button-variants.ts'\n\n/** The surface around a code sample or terminal output. */\nexport interface CodeBlockProps {\n    /** The tone of the block. @default 'default' */\n    variant?: 'default' | 'destructive'\n    /** The spacing and text size. @default 'default' */\n    size?: 'default' | 'sm'\n}\n\n/** Code content. Use the default slot for composed CodeBlockLine parts. */\nexport interface CodeBlockContentProps {\n    /** Plain source text. Takes precedence over the default slot. */\n    code?: string\n    /** Highlight `code` on the server with Shiki. Unknown languages stay plain. */\n    language?: string\n    /** Wrap long lines. @default false */\n    wrap?: boolean\n    /** Show line numbers outside the selectable text. @default false */\n    lineNumbers?: boolean\n    /** The first line number. @default 1 */\n    startLine?: number\n}\n\n/** A single line with an optional diff or highlight state. */\nexport interface CodeBlockLineProps {\n    /** The state of the line. @default 'default' */\n    variant?: 'default' | 'added' | 'removed' | 'highlighted'\n}\n\n/** A Button that copies the supplied source text. */\nexport interface CodeBlockCopyButtonProps extends ButtonVariantProps {\n    /** The exact text to copy, without line numbers or diff markers. */\n    value: string\n}\n",
      "type": "registry:file",
      "target": "~/src/components/ui/code-block/types.ts"
    }
  ],
  "meta": {
    "framework": "astro",
    "source": "@gnostic/ui",
    "testedDependencies": {
      "@lucide/astro": "^1.68.0",
      "class-variance-authority": "^0.7.1",
      "cn": "0.5.2",
      "shiki": "4.6.0"
    }
  },
  "docs": "https://ui.gnosticai.com/components/code-block",
  "categories": [
    "ui"
  ],
  "type": "registry:ui"
}
