import React, { memo, useMemo } from 'react'; import remarkGfm from 'remark-gfm'; import remarkMath from 'remark-math'; import supersub from 'remark-supersub'; import rehypeKatex from 'rehype-katex'; import { useRecoilValue } from 'recoil'; import ReactMarkdown from 'react-markdown'; import rehypeHighlight from 'rehype-highlight'; import remarkDirective from 'remark-directive'; import type { Pluggable } from 'unified'; import { Citation, CompositeCitation, HighlightedText } from '~/components/Web/Citation'; import { Artifact, artifactPlugin } from '~/components/Artifacts/Artifact'; import { ArtifactProvider, CodeBlockProvider } from '~/Providers'; import MarkdownErrorBoundary from './MarkdownErrorBoundary'; import { langSubset, preprocessLaTeX } from '~/utils'; import { unicodeCitation } from '~/components/Web'; import { code, a, p, img } from './MarkdownComponents'; import store from '~/store'; type TContentProps = { content: string; isLatestMessage: boolean; }; const Markdown = memo(({ content = '', isLatestMessage }: TContentProps) => { const LaTeXParsing = useRecoilValue(store.LaTeXParsing); const isInitializing = content === ''; const currentContent = useMemo(() => { if (isInitializing) { return ''; } return LaTeXParsing ? preprocessLaTeX(content) : content; }, [content, LaTeXParsing, isInitializing]); const rehypePlugins = useMemo( () => [ [rehypeKatex], [ rehypeHighlight, { detect: true, ignoreMissing: true, subset: langSubset, }, ], ], [], ); const remarkPlugins: Pluggable[] = [ supersub, remarkGfm, remarkDirective, artifactPlugin, [remarkMath, { singleDollarTextMath: false }], unicodeCitation, ]; if (isInitializing) { return (

); } return ( {currentContent} ); }); export default Markdown;