Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

2.9 KB
Raw Blame
import React from 'react';
import remarkGfm from 'remark-gfm';
import supersub from 'remark-supersub';
import ReactMarkdown from 'react-markdown';
import rehypeHighlight from 'rehype-highlight';
import type { PluggableList } from 'unified';
import {
  code,
  codeNoExecution,
  a,
  p,
  img,
  table,
  ul,
  ol,
  h1,
  h2,
  h3,
  h4,
  h5,
  h6,
  blockquote,
} from './MarkdownComponents';
import { langSubset, remarkApproxTilde } from '~/utils';
import { reportBoundaryError } from '~/lib/rum/logs';
import { CodeBlockProvider } from '~/Providers';

interface ErrorBoundaryState {
  hasError: boolean;
  error?: Error;
}

interface MarkdownErrorBoundaryProps {
  children: React.ReactNode;
  content: string;
  codeExecution?: boolean;
}

class MarkdownErrorBoundary extends React.Component<
  MarkdownErrorBoundaryProps,
  ErrorBoundaryState
> {
  constructor(props: MarkdownErrorBoundaryProps) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError(error: Error): ErrorBoundaryState {
    return { hasError: true, error };
  }

  componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
    console.error('Markdown rendering error:', error, errorInfo);
    reportBoundaryError('markdown', error);
  }

  componentDidUpdate(prevProps: MarkdownErrorBoundaryProps) {
    if (prevProps.content !== this.props.content && this.state.hasError) {
      this.setState({ hasError: false, error: undefined });
    }
  }

  render() {
    if (this.state.hasError) {
      const { content, codeExecution = true } = this.props;

      const rehypePlugins: PluggableList = [
        [
          rehypeHighlight,
          {
            detect: true,
            ignoreMissing: true,
            subset: langSubset,
          },
        ],
      ];

      return (
        <CodeBlockProvider>
          <ReactMarkdown
            remarkPlugins={[
              remarkApproxTilde,
              /** @ts-ignore */
              supersub,
              remarkGfm,
            ]}
            /** @ts-ignore */
            rehypePlugins={rehypePlugins}
            components={
              {
                code: codeExecution ? code : codeNoExecution,
                a,
                p,
                /** The recovery path is exactly where a generated reference
                 *  must still resolve; the default `img` would send
                 *  `![chart](chart.png)` to a page-relative URL. */
                img,
                table,
                ul,
                ol,
                h1,
                h2,
                h3,
                h4,
                h5,
                h6,
                blockquote,
              } as {
                [nodeType: string]: React.ElementType;
              }
            }
          >
            {content}
          </ReactMarkdown>
        </CodeBlockProvider>
      );
    }

    return this.props.children;
  }
}

export default MarkdownErrorBoundary;