{"version":3,"file":"CodeBlock.mjs","names":[],"sources":["../../../src/Markdown/components/CodeBlock.tsx"],"sourcesContent":["import { memo } from 'react';\n\nimport type { HighlighterProps } from '@/Highlighter';\nimport { FALLBACK_LANG } from '@/Highlighter/const';\nimport { isFullHtmlDocument } from '@/HtmlPreview/const';\nimport type { HtmlPreviewProps } from '@/HtmlPreview/type';\nimport Pre, { PreHtmlPreview, PreMermaid, PreSingleLine } from '@/mdx/mdxComponents/Pre';\nimport type { MermaidProps } from '@/Mermaid';\n\nconst countLines = (str: string): number => {\n  const regex = /\\n/g;\n  const matches = str.match(regex);\n  return matches ? matches.length : 1;\n};\n\nexport const useCode = (raw: any) => {\n  if (!raw) return;\n\n  const { children = '', className } = raw?.props || { children: '' };\n\n  if (!children) return;\n\n  const content = Array.isArray(children) ? (children[0] as string) : children;\n\n  const lang = className?.replace('language-', '') || FALLBACK_LANG;\n\n  const isSingleLine = countLines(content) <= 1 && content.length <= 32;\n\n  return {\n    content,\n    isSingleLine,\n    lang,\n  };\n};\n\ninterface CodeBlockProps {\n  animated?: boolean;\n  children: any;\n  enableHtmlPreview?: boolean;\n  enableMermaid?: boolean;\n  fullFeatured?: boolean;\n  highlight?: HighlighterProps;\n  html?: HtmlPreviewProps;\n  mermaid?: MermaidProps;\n}\n\nexport const CodeBlock = memo<CodeBlockProps>(\n  ({\n    fullFeatured,\n    enableHtmlPreview,\n    enableMermaid,\n    highlight,\n    html,\n    mermaid,\n    children,\n    animated,\n    ...rest\n  }) => {\n    const code = useCode(children);\n\n    if (!code) return;\n\n    if (enableMermaid && code.lang === 'mermaid')\n      return (\n        <PreMermaid animated={animated} fullFeatured={fullFeatured} {...mermaid} {...rest}>\n          {code.content}\n        </PreMermaid>\n      );\n\n    // Auto-route to HTML preview only for full HTML documents. Fragments fall\n    // through to the normal highlighter — inline-rendering a `<div>` snippet\n    // produces a degraded experience and risks hiding the actual code from\n    // the reader.\n    if (enableHtmlPreview && code.lang === 'html' && isFullHtmlDocument(code.content))\n      return (\n        <PreHtmlPreview animated={animated} fullFeatured={fullFeatured} {...html} {...rest}>\n          {code.content}\n        </PreHtmlPreview>\n      );\n\n    if (!highlight && code.isSingleLine)\n      return <PreSingleLine language={code.lang}>{code.content}</PreSingleLine>;\n\n    return (\n      <Pre\n        animated={animated}\n        fullFeatured={fullFeatured}\n        language={code.lang}\n        {...highlight}\n        {...rest}\n      >\n        {code.content}\n      </Pre>\n    );\n  },\n  (prevProps, nextProps) => prevProps.children === nextProps.children,\n);\n"],"mappings":";;;;;;AASA,MAAM,cAAc,QAAwB;CAE1C,MAAM,UAAU,IAAI,MAAM,MAAM;AAChC,QAAO,UAAU,QAAQ,SAAS;;AAGpC,MAAa,WAAW,QAAa;AACnC,KAAI,CAAC,IAAK;CAEV,MAAM,EAAE,WAAW,IAAI,cAAc,KAAK,SAAS,EAAE,UAAU,IAAI;AAEnE,KAAI,CAAC,SAAU;CAEf,MAAM,UAAU,MAAM,QAAQ,SAAS,GAAI,SAAS,KAAgB;CAEpE,MAAM,OAAO,WAAW,QAAQ,aAAa,GAAG,IAAA;AAIhD,QAAO;EACL;EACA,cAJmB,WAAW,QAAQ,IAAI,KAAK,QAAQ,UAAU;EAKjE;EACD;;AAcH,MAAa,YAAY,MACtB,EACC,cACA,mBACA,eACA,WACA,MACA,SACA,UACA,UACA,GAAG,WACC;CACJ,MAAM,OAAO,QAAQ,SAAS;AAE9B,KAAI,CAAC,KAAM;AAEX,KAAI,iBAAiB,KAAK,SAAS,UACjC,QACE,oBAAC,YAAD;EAAsB;EAAwB;EAAc,GAAI;EAAS,GAAI;YAC1E,KAAK;EACK,CAAA;AAOjB,KAAI,qBAAqB,KAAK,SAAS,UAAU,mBAAmB,KAAK,QAAQ,CAC/E,QACE,oBAAC,gBAAD;EAA0B;EAAwB;EAAc,GAAI;EAAM,GAAI;YAC3E,KAAK;EACS,CAAA;AAGrB,KAAI,CAAC,aAAa,KAAK,aACrB,QAAO,oBAAC,eAAD;EAAe,UAAU,KAAK;YAAO,KAAK;EAAwB,CAAA;AAE3E,QACE,oBAAC,KAAD;EACY;EACI;EACd,UAAU,KAAK;EACf,GAAI;EACJ,GAAI;YAEH,KAAK;EACF,CAAA;IAGT,WAAW,cAAc,UAAU,aAAa,UAAU,SAC5D"}