{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ds-markdown-editor",
  "description": "Markdown editor with edit/preview/split modes, toolbar, and fullscreen.",
  "dependencies": [
    "class-variance-authority",
    "lucide-react"
  ],
  "registryDependencies": [
    "@lema-ds/skeleton",
    "@lema-ds/button",
    "@lema-ds/separator",
    "@lema-ds/toggle-group"
  ],
  "files": [
    {
      "path": "components/ds/ds-markdown-editor.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { cva } from \"class-variance-authority\"\nimport {\n  Bold,\n  Code,\n  Eye,\n  Heading1,\n  Italic,\n  Link,\n  List,\n  ListOrdered,\n  Maximize2,\n  Minimize2,\n  Quote,\n  SplitSquareHorizontal,\n  Type,\n} from \"lucide-react\"\nimport { cn } from \"@/lib/utils\"\nimport { Skeleton } from \"@/components/ui/skeleton\"\nimport { Button } from \"@/components/ui/button\"\nimport { Separator } from \"@/components/ui/separator\"\nimport { ToggleGroup, ToggleGroupItem } from \"@/components/ui/toggle-group\"\n\n// ── Types ──────────────────────────────────────────────────────────────────\n\nexport type MarkdownEditorMode = \"edit\" | \"preview\" | \"split\"\n\nexport interface MarkdownEditorProps extends Omit<\n  React.HTMLAttributes<HTMLDivElement>,\n  \"onChange\"\n> {\n  value?: string\n  onChange?: (value: string) => void\n  placeholder?: string\n  height?: number\n  disabled?: boolean\n  loading?: boolean\n  mode?: MarkdownEditorMode\n  showToolbar?: boolean\n}\n\n// ── Variants ───────────────────────────────────────────────────────────────\n\nexport const markdownEditorContainerVariants = cva(\n  \"w-full overflow-hidden rounded-xl border border-border bg-card\",\n  {\n    variants: {},\n  }\n)\n\nexport const markdownEditorTextareaVariants = cva(\n  [\n    \"w-full resize-none bg-transparent p-4 text-sm leading-relaxed\",\n    \"text-foreground placeholder:text-muted-foreground\",\n    \"focus:outline-none\",\n    \"disabled:cursor-not-allowed disabled:opacity-50\",\n  ].join(\" \"),\n  {\n    variants: {},\n  }\n)\n\n// ── Markdown renderer (regex-based, no deps) ──────────────────────────────\n\nfunction renderMarkdown(md: string): string {\n  let html = md\n    // Escape HTML\n    .replace(/&/g, \"&amp;\")\n    .replace(/</g, \"&lt;\")\n    .replace(/>/g, \"&gt;\")\n\n  // Code blocks (must be before inline code)\n  html = html.replace(\n    /```([a-z]*)\\n([\\s\\S]*?)```/g,\n    (_, _lang, code) =>\n      `<pre class=\"rounded-lg bg-muted p-3 text-sm overflow-auto my-3\"><code>${code}</code></pre>`\n  )\n\n  // Blockquote\n  html = html.replace(\n    /^&gt; (.+)$/gm,\n    // blockquote accent — intentional, not AI slop\n    '<blockquote class=\"border-l-4 /* blockquote accent — intentional */ border-border pl-4 text-muted-foreground my-2\">$1</blockquote>'\n  )\n\n  // Headings\n  html = html.replace(\n    /^# (.+)$/gm,\n    '<h1 class=\"text-2xl font-bold my-3\">$1</h1>'\n  )\n  html = html.replace(\n    /^## (.+)$/gm,\n    '<h2 class=\"text-xl font-semibold my-2\">$1</h2>'\n  )\n  html = html.replace(\n    /^### (.+)$/gm,\n    '<h3 class=\"text-base font-semibold my-2\">$1</h3>'\n  )\n  html = html.replace(\n    /^#### (.+)$/gm,\n    '<h4 class=\"text-base font-semibold my-1\">$1</h4>'\n  )\n\n  // Horizontal rule\n  html = html.replace(/^---$/gm, '<hr class=\"border-border my-4\"/>')\n\n  // Unordered list\n  html = html.replace(\n    /^[\\*\\-] (.+)$/gm,\n    '<li class=\"ml-4 list-disc text-sm\">$1</li>'\n  )\n\n  // Ordered list\n  html = html.replace(\n    /^\\d+\\. (.+)$/gm,\n    '<li class=\"ml-4 list-decimal text-sm\">$1</li>'\n  )\n\n  // Wrap consecutive list items in <ul>/<ol> (list items must be contained\n  // in a list element for the outline to be valid and accessible)\n  html = html.replace(\n    /(?:^<li class=\"ml-4 list-disc text-sm\">.*<\\/li>\\n?)+/gm,\n    (match) => `<ul class=\"my-2\">${match.trim()}</ul>`\n  )\n  html = html.replace(\n    /(?:^<li class=\"ml-4 list-decimal text-sm\">.*<\\/li>\\n?)+/gm,\n    (match) => `<ol class=\"my-2\">${match.trim()}</ol>`\n  )\n\n  // Bold\n  html = html.replace(\n    /\\*\\*(.+?)\\*\\*/g,\n    '<strong class=\"font-semibold\">$1</strong>'\n  )\n\n  // Italic\n  html = html.replace(/\\*(.+?)\\*/g, '<em class=\"italic\">$1</em>')\n  html = html.replace(/_(.+?)_/g, '<em class=\"italic\">$1</em>')\n\n  // Inline code\n  html = html.replace(\n    /`([^`]+)`/g,\n    '<code class=\"rounded bg-muted px-1 py-0.5 text-xs\">$1</code>'\n  )\n\n  // Links\n  html = html.replace(\n    /\\[(.+?)\\]\\((.+?)\\)/g,\n    '<a href=\"$2\" class=\"text-primary underline underline-offset-4 hover:opacity-80\" target=\"_blank\" rel=\"noopener noreferrer\">$1</a>'\n  )\n\n  // Paragraphs (double newline = paragraph break)\n  html = html\n    .split(/\\n\\n+/)\n    .map((block) => {\n      if (\n        block.startsWith(\"<h\") ||\n        block.startsWith(\"<pre\") ||\n        block.startsWith(\"<blockquote\") ||\n        block.startsWith(\"<hr\") ||\n        block.startsWith(\"<ul\") ||\n        block.startsWith(\"<ol\")\n      ) {\n        return block\n      }\n      const trimmed = block.trim()\n      if (!trimmed) return \"\"\n      return `<p class=\"text-sm leading-relaxed my-2\">${trimmed.replace(/\\n/g, \"<br/>\")}</p>`\n    })\n    .join(\"\\n\")\n\n  return html\n}\n\n// ── Toolbar actions ───────────────────────────────────────────────────────\n\nfunction insertMarkdown(\n  textarea: HTMLTextAreaElement,\n  before: string,\n  after = \"\",\n  placeholder = \"\"\n) {\n  const start = textarea.selectionStart\n  const end = textarea.selectionEnd\n  const selected = textarea.value.substring(start, end) || placeholder\n  const newValue =\n    textarea.value.substring(0, start) +\n    before +\n    selected +\n    after +\n    textarea.value.substring(end)\n  return {\n    value: newValue,\n    cursor: start + before.length + selected.length + after.length,\n  }\n}\n\n// ── MarkdownEditor ─────────────────────────────────────────────────────────\n\nexport function MarkdownEditor({\n  value = \"\",\n  onChange,\n  placeholder = \"Write your content here…\",\n  height = 360,\n  disabled = false,\n  loading = false,\n  mode: modeProp = \"split\",\n  showToolbar = true,\n  className,\n  ...props\n}: MarkdownEditorProps) {\n  const [internalValue, setInternalValue] = React.useState(value)\n  const [mode, setMode] = React.useState<MarkdownEditorMode>(modeProp)\n  const [fullscreen, setFullscreen] = React.useState(false)\n  const textareaRef = React.useRef<HTMLTextAreaElement>(null)\n\n  const currentValue = onChange ? value : internalValue\n\n  const handleChange = React.useCallback(\n    (newVal: string) => {\n      if (!onChange) setInternalValue(newVal)\n      onChange?.(newVal)\n    },\n    [onChange]\n  )\n\n  const applyFormat = React.useCallback(\n    (before: string, after = \"\", placeholder = \"\") => {\n      const ta = textareaRef.current\n      if (!ta) return\n      const result = insertMarkdown(ta, before, after, placeholder)\n      handleChange(result.value)\n      // Restore cursor\n      requestAnimationFrame(() => {\n        ta.focus()\n        ta.setSelectionRange(result.cursor, result.cursor)\n      })\n    },\n    [handleChange]\n  )\n\n  const toolbarItems = React.useMemo(\n    () => [\n      {\n        icon: Heading1,\n        label: \"Heading\",\n        before: \"# \",\n        after: \"\",\n        placeholder: \"Heading\",\n      },\n      {\n        icon: Bold,\n        label: \"Bold\",\n        before: \"**\",\n        after: \"**\",\n        placeholder: \"bold text\",\n      },\n      {\n        icon: Italic,\n        label: \"Italic\",\n        before: \"*\",\n        after: \"*\",\n        placeholder: \"italic text\",\n      },\n      {\n        icon: Code,\n        label: \"Code\",\n        before: \"`\",\n        after: \"`\",\n        placeholder: \"code\",\n      },\n      {\n        icon: Link,\n        label: \"Link\",\n        before: \"[\",\n        after: \"](url)\",\n        placeholder: \"link text\",\n      },\n      {\n        icon: List,\n        label: \"List\",\n        before: \"- \",\n        after: \"\",\n        placeholder: \"item\",\n      },\n      {\n        icon: ListOrdered,\n        label: \"Ordered list\",\n        before: \"1. \",\n        after: \"\",\n        placeholder: \"item\",\n      },\n      {\n        icon: Quote,\n        label: \"Quote\",\n        before: \"> \",\n        after: \"\",\n        placeholder: \"quote\",\n      },\n    ],\n    []\n  )\n\n  const effectiveHeight = fullscreen ? \"100vh\" : height\n\n  // ── Loading ─────────────────────────────────────────────────────────────\n\n  if (loading) {\n    return (\n      <div className={cn(markdownEditorContainerVariants(), className)}>\n        <div className=\"flex h-10 items-center gap-1 border-b border-border px-2\">\n          {Array.from({ length: 8 }).map((_, i) => (\n            <Skeleton key={i} className=\"size-7 rounded\" />\n          ))}\n        </div>\n        <Skeleton className=\"m-4 rounded\" style={{ height: height - 60 }} />\n      </div>\n    )\n  }\n\n  const preview = (\n    <div\n      className=\"prose prose-sm max-w-none flex-1 overflow-auto p-4 text-foreground\"\n      style={{ minHeight: 0 }}\n      dangerouslySetInnerHTML={{ __html: renderMarkdown(currentValue) }}\n    />\n  )\n\n  const editor = (\n    <textarea\n      ref={textareaRef}\n      value={currentValue}\n      onChange={(e) => handleChange(e.target.value)}\n      placeholder={placeholder}\n      disabled={disabled}\n      className={cn(markdownEditorTextareaVariants(), \"flex-1\")}\n      style={{ minHeight: 0 }}\n      aria-label=\"Markdown editor\"\n      aria-multiline=\"true\"\n    />\n  )\n\n  return (\n    <div\n      className={cn(\n        markdownEditorContainerVariants(),\n        fullscreen && \"fixed inset-0 z-50 rounded-none\",\n        className\n      )}\n      style={{ height: effectiveHeight }}\n      {...props}\n    >\n      {/* Toolbar */}\n      {showToolbar && (\n        <div className=\"flex h-10 items-center gap-0.5 border-b border-border bg-muted/40 px-2\">\n          {toolbarItems.map(\n            ({ icon: Icon, label, before, after, placeholder }) => (\n              <Button\n                key={label}\n                variant=\"ghost\"\n                size=\"icon\"\n                className=\"size-7\"\n                onClick={() => applyFormat(before, after, placeholder)}\n                disabled={disabled || mode === \"preview\"}\n                aria-label={label}\n                title={label}\n              >\n                <Icon className=\"size-3.5\" />\n              </Button>\n            )\n          )}\n          <Separator orientation=\"vertical\" className=\"mx-1 h-5\" />\n          {/* Mode switcher */}\n          <ToggleGroup\n            type=\"single\"\n            size=\"sm\"\n            value={mode}\n            onValueChange={(v) => v && setMode(v as MarkdownEditorMode)}\n            className=\"ml-auto\"\n          >\n            <ToggleGroupItem\n              value=\"edit\"\n              className=\"h-7 px-2 text-xs\"\n              aria-label=\"Edit mode\"\n            >\n              <Type className=\"size-3\" />\n            </ToggleGroupItem>\n            <ToggleGroupItem\n              value=\"split\"\n              className=\"h-7 px-2 text-xs\"\n              aria-label=\"Split mode\"\n            >\n              <SplitSquareHorizontal className=\"size-3\" />\n            </ToggleGroupItem>\n            <ToggleGroupItem\n              value=\"preview\"\n              className=\"h-7 px-2 text-xs\"\n              aria-label=\"Preview mode\"\n            >\n              <Eye className=\"size-3\" />\n            </ToggleGroupItem>\n          </ToggleGroup>\n          <Button\n            variant=\"ghost\"\n            size=\"icon\"\n            className=\"ml-1 size-7\"\n            onClick={() => setFullscreen((f) => !f)}\n            aria-label={fullscreen ? \"Exit fullscreen\" : \"Fullscreen\"}\n          >\n            {fullscreen ? (\n              <Minimize2 className=\"size-3.5\" />\n            ) : (\n              <Maximize2 className=\"size-3.5\" />\n            )}\n          </Button>\n        </div>\n      )}\n\n      {/* Content */}\n      <div\n        className={cn(\n          \"flex\",\n          mode === \"split\" && \"divide-x divide-border\",\n          \"overflow-hidden\"\n        )}\n        style={{ height: `calc(100% - ${showToolbar ? \"2.5rem\" : \"0px\"})` }}\n      >\n        {(mode === \"edit\" || mode === \"split\") && editor}\n        {(mode === \"preview\" || mode === \"split\") && preview}\n      </div>\n    </div>\n  )\n}\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}
