You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

markdown.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import ReactMarkdown from 'react-markdown'
  2. import ReactEcharts from 'echarts-for-react'
  3. import 'katex/dist/katex.min.css'
  4. import RemarkMath from 'remark-math'
  5. import RemarkBreaks from 'remark-breaks'
  6. import RehypeKatex from 'rehype-katex'
  7. import RemarkGfm from 'remark-gfm'
  8. import RehypeRaw from 'rehype-raw'
  9. import SyntaxHighlighter from 'react-syntax-highlighter'
  10. import {
  11. atelierHeathDark,
  12. atelierHeathLight,
  13. } from 'react-syntax-highlighter/dist/esm/styles/hljs'
  14. import { Component, memo, useMemo, useRef, useState } from 'react'
  15. import { flow } from 'lodash-es'
  16. import ActionButton from '@/app/components/base/action-button'
  17. import CopyIcon from '@/app/components/base/copy-icon'
  18. import SVGBtn from '@/app/components/base/svg'
  19. import Flowchart from '@/app/components/base/mermaid'
  20. import ImageGallery from '@/app/components/base/image-gallery'
  21. import { useChatContext } from '@/app/components/base/chat/chat/context'
  22. import VideoGallery from '@/app/components/base/video-gallery'
  23. import AudioGallery from '@/app/components/base/audio-gallery'
  24. import MarkdownButton from '@/app/components/base/markdown-blocks/button'
  25. import MarkdownForm from '@/app/components/base/markdown-blocks/form'
  26. import ThinkBlock from '@/app/components/base/markdown-blocks/think-block'
  27. import { Theme } from '@/types/app'
  28. import useTheme from '@/hooks/use-theme'
  29. import cn from '@/utils/classnames'
  30. import SVGRenderer from './svg-gallery'
  31. // Available language https://github.com/react-syntax-highlighter/react-syntax-highlighter/blob/master/AVAILABLE_LANGUAGES_HLJS.MD
  32. const capitalizationLanguageNameMap: Record<string, string> = {
  33. sql: 'SQL',
  34. javascript: 'JavaScript',
  35. java: 'Java',
  36. typescript: 'TypeScript',
  37. vbscript: 'VBScript',
  38. css: 'CSS',
  39. html: 'HTML',
  40. xml: 'XML',
  41. php: 'PHP',
  42. python: 'Python',
  43. yaml: 'Yaml',
  44. mermaid: 'Mermaid',
  45. markdown: 'MarkDown',
  46. makefile: 'MakeFile',
  47. echarts: 'ECharts',
  48. shell: 'Shell',
  49. powershell: 'PowerShell',
  50. json: 'JSON',
  51. latex: 'Latex',
  52. svg: 'SVG',
  53. }
  54. const getCorrectCapitalizationLanguageName = (language: string) => {
  55. if (!language)
  56. return 'Plain'
  57. if (language in capitalizationLanguageNameMap)
  58. return capitalizationLanguageNameMap[language]
  59. return language.charAt(0).toUpperCase() + language.substring(1)
  60. }
  61. const preprocessLaTeX = (content: string) => {
  62. if (typeof content !== 'string')
  63. return content
  64. const codeBlockRegex = /```[\s\S]*?```/g
  65. const codeBlocks = content.match(codeBlockRegex) || []
  66. let processedContent = content.replace(codeBlockRegex, 'CODE_BLOCK_PLACEHOLDER')
  67. processedContent = flow([
  68. (str: string) => str.replace(/\\\[(.*?)\\\]/g, (_, equation) => `$$${equation}$$`),
  69. (str: string) => str.replace(/\\\[(.*?)\\\]/gs, (_, equation) => `$$${equation}$$`),
  70. (str: string) => str.replace(/\\\((.*?)\\\)/g, (_, equation) => `$$${equation}$$`),
  71. (str: string) => str.replace(/(^|[^\\])\$(.+?)\$/g, (_, prefix, equation) => `${prefix}$${equation}$`),
  72. ])(processedContent)
  73. codeBlocks.forEach((block) => {
  74. processedContent = processedContent.replace('CODE_BLOCK_PLACEHOLDER', block)
  75. })
  76. return processedContent
  77. }
  78. const preprocessThinkTag = (content: string) => {
  79. const thinkOpenTagRegex = /<think>\n/g
  80. const thinkCloseTagRegex = /\n<\/think>/g
  81. return flow([
  82. (str: string) => str.replace(thinkOpenTagRegex, '<details data-think=true>\n'),
  83. (str: string) => str.replace(thinkCloseTagRegex, '\n[ENDTHINKFLAG]</details>'),
  84. ])(content)
  85. }
  86. export function PreCode(props: { children: any }) {
  87. const ref = useRef<HTMLPreElement>(null)
  88. return (
  89. <pre ref={ref}>
  90. <span
  91. className="copy-code-button"
  92. ></span>
  93. {props.children}
  94. </pre>
  95. )
  96. }
  97. // **Add code block
  98. // Avoid error #185 (Maximum update depth exceeded.
  99. // This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate.
  100. // React limits the number of nested updates to prevent infinite loops.)
  101. // Reference A: https://reactjs.org/docs/error-decoder.html?invariant=185
  102. // Reference B1: https://react.dev/reference/react/memo
  103. // Reference B2: https://react.dev/reference/react/useMemo
  104. // ****
  105. // The original error that occurred in the streaming response during the conversation:
  106. // Error: Minified React error 185;
  107. // visit https://reactjs.org/docs/error-decoder.html?invariant=185 for the full message
  108. // or use the non-minified dev environment for full errors and additional helpful warnings.
  109. const CodeBlock: any = memo(({ inline, className, children, ...props }: any) => {
  110. const { theme } = useTheme()
  111. const [isSVG, setIsSVG] = useState(true)
  112. const match = /language-(\w+)/.exec(className || '')
  113. const language = match?.[1]
  114. const languageShowName = getCorrectCapitalizationLanguageName(language || '')
  115. const chartData = useMemo(() => {
  116. if (language === 'echarts') {
  117. try {
  118. return JSON.parse(String(children).replace(/\n$/, ''))
  119. }
  120. catch { }
  121. }
  122. return JSON.parse('{"title":{"text":"ECharts error - Wrong JSON format."}}')
  123. }, [language, children])
  124. const renderCodeContent = useMemo(() => {
  125. const content = String(children).replace(/\n$/, '')
  126. if (language === 'mermaid' && isSVG) {
  127. return <Flowchart PrimitiveCode={content} />
  128. }
  129. else if (language === 'echarts') {
  130. return (
  131. <div style={{ minHeight: '350px', minWidth: '100%', overflowX: 'scroll' }}>
  132. <ErrorBoundary>
  133. <ReactEcharts option={chartData} style={{ minWidth: '700px' }} />
  134. </ErrorBoundary>
  135. </div>
  136. )
  137. }
  138. else if (language === 'svg' && isSVG) {
  139. return (
  140. <ErrorBoundary>
  141. <SVGRenderer content={content} />
  142. </ErrorBoundary>
  143. )
  144. }
  145. else {
  146. return (
  147. <SyntaxHighlighter
  148. {...props}
  149. style={theme === Theme.light ? atelierHeathLight : atelierHeathDark}
  150. customStyle={{
  151. paddingLeft: 12,
  152. borderBottomLeftRadius: '10px',
  153. borderBottomRightRadius: '10px',
  154. backgroundColor: 'var(--color-components-input-bg-normal)',
  155. }}
  156. language={match?.[1]}
  157. showLineNumbers
  158. PreTag="div"
  159. >
  160. {content}
  161. </SyntaxHighlighter>
  162. )
  163. }
  164. }, [language, match, props, children, chartData, isSVG])
  165. if (inline || !match)
  166. return <code {...props} className={className}>{children}</code>
  167. return (
  168. <div className='relative'>
  169. <div className='flex h-8 items-center justify-between rounded-t-[10px] border-b border-divider-subtle bg-components-input-bg-normal p-1 pl-3'>
  170. <div className='system-xs-semibold-uppercase text-text-secondary'>{languageShowName}</div>
  171. <div className='flex items-center gap-1'>
  172. {(['mermaid', 'svg']).includes(language!) && <SVGBtn isSVG={isSVG} setIsSVG={setIsSVG} />}
  173. <ActionButton>
  174. <CopyIcon content={String(children).replace(/\n$/, '')} />
  175. </ActionButton>
  176. </div>
  177. </div>
  178. {renderCodeContent}
  179. </div>
  180. )
  181. })
  182. CodeBlock.displayName = 'CodeBlock'
  183. const VideoBlock: any = memo(({ node }: any) => {
  184. const srcs = node.children.filter((child: any) => 'properties' in child).map((child: any) => (child as any).properties.src)
  185. if (srcs.length === 0)
  186. return null
  187. return <VideoGallery key={srcs.join()} srcs={srcs} />
  188. })
  189. VideoBlock.displayName = 'VideoBlock'
  190. const AudioBlock: any = memo(({ node }: any) => {
  191. const srcs = node.children.filter((child: any) => 'properties' in child).map((child: any) => (child as any).properties.src)
  192. if (srcs.length === 0)
  193. return null
  194. return <AudioGallery key={srcs.join()} srcs={srcs} />
  195. })
  196. AudioBlock.displayName = 'AudioBlock'
  197. const ScriptBlock = memo(({ node }: any) => {
  198. const scriptContent = node.children[0]?.value || ''
  199. return `<script>${scriptContent}</script>`
  200. })
  201. ScriptBlock.displayName = 'ScriptBlock'
  202. const Paragraph = (paragraph: any) => {
  203. const { node }: any = paragraph
  204. const children_node = node.children
  205. if (children_node && children_node[0] && 'tagName' in children_node[0] && children_node[0].tagName === 'img') {
  206. return (
  207. <div className="markdown-img-wrapper">
  208. <ImageGallery srcs={[children_node[0].properties.src]} />
  209. {
  210. Array.isArray(paragraph.children) && paragraph.children.length > 1 && (
  211. <div className="mt-2">{paragraph.children.slice(1)}</div>
  212. )
  213. }
  214. </div>
  215. )
  216. }
  217. return <p>{paragraph.children}</p>
  218. }
  219. const Img = ({ src }: any) => {
  220. return <div className="markdown-img-wrapper"><ImageGallery srcs={[src]} /></div>
  221. }
  222. const Link = ({ node, ...props }: any) => {
  223. if (node.properties?.href && node.properties.href?.toString().startsWith('abbr')) {
  224. // eslint-disable-next-line react-hooks/rules-of-hooks
  225. const { onSend } = useChatContext()
  226. const hidden_text = decodeURIComponent(node.properties.href.toString().split('abbr:')[1])
  227. return <abbr className="cursor-pointer underline !decoration-primary-700 decoration-dashed" onClick={() => onSend?.(hidden_text)} title={node.children[0]?.value}>{node.children[0]?.value}</abbr>
  228. }
  229. else {
  230. return <a {...props} target="_blank" className="cursor-pointer underline !decoration-primary-700 decoration-dashed">{node.children[0] ? node.children[0]?.value : 'Download'}</a>
  231. }
  232. }
  233. export function Markdown(props: { content: string; className?: string; customDisallowedElements?: string[] }) {
  234. const latexContent = flow([
  235. preprocessThinkTag,
  236. preprocessLaTeX,
  237. ])(props.content)
  238. return (
  239. <div className={cn('markdown-body', '!text-text-primary', props.className)}>
  240. <ReactMarkdown
  241. remarkPlugins={[
  242. RemarkGfm,
  243. [RemarkMath, { singleDollarTextMath: false }],
  244. RemarkBreaks,
  245. ]}
  246. rehypePlugins={[
  247. RehypeKatex,
  248. RehypeRaw as any,
  249. // The Rehype plug-in is used to remove the ref attribute of an element
  250. () => {
  251. return (tree) => {
  252. const iterate = (node: any) => {
  253. if (node.type === 'element' && node.properties?.ref)
  254. delete node.properties.ref
  255. if (node.type === 'element' && !/^[a-z][a-z0-9]*$/i.test(node.tagName)) {
  256. node.type = 'text'
  257. node.value = `<${node.tagName}`
  258. }
  259. if (node.children)
  260. node.children.forEach(iterate)
  261. }
  262. tree.children.forEach(iterate)
  263. }
  264. },
  265. ]}
  266. disallowedElements={['iframe', 'head', 'html', 'meta', 'link', 'style', 'body', ...(props.customDisallowedElements || [])]}
  267. components={{
  268. code: CodeBlock,
  269. img: Img,
  270. video: VideoBlock,
  271. audio: AudioBlock,
  272. a: Link,
  273. p: Paragraph,
  274. button: MarkdownButton,
  275. form: MarkdownForm,
  276. script: ScriptBlock as any,
  277. details: ThinkBlock,
  278. }}
  279. >
  280. {/* Markdown detect has problem. */}
  281. {latexContent}
  282. </ReactMarkdown>
  283. </div>
  284. )
  285. }
  286. // **Add an ECharts runtime error handler
  287. // Avoid error #7832 (Crash when ECharts accesses undefined objects)
  288. // This can happen when a component attempts to access an undefined object that references an unregistered map, causing the program to crash.
  289. export default class ErrorBoundary extends Component {
  290. constructor(props: any) {
  291. super(props)
  292. this.state = { hasError: false }
  293. }
  294. componentDidCatch(error: any, errorInfo: any) {
  295. this.setState({ hasError: true })
  296. console.error(error, errorInfo)
  297. }
  298. render() {
  299. // eslint-disable-next-line ts/ban-ts-comment
  300. // @ts-expect-error
  301. if (this.state.hasError)
  302. return <div>Oops! An error occurred. This could be due to an ECharts runtime error or invalid SVG content. <br />(see the browser console for more information)</div>
  303. // eslint-disable-next-line ts/ban-ts-comment
  304. // @ts-expect-error
  305. return this.props.children
  306. }
  307. }