{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "markdown",
  "type": "registry:ui",
  "title": "Markdown",
  "description": "Markdown provides its canonical documented behavior.",
  "dependencies": [
    "react-markdown@10.1.0",
    "remark-gfm@4.0.1"
  ],
  "registryDependencies": [
    "https://ghost-ui.com/r/ghost-tokens.json",
    "https://ghost-ui.com/r/snippet.json",
    "https://ghost-ui.com/r/table.json",
    "https://ghost-ui.com/r/ghost-typeset.json",
    "https://ghost-ui.com/r/link.json"
  ],
  "files": [
    {
      "path": "src/components/ui/markdown.tsx",
      "type": "registry:component",
      "target": "@ui/markdown.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport MarkdownRenderer, { type Components, type ExtraProps } from \"react-markdown\";\nimport remarkGfm from \"remark-gfm\";\n\nimport { Link } from \"./link\";\nimport { Snippet } from \"./snippet\";\nimport { Table } from \"./table\";\nimport \"./markdown.css\";\n\nexport interface MarkdownProps {\n  /** Markdown text supplied by the caller. Raw HTML is always omitted. */\n  children: string;\n  /** Partial output keeps external links and code copying unavailable. */\n  streaming?: boolean;\n  className?: string;\n}\n\nfunction isAllowedUrl(value: string, allowAnchor = true) {\n  if (allowAnchor && value.startsWith(\"#\")) return true;\n  try {\n    const url = new URL(value);\n    return (url.protocol === \"http:\" || url.protocol === \"https:\") && url.username === \"\" && url.password === \"\";\n  } catch {\n    return false;\n  }\n}\n\nconst MarkdownState = React.createContext({ streaming: false, footnotePrefix: \"\" });\n\nfunction transformUrl(value: string, key: string) {\n  if (key === \"href\" || key === \"src\") return isAllowedUrl(value) ? value : undefined;\n  return value;\n}\n\nfunction readableText(node: React.ReactNode): string {\n  if (typeof node === \"string\" || typeof node === \"number\") return String(node);\n  if (Array.isArray(node)) return node.map(readableText).join(\"\");\n  if (React.isValidElement<{ children?: React.ReactNode }>(node)) return readableText(node.props.children);\n  return \"\";\n}\n\nfunction MarkdownLink({ children, href, node: _node, \"aria-describedby\": describedBy, ...props }: React.ComponentProps<\"a\"> & ExtraProps) {\n  const { streaming, footnotePrefix } = React.useContext(MarkdownState);\n  const value = typeof href === \"string\" ? href : \"\";\n  const external = !value.startsWith(\"#\");\n  const description = describedBy === \"footnote-label\" ? `${footnotePrefix}footnote-label` : describedBy;\n  if (!isAllowedUrl(value) || (streaming && external)) {\n    return <span className=\"ui-markdown__link-disabled\" {...props}>{children}</span>;\n  }\n  if (!external) return <Link {...props} href={value} aria-describedby={description}>{children}</Link>;\n  return <Link {...props} href={value} target=\"_blank\" rel=\"noreferrer noopener\" aria-describedby={description}>{children}</Link>;\n}\n\nfunction MarkdownImage({ alt }: React.ComponentProps<\"img\">) {\n  return <span className=\"ui-markdown__image-alt\" role=\"img\" aria-label={alt || \"Image\"}>{alt || \"Image\"}</span>;\n}\n\nfunction MarkdownPre({ children }: React.ComponentProps<\"pre\">) {\n  const { streaming } = React.useContext(MarkdownState);\n  const code = readableText(children);\n  const child = React.Children.toArray(children)[0];\n  const language = React.isValidElement<{ className?: string }>(child) && typeof child.props.className === \"string\" ? child.props.className.match(/(?:^|\\s)language-([^\\s]+)/)?.[1] : undefined;\n  const label = language ? `Language: ${language}` : \"Code block\";\n  return <div className=\"ui-markdown__codeblock\"><Snippet value={code} variant=\"block\" title={label} language={language ?? \"text\"} disabled={streaming} /><span className=\"ui-markdown__code-status\" role=\"status\" aria-live=\"polite\">{streaming ? \"Copy unavailable for partial code.\" : \"\"}</span></div>;\n}\n\nfunction MarkdownHeading({ node: _node, id, ...props }: React.ComponentProps<\"h2\"> & ExtraProps) {\n  const { footnotePrefix } = React.useContext(MarkdownState);\n  return <h2 {...props} id={id === \"footnote-label\" ? `${footnotePrefix}footnote-label` : id} />;\n}\n\nfunction MarkdownTable({ node: _node, ...props }: React.ComponentProps<\"table\"> & ExtraProps) {\n  return <Table.Root layout=\"scroll\" aria-label=\"Table\">{props.children}</Table.Root>;\n}\n\nconst components: Components = { a: MarkdownLink, h2: MarkdownHeading, img: MarkdownImage, pre: MarkdownPre, table: MarkdownTable };\n\nexport function Markdown({ children, streaming = false, className }: MarkdownProps) {\n  const id = React.useId();\n  const footnotePrefix = `ui-markdown-${id.replace(/[^a-zA-Z0-9_-]/g, \"\")}-`;\n  const state = React.useMemo(() => ({ streaming, footnotePrefix }), [streaming, footnotePrefix]);\n  return (\n    <MarkdownState.Provider value={state}>\n      <div className={[\"ui-markdown\", \"typeset\", className].filter(Boolean).join(\" \")}>\n        <MarkdownRenderer skipHtml remarkPlugins={[remarkGfm]} remarkRehypeOptions={{ clobberPrefix: footnotePrefix }} urlTransform={transformUrl} components={components}>{children}</MarkdownRenderer>\n      </div>\n    </MarkdownState.Provider>\n  );\n}\n\nMarkdown.displayName = \"Markdown\";\n"
    },
    {
      "path": "src/components/ui/markdown.css",
      "type": "registry:file",
      "target": "@ui/markdown.css",
      "content": "@import \"./tokens.css\";\n@import \"./typeset.css\";\n\n.ui-markdown {\n  min-width: 0;\n  max-width: 100%;\n  overflow-wrap: anywhere;\n}\n\n.ui-markdown > :first-child { margin-block-start: 0; }\n.ui-markdown > :last-child { margin-block-end: 0; }\n.ui-markdown.typeset { --typeset-flow: var(--space-4); font-size: var(--font-size-base); line-height: 22px; }\n.ui-markdown.typeset :where(h1, h2, h3, h4, h5, h6) { margin-block: var(--space-4) var(--space-2); font-weight: var(--font-weight-regular); }\n.ui-markdown.typeset h1 { font-size: calc(var(--font-size-lg) + 2px); line-height: var(--space-6); }\n.ui-markdown.typeset h2 { font-size: var(--font-size-lg); line-height: var(--space-6); }\n.ui-markdown.typeset h3 { font-size: calc((var(--font-size-base) + var(--font-size-lg)) / 2); line-height: var(--space-6); }\n.ui-markdown.typeset :where(h4, h5, h6) { font-size: var(--font-size-base); line-height: var(--space-5); color: var(--muted-foreground); }\n.ui-markdown.typeset > :first-child { margin-block-start: 0; }\n.ui-markdown__link-disabled {\n  color: inherit;\n  text-decoration: underline;\n  text-decoration-style: dotted;\n  text-decoration-color: var(--muted-foreground);\n  text-underline-offset: var(--space-1);\n}\n.ui-markdown__image-alt { color: var(--muted-foreground); font-style: italic; }\n.ui-markdown__codeblock { min-width: 0; max-width: 100%; margin-block: var(--space-4); }\n.ui-markdown__code-status { display: block; color: var(--muted-foreground); font-size: var(--font-size-caption); }\n.ui-markdown :where(a):focus-visible { outline: var(--focus-border-width) solid var(--ring); outline-offset: var(--focus-outline-offset); }\n"
    }
  ]
}
