{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "markdown",
  "title": "Markdown",
  "type": "registry:ui",
  "files": [
    {
      "path": "packages/registry-native/components/markdown/Markdown.tsx",
      "content": "import * as React from 'react'\nimport { Platform, Text as NativeText, View, type ViewProps } from 'react-native'\nimport { Text, type TextProps } from '@/components/ui/text'\nimport { Image } from '@/components/ui/image'\nimport { cn } from '@/lib/utils'\nimport { isMarkdownLinkAllowed, markdownPlainText, parseMarkdown, type MarkdownNode } from './markdown.utils'\n\nexport interface MarkdownProps extends Omit<ViewProps, 'children'> {\n  source: string\n  className?: string\n  textProps?: Omit<TextProps, 'children'>\n  /** HTTP(S) and mailto callbacks only. The component never launches a URL itself. */\n  onLinkPress?: (url: string) => void\n  disabled?: boolean\n  /** Standalone image blocks are opt-in to avoid requests from untrusted documents. Inline images keep their alt text. */\n  allowImages?: boolean\n  imageHeight?: number\n  renderCode?: (code: string, language: string | undefined) => React.ReactNode\n}\ninterface RenderOptions {\n  textProps: Omit<TextProps, 'children'>\n  onLinkPress?: (url: string) => void\n  disabled: boolean\n  allowImages: boolean\n  imageHeight: number\n  renderCode?: MarkdownProps['renderCode']\n}\nconst mono = Platform.OS === 'ios' ? 'Menlo' : 'monospace'\n\nfunction inline(nodes: MarkdownNode[], options: RenderOptions, prefix: string): React.ReactNode {\n  return nodes.map((node, index) => {\n    const key = `${prefix}-${index}`\n    const children = node.children.length ? inline(node.children, options, key) : node.content\n    if (node.type === 'softbreak' || node.type === 'hardbreak') return '\\n'\n    if (node.type === 'code_inline')\n      return (\n        <NativeText key={key} style={{ fontFamily: mono }} className=\"bg-muted\">\n          {node.content}\n        </NativeText>\n      )\n    if (node.type === 'em')\n      return (\n        <NativeText key={key} className=\"italic\">\n          {children}\n        </NativeText>\n      )\n    if (node.type === 'strong')\n      return (\n        <NativeText key={key} className=\"font-semibold\">\n          {children}\n        </NativeText>\n      )\n    if (node.type === 'link') {\n      const url = node.attrs.href ?? ''\n      const allowed = isMarkdownLinkAllowed(url)\n      const actionable = allowed && !!options.onLinkPress && !options.disabled\n      return (\n        <NativeText\n          key={key}\n          className={allowed ? 'text-primary underline' : undefined}\n          accessibilityRole={actionable ? 'link' : undefined}\n          accessibilityState={allowed && options.onLinkPress ? { disabled: options.disabled } : undefined}\n          onPress={actionable ? () => options.onLinkPress?.(url) : undefined}\n        >\n          {children}\n        </NativeText>\n      )\n    }\n    if (node.type === 'image') return node.content || markdownPlainText(node.children) || '[Image]'\n    return node.children.length ? <React.Fragment key={key}>{children}</React.Fragment> : node.content\n  })\n}\n\nfunction blocks(nodes: MarkdownNode[], options: RenderOptions, prefix = 'md'): React.ReactNode {\n  return nodes.map((node, index) => {\n    const key = `${prefix}-${index}`\n    const textProps = options.textProps\n    if (node.type === 'inline')\n      return (\n        <Text key={key} {...textProps}>\n          {inline(node.children, options, key)}\n        </Text>\n      )\n    if (node.type === 'paragraph' || node.type === 'heading') {\n      const content = node.children[0]?.type === 'inline' ? node.children[0].children : node.children\n      const onlyImage = content.length === 1 && content[0].type === 'image' ? content[0] : null\n      if (\n        onlyImage &&\n        options.allowImages &&\n        /^https?:\\/\\//i.test(onlyImage.attrs.src ?? '') &&\n        isMarkdownLinkAllowed(onlyImage.attrs.src)\n      ) {\n        return (\n          <Image\n            key={`${key}-${onlyImage.attrs.src}`}\n            source={{ uri: onlyImage.attrs.src }}\n            accessibilityLabel={onlyImage.content || 'Document image'}\n            style={{ height: options.imageHeight, aspectRatio: 'auto' }}\n          />\n        )\n      }\n      const heading = node.type === 'heading'\n      return (\n        <Text\n          key={key}\n          {...textProps}\n          accessibilityRole={heading ? 'header' : textProps.accessibilityRole}\n          size={heading ? (node.tag === 'h1' ? 'title' : node.tag === 'h2' ? 'xl' : 'lg') : textProps.size}\n          className={cn(heading && 'font-semibold', textProps.className)}\n        >\n          {inline(content, options, key)}\n        </Text>\n      )\n    }\n    if (node.type === 'fence' || node.type === 'code_block') {\n      return (\n        <View key={key} className=\"min-w-0 rounded-md border border-border bg-muted p-[12px]\">\n          {options.renderCode ? (\n            options.renderCode(node.content, node.attrs.language)\n          ) : (\n            <Text {...textProps} style={[{ fontFamily: mono }, textProps.style]}>\n              {node.content.replace(/\\n$/, '')}\n            </Text>\n          )}\n        </View>\n      )\n    }\n    if (node.type === 'blockquote')\n      return (\n        <View key={key} className=\"gap-[8px] border-border ps-[12px]\" style={{ borderStartWidth: 3 }}>\n          {blocks(node.children, options, key)}\n        </View>\n      )\n    if (node.type === 'bullet_list' || node.type === 'ordered_list') {\n      const start = Number(node.attrs.start ?? 1)\n      return (\n        <View key={key} className=\"gap-[8px]\">\n          {node.children.map((item, itemIndex) => (\n            <View key={`${key}-${itemIndex}`} className=\"min-w-0 flex-row items-start gap-[8px]\">\n              <Text {...textProps} className={cn('min-w-[24px] text-end', textProps.className)}>\n                {node.type === 'ordered_list' ? `${start + itemIndex}.` : '•'}\n              </Text>\n              <View className=\"min-w-0 flex-1 gap-[8px]\">{blocks(item.children, options, `${key}-${itemIndex}`)}</View>\n            </View>\n          ))}\n        </View>\n      )\n    }\n    if (node.type === 'hr') return <View key={key} className=\"h-px bg-border\" aria-hidden />\n    return node.children.length ? (\n      <View key={key} className=\"gap-[8px]\">\n        {blocks(node.children, options, key)}\n      </View>\n    ) : node.content ? (\n      <Text key={key} {...textProps}>\n        {node.content}\n      </Text>\n    ) : null\n  })\n}\n\n/** Read-only CommonMark. Raw HTML, plugins and automatic external navigation are not enabled. */\nexport const Markdown = React.forwardRef<React.ComponentRef<typeof View>, MarkdownProps>(\n  (\n    {\n      source,\n      className,\n      textProps = {},\n      onLinkPress,\n      disabled = false,\n      allowImages = false,\n      imageHeight = 180,\n      renderCode,\n      ...props\n    },\n    ref,\n  ) => {\n    const nodes = React.useMemo(() => parseMarkdown(source), [source])\n    const safeImageHeight = Number.isFinite(imageHeight) && imageHeight > 0 ? imageHeight : 180\n    return (\n      <View ref={ref} {...props} className={cn('min-w-0 gap-[12px]', className)}>\n        {blocks(nodes, {\n          textProps: { selectable: true, ...textProps },\n          onLinkPress,\n          disabled,\n          allowImages,\n          imageHeight: safeImageHeight,\n          renderCode,\n        })}\n      </View>\n    )\n  },\n)\nMarkdown.displayName = 'Markdown'\n",
      "type": "registry:ui",
      "target": "~/components/ui/markdown/Markdown.tsx"
    },
    {
      "path": "packages/registry-native/components/markdown/markdown.utils.ts",
      "content": "import MarkdownIt from 'markdown-it'\nimport type Token from 'markdown-it/lib/token.mjs'\n\nexport interface MarkdownNode {\n  type: string\n  tag: string\n  content: string\n  attrs: Record<string, string>\n  children: MarkdownNode[]\n}\n\n// CommonMark tokens are rendered as native elements; HTML is never evaluated.\n// https://github.com/markdown-it/markdown-it/blob/14.1.0/docs/security.md\nconst parser = new MarkdownIt('commonmark', { html: false, linkify: false })\n\nfunction nodesFromTokens(tokens: Token[]): MarkdownNode[] {\n  const root: MarkdownNode[] = []\n  const stack: MarkdownNode[][] = [root]\n  for (const token of tokens) {\n    if (token.nesting === -1) {\n      if (stack.length > 1) stack.pop()\n      continue\n    }\n    const node: MarkdownNode = {\n      type: token.type.replace(/_open$/, ''),\n      tag: token.tag,\n      content: token.content,\n      attrs: Object.fromEntries(token.attrs ?? []),\n      children: token.children ? nodesFromTokens(token.children) : [],\n    }\n    if (token.info) node.attrs.language = token.info.trim().split(/\\s+/)[0]\n    stack[stack.length - 1].push(node)\n    if (token.nesting === 1) stack.push(node.children)\n  }\n  return root\n}\n\nexport function parseMarkdown(source: string): MarkdownNode[] {\n  return nodesFromTokens(parser.parse(source, {}))\n}\n\n/** Caller callbacks never receive script, file, custom-scheme or control-character links. */\nexport function isMarkdownLinkAllowed(url: string): boolean {\n  if (!/^(https?:\\/\\/|mailto:)/i.test(url)) return false\n  for (let index = 0; index < url.length; index++) {\n    const code = url.charCodeAt(index)\n    if (code <= 32 || code === 127) return false\n  }\n  return true\n}\n\nexport function markdownPlainText(nodes: readonly MarkdownNode[]): string {\n  return nodes.map((node) => (node.children.length ? markdownPlainText(node.children) : node.content)).join('')\n}\n",
      "type": "registry:ui",
      "target": "~/components/ui/markdown/markdown.utils.ts"
    },
    {
      "path": "packages/registry-native/components/markdown/index.ts",
      "content": "export * from './Markdown'\n",
      "type": "registry:ui",
      "target": "~/components/ui/markdown/index.ts"
    }
  ],
  "dependencies": [
    "markdown-it@^14.1.0"
  ],
  "devDependencies": [
    "@types/markdown-it@^14.1.2"
  ],
  "registryDependencies": [
    "https://native.uipkge.dev/r/native/text.json",
    "https://native.uipkge.dev/r/native/image.json",
    "https://native.uipkge.dev/r/native/utils.json"
  ],
  "description": "Read-only native CommonMark rendering with safe caller-owned links, code and opt-in bounded image blocks.",
  "categories": [
    "content"
  ]
}