{"version":3,"file":"AIMarkdown.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/AIMarkdown/AIMarkdown.tsx"],"sourcesContent":["import React, {\n  Children,\n  type ComponentProps,\n  type ComponentType,\n  type ElementType,\n  isValidElement,\n  useContext,\n  useMemo,\n} from 'react';\nimport ReactMarkdown, { type Components, type ExtraProps } from 'react-markdown';\nimport remarkGfm from 'remark-gfm';\nimport { Prism, type SyntaxHighlighterProps } from 'react-syntax-highlighter';\nimport clsx from 'clsx';\n\nimport { JsonChart } from './tools/charts/JsonChart';\nimport { SuspendedChart } from './tools/charts/SuspendedChart';\nimport { AIThemeScope } from '../themeScope';\n\ntype CodeElementProps = {\n  children?: React.ReactNode;\n  className?: string;\n  node?: { tagName?: string };\n};\n\nconst SyntaxHighlighter = Prism as unknown as ComponentType<SyntaxHighlighterProps>;\n\nconst getToolOrLanguage = (className: string = '') =>\n  className.match(/language-(?<tool>[\\w-]+)/)?.groups?.['tool'];\n\ntype ToolComponents = {\n  [key in string]?: ComponentType<{\n    data: string;\n    fallback: React.ReactNode;\n  }>;\n};\n\ntype ToolComponent = NonNullable<ToolComponents[string]>;\n\nexport type ToolComponentProps = ToolComponent extends ComponentType<infer P> ? P : never;\n\ntype MarkdownComponents = Components;\n\nconst AIMarkdownContext = React.createContext<{\n  toolComponents: ToolComponents;\n}>({ toolComponents: {} });\n\ntype BaseDefaultPreProps = ComponentProps<'pre'> & ExtraProps;\n\ntype DefaultPreProps = BaseDefaultPreProps & {\n  Pre?: ComponentType<BaseDefaultPreProps> | ElementType;\n};\n\n// True inside a language-less <pre>, so DefaultCode can tell a fenced block from inline code\n// (react-markdown v9 has no `inline` prop).\nconst InsidePreContext = React.createContext(false);\n\nconst DefaultPre = (props: DefaultPreProps) => {\n  const { children, className, node, Pre = 'pre', ...restProps } = props;\n\n  const { toolComponents } = useContext(AIMarkdownContext);\n\n  const [codeElement] = Children.toArray(children);\n\n  if (\n    isValidElement<CodeElementProps>(codeElement) &&\n    codeElement.props.node?.tagName === 'code'\n  ) {\n    const toolOrLanguage = getToolOrLanguage(codeElement.props.className);\n\n    const fallback = <>{children}</>;\n\n    const Component =\n      typeof toolOrLanguage === 'string' ? toolComponents[toolOrLanguage] : null;\n\n    if (Component) {\n      // TODO: forward metadata\n\n      return (\n        <Component data={codeElement.props.children as string} fallback={fallback} />\n      );\n    }\n\n    // Only the code content: DefaultCode renders it with SyntaxHighlighter, which adds its own pre.\n    if (toolOrLanguage) {\n      return fallback;\n    }\n  }\n\n  // No tool or language: render a regular pre block.\n  return (\n    <Pre\n      className={clsx(className, 'str-chat__ai-pre')}\n      // `node` is react-markdown metadata: forward it to components, never to DOM elements\n      {...(typeof Pre === 'string' ? {} : { node })}\n      {...restProps}\n    >\n      <InsidePreContext.Provider value={true}>{children}</InsidePreContext.Provider>\n    </Pre>\n  );\n};\n\nconst Code = ({\n  children,\n  className,\n  // eslint-disable-next-line @typescript-eslint/no-unused-vars -- strip `style` so it is not forwarded to <code>\n  style: _style,\n  ...restProps\n}: ComponentProps<'code'>) => (\n  <code\n    className={clsx('str-chat__ai-syntax-highlighter-code', className)}\n    {...restProps}\n  >\n    {children}\n  </code>\n);\n\nconst Pre = ({ children, className, ...restProps }: ComponentProps<'pre'>) => (\n  <pre className={clsx('str-chat__ai-syntax-highlighter-pre', className)} {...restProps}>\n    {children}\n  </pre>\n);\n\nconst DefaultSyntaxHighlighter = ({ children, language }: BaseDefaultCodeProps) => (\n  <SyntaxHighlighter\n    CodeTag={Code as SyntaxHighlighterProps['CodeTag']}\n    language={language}\n    PreTag={Pre as SyntaxHighlighterProps['PreTag']}\n    showLineNumbers\n    useInlineStyles={false}\n  >\n    {children as string}\n  </SyntaxHighlighter>\n);\n\ntype BaseDefaultCodeProps = ComponentProps<'code'> &\n  ExtraProps & { language?: string; inline?: boolean };\n\ntype DefaultCodeProps = BaseDefaultCodeProps & {\n  Code?: ComponentType<BaseDefaultCodeProps> | ElementType;\n  SyntaxHighlighter?: ComponentType<BaseDefaultCodeProps>;\n};\n\nconst DefaultCode = (props: DefaultCodeProps) => {\n  const {\n    children,\n    className,\n    Code = 'code',\n    node,\n    SyntaxHighlighter = DefaultSyntaxHighlighter,\n    ...restProps\n  } = props;\n\n  const insidePre = useContext(InsidePreContext);\n  const language = getToolOrLanguage(className);\n  const inline = !language && !insidePre;\n\n  const Component = language ? SyntaxHighlighter : Code;\n\n  return (\n    <Component\n      className={clsx(className, 'str-chat__ai-code')}\n      {...(typeof Component === 'string'\n        ? {\n            'data-inline': inline ? 'true' : 'false',\n            'data-language': language,\n          }\n        : { inline, language, node })}\n      {...restProps}\n    >\n      {children}\n    </Component>\n  );\n};\n\nconst DefaultComponents = {\n  code: DefaultCode,\n  pre: DefaultPre,\n} as const;\n\ninterface AIMarkdown {\n  (props: {\n    children: string;\n    toolComponents?: ToolComponents;\n    markdownComponents?: MarkdownComponents;\n  }): React.JSX.Element;\n  default: typeof DefaultComponents;\n}\n\nexport const AIMarkdown: AIMarkdown = (props) => {\n  const mergedMarkdownComponents: MarkdownComponents = useMemo(\n    () => ({\n      ...DefaultComponents,\n      ...props.markdownComponents,\n    }),\n    [props.markdownComponents],\n  );\n\n  const mergedToolComponents: ToolComponents = useMemo(\n    () => ({\n      chartjs: SuspendedChart,\n      json: JsonChart,\n      ...props.toolComponents,\n    }),\n    [props.toolComponents],\n  );\n\n  return (\n    // No single root element of its own, so the scope is a layout-neutral wrapper, added only\n    // when nothing above provides one.\n    <AIThemeScope>\n      <AIMarkdownContext.Provider value={{ toolComponents: mergedToolComponents }}>\n        <ReactMarkdown components={mergedMarkdownComponents} remarkPlugins={[remarkGfm]}>\n          {props.children}\n        </ReactMarkdown>\n      </AIMarkdownContext.Provider>\n    </AIThemeScope>\n  );\n};\n\nAIMarkdown.default = DefaultComponents;\n"],"mappings":";;;;;;;;;;AAwBA,IAAM,oBAAoB;AAE1B,IAAM,qBAAqB,YAAoB,OAC7C,UAAU,MAAM,0BAA0B,CAAC,EAAE,SAAS;AAexD,IAAM,oB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