{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "opentui/markdown",
  "title": "Markdown",
  "description": "Full markdown renderer for terminal",
  "dependencies": [
    "@opentui/react"
  ],
  "registryDependencies": [
    "https://termcn.dev/r/opentui/use-theme.json"
  ],
  "files": [
    {
      "path": "registry/opentui/ui/markdown.tsx",
      "content": "/* @jsxImportSource @opentui/react */\nimport React from \"react\";\n\nimport { useTheme } from \"@/hooks/use-theme\";\n\nexport interface MarkdownProps {\n  children: string;\n  width?: number;\n}\n\ninterface InlineSegment {\n  text: string;\n  bold?: boolean;\n  italic?: boolean;\n  code?: boolean;\n  link?: boolean;\n  url?: string;\n}\n\nconst parseInline = (line: string): InlineSegment[] => {\n  const segments: InlineSegment[] = [];\n  const re = /(\\*\\*(.+?)\\*\\*|\\*(.+?)\\*|`(.+?)`|\\[(.+?)\\]\\((.+?)\\))/g;\n  let last = 0;\n  let match: RegExpExecArray | null;\n\n  while ((match = re.exec(line)) !== null) {\n    if (match.index > last) {\n      segments.push({ text: line.slice(last, match.index) });\n    }\n\n    const [full] = match;\n    if (full.startsWith(\"**\")) {\n      segments.push({ bold: true, text: match[2] });\n    } else if (full.startsWith(\"*\")) {\n      segments.push({ italic: true, text: match[3] });\n    } else if (full.startsWith(\"`\")) {\n      segments.push({ code: true, text: match[4] });\n    } else if (full.startsWith(\"[\")) {\n      segments.push({ link: true, text: match[5], url: match[6] });\n    }\n\n    last = match.index + full.length;\n  }\n\n  if (last < line.length) {\n    segments.push({ text: line.slice(last) });\n  }\n\n  return segments;\n};\n\nconst InlineLine = ({ segments }: { segments: InlineSegment[] }) => {\n  const theme = useTheme();\n\n  return (\n    <box flexDirection=\"row\">\n      {segments.map((seg, i) => {\n        if (seg.code) {\n          return (\n            <text key={i} fg={theme.colors.accent}>\n              {seg.text}\n            </text>\n          );\n        }\n        if (seg.link) {\n          return (\n            <box key={i} flexDirection=\"row\">\n              <text fg={theme.colors.info}>\n                <u>{seg.text}</u>\n              </text>\n              <text fg=\"#666\">{` (${seg.url})`}</text>\n            </box>\n          );\n        }\n        let content: React.ReactNode = seg.text;\n        if (seg.bold && seg.italic) {\n          content = (\n            <b>\n              <i>{seg.text}</i>\n            </b>\n          );\n        } else if (seg.bold) {\n          content = <b>{seg.text}</b>;\n        } else if (seg.italic) {\n          content = <i>{seg.text}</i>;\n        }\n        return <text key={i}>{content}</text>;\n      })}\n    </box>\n  );\n};\n\nexport const Markdown = ({ children, width }: MarkdownProps) => {\n  const theme = useTheme();\n  const lines = children.split(\"\\n\");\n\n  const elements: React.ReactNode[] = [];\n  let i = 0;\n\n  while (i < lines.length) {\n    const line = lines[i];\n\n    const h4 = line.match(/^####\\s+(.*)/);\n    const h3 = line.match(/^###\\s+(.*)/);\n    const h2 = line.match(/^##\\s+(.*)/);\n    const h1 = line.match(/^#\\s+(.*)/);\n\n    if (h1) {\n      elements.push(\n        <text key={i} fg={theme.colors.primary}>\n          <b>{h1[1]}</b>\n        </text>\n      );\n    } else if (h2) {\n      elements.push(\n        <text key={i} fg={theme.colors.primary}>\n          <b>{h2[1]}</b>\n        </text>\n      );\n    } else if (h3) {\n      elements.push(\n        <text key={i} fg={theme.colors.primary}>\n          <b>{h3[1]}</b>\n        </text>\n      );\n    } else if (h4) {\n      elements.push(\n        <text key={i} fg={theme.colors.primary}>\n          {h4[1]}\n        </text>\n      );\n    } else if (/^---+$/.test(line)) {\n      elements.push(\n        <text key={i} fg={theme.colors.border}>\n          {\"─\".repeat(width ?? 40)}\n        </text>\n      );\n    } else if (/^>\\s/.test(line)) {\n      const content = line.replace(/^>\\s/, \"\");\n      elements.push(\n        <box key={i} gap={1}>\n          <text fg={theme.colors.primary}>│</text>\n          <InlineLine segments={parseInline(content)} />\n        </box>\n      );\n    } else if (/^[-*]\\s/.test(line)) {\n      const content = line.replace(/^[-*]\\s/, \"\");\n      elements.push(\n        <box key={i} gap={1}>\n          <text fg={theme.colors.mutedForeground}>•</text>\n          <InlineLine segments={parseInline(content)} />\n        </box>\n      );\n    } else if (line === \"\") {\n      elements.push(<box key={i} />);\n    } else {\n      elements.push(\n        <box key={i} flexWrap=\"wrap\" width={width}>\n          <InlineLine segments={parseInline(line)} />\n        </box>\n      );\n    }\n\n    i += 1;\n  }\n\n  return <box flexDirection=\"column\">{elements}</box>;\n};\n",
      "type": "registry:ui",
      "target": "components/ui/markdown.tsx"
    }
  ],
  "categories": [
    "typography"
  ],
  "type": "registry:ui"
}