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 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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, useEffect, 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 MarkdownMusic from '@/app/components/base/markdown-blocks/music'
  27. import ThinkBlock from '@/app/components/base/markdown-blocks/think-block'
  28. import { Theme } from '@/types/app'
  29. import useTheme from '@/hooks/use-theme'
  30. import cn from '@/utils/classnames'
  31. import SVGRenderer from './svg-gallery'
  32. // Available language https://github.com/react-syntax-highlighter/react-syntax-highlighter/blob/master/AVAILABLE_LANGUAGES_HLJS.MD
  33. const capitalizationLanguageNameMap: Record<string, string> = {
  34. sql: 'SQL',
  35. javascript: 'JavaScript',
  36. java: 'Java',
  37. typescript: 'TypeScript',
  38. vbscript: 'VBScript',
  39. css: 'CSS',
  40. html: 'HTML',
  41. xml: 'XML',
  42. php: 'PHP',
  43. python: 'Python',
  44. yaml: 'Yaml',
  45. mermaid: 'Mermaid',
  46. markdown: 'MarkDown',
  47. makefile: 'MakeFile',
  48. echarts: 'ECharts',
  49. shell: 'Shell',
  50. powershell: 'PowerShell',
  51. json: 'JSON',
  52. latex: 'Latex',
  53. svg: 'SVG',
  54. abc: 'ABC',
  55. }
  56. const getCorrectCapitalizationLanguageName = (language: string) => {
  57. if (!language)
  58. return 'Plain'
  59. if (language in capitalizationLanguageNameMap)
  60. return capitalizationLanguageNameMap[language]
  61. return language.charAt(0).toUpperCase() + language.substring(1)
  62. }
  63. const preprocessLaTeX = (content: string) => {
  64. if (typeof content !== 'string')
  65. return content
  66. const codeBlockRegex = /```[\s\S]*?```/g
  67. const codeBlocks = content.match(codeBlockRegex) || []
  68. let processedContent = content.replace(codeBlockRegex, 'CODE_BLOCK_PLACEHOLDER')
  69. processedContent = flow([
  70. (str: string) => str.replace(/\\\[(.*?)\\\]/g, (_, equation) => `$$${equation}$$`),
  71. (str: string) => str.replace(/\\\[([\s\S]*?)\\\]/g, (_, equation) => `$$${equation}$$`),
  72. (str: string) => str.replace(/\\\((.*?)\\\)/g, (_, equation) => `$$${equation}$$`),
  73. (str: string) => str.replace(/(^|[^\\])\$(.+?)\$/g, (_, prefix, equation) => `${prefix}$${equation}$`),
  74. ])(processedContent)
  75. codeBlocks.forEach((block) => {
  76. processedContent = processedContent.replace('CODE_BLOCK_PLACEHOLDER', block)
  77. })
  78. return processedContent
  79. }
  80. const preprocessThinkTag = (content: string) => {
  81. const thinkOpenTagRegex = /<think>\n/g
  82. const thinkCloseTagRegex = /\n<\/think>/g
  83. return flow([
  84. (str: string) => str.replace(thinkOpenTagRegex, '<details data-think=true>\n'),
  85. (str: string) => str.replace(thinkCloseTagRegex, '\n[ENDTHINKFLAG]</details>'),
  86. ])(content)
  87. }
  88. export function PreCode(props: { children: any }) {
  89. const ref = useRef<HTMLPreElement>(null)
  90. return (
  91. <pre ref={ref}>
  92. <span
  93. className="copy-code-button"
  94. ></span>
  95. {props.children}
  96. </pre>
  97. )
  98. }
  99. // **Add code block
  100. // Avoid error #185 (Maximum update depth exceeded.
  101. // This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate.
  102. // React limits the number of nested updates to prevent infinite loops.)
  103. // Reference A: https://reactjs.org/docs/error-decoder.html?invariant=185
  104. // Reference B1: https://react.dev/reference/react/memo
  105. // Reference B2: https://react.dev/reference/react/useMemo
  106. // ****
  107. // The original error that occurred in the streaming response during the conversation:
  108. // Error: Minified React error 185;
  109. // visit https://reactjs.org/docs/error-decoder.html?invariant=185 for the full message
  110. // or use the non-minified dev environment for full errors and additional helpful warnings.
  111. const CodeBlock: any = memo(({ inline, className, children = '', ...props }: any) => {
  112. const { theme } = useTheme()
  113. const [isSVG, setIsSVG] = useState(true)
  114. const [chartState, setChartState] = useState<'loading' | 'success' | 'error'>('loading')
  115. const [finalChartOption, setFinalChartOption] = useState<any>(null)
  116. const echartsRef = useRef<any>(null)
  117. const contentRef = useRef<string>('')
  118. const processedRef = useRef<boolean>(false) // Track if content was successfully processed
  119. const match = /language-(\w+)/.exec(className || '')
  120. const language = match?.[1]
  121. const languageShowName = getCorrectCapitalizationLanguageName(language || '')
  122. const isDarkMode = theme === Theme.dark
  123. const echartsStyle = useMemo(() => ({
  124. height: '350px',
  125. width: '100%',
  126. }), [])
  127. const echartsOpts = useMemo(() => ({
  128. renderer: 'canvas',
  129. width: 'auto',
  130. }) as any, [])
  131. const echartsOnEvents = useMemo(() => ({
  132. finished: () => {
  133. const instance = echartsRef.current?.getEchartsInstance?.()
  134. if (instance)
  135. instance.resize()
  136. },
  137. }), [echartsRef]) // echartsRef is stable, so this effectively runs once.
  138. // Handle container resize for echarts
  139. useEffect(() => {
  140. if (language !== 'echarts' || !echartsRef.current) return
  141. const handleResize = () => {
  142. // This gets the echarts instance from the component
  143. const instance = echartsRef.current?.getEchartsInstance?.()
  144. if (instance)
  145. instance.resize()
  146. }
  147. window.addEventListener('resize', handleResize)
  148. // Also manually trigger resize after a short delay to ensure proper sizing
  149. const resizeTimer = setTimeout(handleResize, 200)
  150. return () => {
  151. window.removeEventListener('resize', handleResize)
  152. clearTimeout(resizeTimer)
  153. }
  154. }, [language, echartsRef.current])
  155. // Process chart data when content changes
  156. useEffect(() => {
  157. // Only process echarts content
  158. if (language !== 'echarts') return
  159. // Reset state when new content is detected
  160. if (!contentRef.current) {
  161. setChartState('loading')
  162. processedRef.current = false
  163. }
  164. const newContent = String(children).replace(/\n$/, '')
  165. // Skip if content hasn't changed
  166. if (contentRef.current === newContent) return
  167. contentRef.current = newContent
  168. const trimmedContent = newContent.trim()
  169. if (!trimmedContent) return
  170. // Detect if this is historical data (already complete)
  171. // Historical data typically comes as a complete code block with complete JSON
  172. const isCompleteJson
  173. = (trimmedContent.startsWith('{') && trimmedContent.endsWith('}')
  174. && trimmedContent.split('{').length === trimmedContent.split('}').length)
  175. || (trimmedContent.startsWith('[') && trimmedContent.endsWith(']')
  176. && trimmedContent.split('[').length === trimmedContent.split(']').length)
  177. // If the JSON structure looks complete, try to parse it right away
  178. if (isCompleteJson && !processedRef.current) {
  179. try {
  180. const parsed = JSON.parse(trimmedContent)
  181. if (typeof parsed === 'object' && parsed !== null) {
  182. setFinalChartOption(parsed)
  183. setChartState('success')
  184. processedRef.current = true
  185. return
  186. }
  187. }
  188. catch {
  189. try {
  190. // eslint-disable-next-line no-new-func, sonarjs/code-eval
  191. const result = new Function(`return ${trimmedContent}`)()
  192. if (typeof result === 'object' && result !== null) {
  193. setFinalChartOption(result)
  194. setChartState('success')
  195. processedRef.current = true
  196. return
  197. }
  198. }
  199. catch {
  200. // If we have a complete JSON structure but it doesn't parse,
  201. // it's likely an error rather than incomplete data
  202. setChartState('error')
  203. processedRef.current = true
  204. return
  205. }
  206. }
  207. }
  208. // If we get here, either the JSON isn't complete yet, or we failed to parse it
  209. // Check more conditions for streaming data
  210. const isIncomplete
  211. = trimmedContent.length < 5
  212. || (trimmedContent.startsWith('{')
  213. && (!trimmedContent.endsWith('}')
  214. || trimmedContent.split('{').length !== trimmedContent.split('}').length))
  215. || (trimmedContent.startsWith('[')
  216. && (!trimmedContent.endsWith(']')
  217. || trimmedContent.split('[').length !== trimmedContent.split('}').length))
  218. || (trimmedContent.split('"').length % 2 !== 1)
  219. || (trimmedContent.includes('{"') && !trimmedContent.includes('"}'))
  220. // Only try to parse streaming data if it looks complete and hasn't been processed
  221. if (!isIncomplete && !processedRef.current) {
  222. let isValidOption = false
  223. try {
  224. const parsed = JSON.parse(trimmedContent)
  225. if (typeof parsed === 'object' && parsed !== null) {
  226. setFinalChartOption(parsed)
  227. isValidOption = true
  228. }
  229. }
  230. catch {
  231. try {
  232. // eslint-disable-next-line no-new-func, sonarjs/code-eval
  233. const result = new Function(`return ${trimmedContent}`)()
  234. if (typeof result === 'object' && result !== null) {
  235. setFinalChartOption(result)
  236. isValidOption = true
  237. }
  238. }
  239. catch {
  240. // Both parsing methods failed, but content looks complete
  241. setChartState('error')
  242. processedRef.current = true
  243. }
  244. }
  245. if (isValidOption) {
  246. setChartState('success')
  247. processedRef.current = true
  248. }
  249. }
  250. }, [language, children])
  251. const renderCodeContent = useMemo(() => {
  252. const content = String(children).replace(/\n$/, '')
  253. switch (language) {
  254. case 'mermaid':
  255. if (isSVG)
  256. return <Flowchart PrimitiveCode={content} />
  257. break
  258. case 'echarts': {
  259. // Loading state: show loading indicator
  260. if (chartState === 'loading') {
  261. return (
  262. <div style={{
  263. minHeight: '350px',
  264. width: '100%',
  265. display: 'flex',
  266. flexDirection: 'column',
  267. alignItems: 'center',
  268. justifyContent: 'center',
  269. borderBottomLeftRadius: '10px',
  270. borderBottomRightRadius: '10px',
  271. backgroundColor: isDarkMode ? 'var(--color-components-input-bg-normal)' : 'transparent',
  272. color: 'var(--color-text-secondary)',
  273. }}>
  274. <div style={{
  275. marginBottom: '12px',
  276. width: '24px',
  277. height: '24px',
  278. }}>
  279. {/* Rotating spinner that works in both light and dark modes */}
  280. <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" style={{ animation: 'spin 1.5s linear infinite' }}>
  281. <style>
  282. {`
  283. @keyframes spin {
  284. 0% { transform: rotate(0deg); }
  285. 100% { transform: rotate(360deg); }
  286. }
  287. `}
  288. </style>
  289. <circle opacity="0.2" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="2" />
  290. <path d="M12 2C6.47715 2 2 6.47715 2 12" stroke="currentColor" strokeWidth="2" strokeLinecap="round" />
  291. </svg>
  292. </div>
  293. <div style={{
  294. fontFamily: 'var(--font-family)',
  295. fontSize: '14px',
  296. }}>Chart loading...</div>
  297. </div>
  298. )
  299. }
  300. // Success state: show the chart
  301. if (chartState === 'success' && finalChartOption) {
  302. return (
  303. <div style={{
  304. minWidth: '300px',
  305. minHeight: '350px',
  306. width: '100%',
  307. overflowX: 'auto',
  308. borderBottomLeftRadius: '10px',
  309. borderBottomRightRadius: '10px',
  310. transition: 'background-color 0.3s ease',
  311. }}>
  312. <ErrorBoundary>
  313. <ReactEcharts
  314. ref={echartsRef}
  315. option={finalChartOption}
  316. style={echartsStyle}
  317. theme={isDarkMode ? 'dark' : undefined}
  318. opts={echartsOpts}
  319. notMerge={true}
  320. onEvents={echartsOnEvents}
  321. />
  322. </ErrorBoundary>
  323. </div>
  324. )
  325. }
  326. // Error state: show error message
  327. const errorOption = {
  328. title: {
  329. text: 'ECharts error - Wrong option.',
  330. },
  331. }
  332. return (
  333. <div style={{
  334. minWidth: '300px',
  335. minHeight: '350px',
  336. width: '100%',
  337. overflowX: 'auto',
  338. borderBottomLeftRadius: '10px',
  339. borderBottomRightRadius: '10px',
  340. transition: 'background-color 0.3s ease',
  341. }}>
  342. <ErrorBoundary>
  343. <ReactEcharts
  344. ref={echartsRef}
  345. option={errorOption}
  346. style={echartsStyle}
  347. theme={isDarkMode ? 'dark' : undefined}
  348. opts={echartsOpts}
  349. notMerge={true}
  350. />
  351. </ErrorBoundary>
  352. </div>
  353. )
  354. }
  355. case 'svg':
  356. if (isSVG) {
  357. return (
  358. <ErrorBoundary>
  359. <SVGRenderer content={content} />
  360. </ErrorBoundary>
  361. )
  362. }
  363. break
  364. case 'abc':
  365. return (
  366. <ErrorBoundary>
  367. <MarkdownMusic children={content} />
  368. </ErrorBoundary>
  369. )
  370. default:
  371. return (
  372. <SyntaxHighlighter
  373. {...props}
  374. style={theme === Theme.light ? atelierHeathLight : atelierHeathDark}
  375. customStyle={{
  376. paddingLeft: 12,
  377. borderBottomLeftRadius: '10px',
  378. borderBottomRightRadius: '10px',
  379. backgroundColor: 'var(--color-components-input-bg-normal)',
  380. }}
  381. language={match?.[1]}
  382. showLineNumbers
  383. PreTag="div"
  384. >
  385. {content}
  386. </SyntaxHighlighter>
  387. )
  388. }
  389. }, [children, language, isSVG, finalChartOption, props, theme, match, chartState, isDarkMode, echartsStyle, echartsOpts, echartsOnEvents])
  390. if (inline || !match)
  391. return <code {...props} className={className}>{children}</code>
  392. return (
  393. <div className='relative'>
  394. <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'>
  395. <div className='system-xs-semibold-uppercase text-text-secondary'>{languageShowName}</div>
  396. <div className='flex items-center gap-1'>
  397. {(['mermaid', 'svg']).includes(language!) && <SVGBtn isSVG={isSVG} setIsSVG={setIsSVG} />}
  398. <ActionButton>
  399. <CopyIcon content={String(children).replace(/\n$/, '')} />
  400. </ActionButton>
  401. </div>
  402. </div>
  403. {renderCodeContent}
  404. </div>
  405. )
  406. })
  407. CodeBlock.displayName = 'CodeBlock'
  408. const VideoBlock: any = memo(({ node }: any) => {
  409. const srcs = node.children.filter((child: any) => 'properties' in child).map((child: any) => (child as any).properties.src)
  410. if (srcs.length === 0) {
  411. const src = node.properties?.src
  412. if (src)
  413. return <VideoGallery key={src} srcs={[src]} />
  414. return null
  415. }
  416. return <VideoGallery key={srcs.join()} srcs={srcs} />
  417. })
  418. VideoBlock.displayName = 'VideoBlock'
  419. const AudioBlock: any = memo(({ node }: any) => {
  420. const srcs = node.children.filter((child: any) => 'properties' in child).map((child: any) => (child as any).properties.src)
  421. if (srcs.length === 0) {
  422. const src = node.properties?.src
  423. if (src)
  424. return <AudioGallery key={src} srcs={[src]} />
  425. return null
  426. }
  427. return <AudioGallery key={srcs.join()} srcs={srcs} />
  428. })
  429. AudioBlock.displayName = 'AudioBlock'
  430. const ScriptBlock = memo(({ node }: any) => {
  431. const scriptContent = node.children[0]?.value || ''
  432. return `<script>${scriptContent}</script>`
  433. })
  434. ScriptBlock.displayName = 'ScriptBlock'
  435. const Paragraph = (paragraph: any) => {
  436. const { node }: any = paragraph
  437. const children_node = node.children
  438. if (children_node && children_node[0] && 'tagName' in children_node[0] && children_node[0].tagName === 'img') {
  439. return (
  440. <div className="markdown-img-wrapper">
  441. <ImageGallery srcs={[children_node[0].properties.src]} />
  442. {
  443. Array.isArray(paragraph.children) && paragraph.children.length > 1 && (
  444. <div className="mt-2">{paragraph.children.slice(1)}</div>
  445. )
  446. }
  447. </div>
  448. )
  449. }
  450. return <p>{paragraph.children}</p>
  451. }
  452. const Img = ({ src }: any) => {
  453. return <div className="markdown-img-wrapper"><ImageGallery srcs={[src]} /></div>
  454. }
  455. const Link = ({ node, children, ...props }: any) => {
  456. if (node.properties?.href && node.properties.href?.toString().startsWith('abbr')) {
  457. // eslint-disable-next-line react-hooks/rules-of-hooks
  458. const { onSend } = useChatContext()
  459. const hidden_text = decodeURIComponent(node.properties.href.toString().split('abbr:')[1])
  460. 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>
  461. }
  462. else {
  463. return <a {...props} target="_blank" className="cursor-pointer underline !decoration-primary-700 decoration-dashed">{children || 'Download'}</a>
  464. }
  465. }
  466. export function Markdown(props: { content: string; className?: string; customDisallowedElements?: string[] }) {
  467. const latexContent = flow([
  468. preprocessThinkTag,
  469. preprocessLaTeX,
  470. ])(props.content)
  471. return (
  472. <div className={cn('markdown-body', '!text-text-primary', props.className)}>
  473. <ReactMarkdown
  474. remarkPlugins={[
  475. RemarkGfm,
  476. [RemarkMath, { singleDollarTextMath: false }],
  477. RemarkBreaks,
  478. ]}
  479. rehypePlugins={[
  480. RehypeKatex,
  481. RehypeRaw as any,
  482. // The Rehype plug-in is used to remove the ref attribute of an element
  483. () => {
  484. return (tree) => {
  485. const iterate = (node: any) => {
  486. if (node.type === 'element' && node.properties?.ref)
  487. delete node.properties.ref
  488. if (node.type === 'element' && !/^[a-z][a-z0-9]*$/i.test(node.tagName)) {
  489. node.type = 'text'
  490. node.value = `<${node.tagName}`
  491. }
  492. if (node.children)
  493. node.children.forEach(iterate)
  494. }
  495. tree.children.forEach(iterate)
  496. }
  497. },
  498. ]}
  499. disallowedElements={['iframe', 'head', 'html', 'meta', 'link', 'style', 'body', ...(props.customDisallowedElements || [])]}
  500. components={{
  501. code: CodeBlock,
  502. img: Img,
  503. video: VideoBlock,
  504. audio: AudioBlock,
  505. a: Link,
  506. p: Paragraph,
  507. button: MarkdownButton,
  508. form: MarkdownForm,
  509. script: ScriptBlock as any,
  510. details: ThinkBlock,
  511. }}
  512. >
  513. {/* Markdown detect has problem. */}
  514. {latexContent}
  515. </ReactMarkdown>
  516. </div>
  517. )
  518. }
  519. // **Add an ECharts runtime error handler
  520. // Avoid error #7832 (Crash when ECharts accesses undefined objects)
  521. // This can happen when a component attempts to access an undefined object that references an unregistered map, causing the program to crash.
  522. export default class ErrorBoundary extends Component {
  523. constructor(props: any) {
  524. super(props)
  525. this.state = { hasError: false }
  526. }
  527. componentDidCatch(error: any, errorInfo: any) {
  528. this.setState({ hasError: true })
  529. console.error(error, errorInfo)
  530. }
  531. render() {
  532. // eslint-disable-next-line ts/ban-ts-comment
  533. // @ts-expect-error
  534. if (this.state.hasError)
  535. 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>
  536. // eslint-disable-next-line ts/ban-ts-comment
  537. // @ts-expect-error
  538. return this.props.children
  539. }
  540. }