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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 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(/\\\[(.*?)\\\]/gs, (_, 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 match = /language-(\w+)/.exec(className || '')
  115. const language = match?.[1]
  116. const languageShowName = getCorrectCapitalizationLanguageName(language || '')
  117. const chartData = useMemo(() => {
  118. if (language === 'echarts') {
  119. try {
  120. return JSON.parse(String(children).replace(/\n$/, ''))
  121. }
  122. catch { }
  123. }
  124. return JSON.parse('{"title":{"text":"ECharts error - Wrong JSON format."}}')
  125. }, [language, children])
  126. const renderCodeContent = useMemo(() => {
  127. const content = String(children).replace(/\n$/, '')
  128. switch (language) {
  129. case 'mermaid':
  130. if (isSVG)
  131. return <Flowchart PrimitiveCode={content} />
  132. break
  133. case 'echarts':
  134. return (
  135. <div style={{ minHeight: '350px', minWidth: '100%', overflowX: 'scroll' }}>
  136. <ErrorBoundary>
  137. <ReactEcharts option={chartData} style={{ minWidth: '700px' }} />
  138. </ErrorBoundary>
  139. </div>
  140. )
  141. case 'svg':
  142. if (isSVG) {
  143. return (
  144. <ErrorBoundary>
  145. <SVGRenderer content={content} />
  146. </ErrorBoundary>
  147. )
  148. }
  149. break
  150. case 'abc':
  151. return (
  152. <ErrorBoundary>
  153. <MarkdownMusic children={content} />
  154. </ErrorBoundary>
  155. )
  156. default:
  157. return (
  158. <SyntaxHighlighter
  159. {...props}
  160. style={theme === Theme.light ? atelierHeathLight : atelierHeathDark}
  161. customStyle={{
  162. paddingLeft: 12,
  163. borderBottomLeftRadius: '10px',
  164. borderBottomRightRadius: '10px',
  165. backgroundColor: 'var(--color-components-input-bg-normal)',
  166. }}
  167. language={match?.[1]}
  168. showLineNumbers
  169. PreTag="div"
  170. >
  171. {content}
  172. </SyntaxHighlighter>
  173. )
  174. }
  175. }, [children, language, isSVG, chartData, props, theme, match])
  176. if (inline || !match)
  177. return <code {...props} className={className}>{children}</code>
  178. return (
  179. <div className='relative'>
  180. <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'>
  181. <div className='system-xs-semibold-uppercase text-text-secondary'>{languageShowName}</div>
  182. <div className='flex items-center gap-1'>
  183. {(['mermaid', 'svg']).includes(language!) && <SVGBtn isSVG={isSVG} setIsSVG={setIsSVG} />}
  184. <ActionButton>
  185. <CopyIcon content={String(children).replace(/\n$/, '')} />
  186. </ActionButton>
  187. </div>
  188. </div>
  189. {renderCodeContent}
  190. </div>
  191. )
  192. })
  193. CodeBlock.displayName = 'CodeBlock'
  194. const VideoBlock: any = memo(({ node }: any) => {
  195. const srcs = node.children.filter((child: any) => 'properties' in child).map((child: any) => (child as any).properties.src)
  196. if (srcs.length === 0)
  197. return null
  198. return <VideoGallery key={srcs.join()} srcs={srcs} />
  199. })
  200. VideoBlock.displayName = 'VideoBlock'
  201. const AudioBlock: any = memo(({ node }: any) => {
  202. const srcs = node.children.filter((child: any) => 'properties' in child).map((child: any) => (child as any).properties.src)
  203. if (srcs.length === 0)
  204. return null
  205. return <AudioGallery key={srcs.join()} srcs={srcs} />
  206. })
  207. AudioBlock.displayName = 'AudioBlock'
  208. const ScriptBlock = memo(({ node }: any) => {
  209. const scriptContent = node.children[0]?.value || ''
  210. return `<script>${scriptContent}</script>`
  211. })
  212. ScriptBlock.displayName = 'ScriptBlock'
  213. const Paragraph = (paragraph: any) => {
  214. const { node }: any = paragraph
  215. const children_node = node.children
  216. if (children_node && children_node[0] && 'tagName' in children_node[0] && children_node[0].tagName === 'img') {
  217. return (
  218. <div className="markdown-img-wrapper">
  219. <ImageGallery srcs={[children_node[0].properties.src]} />
  220. {
  221. Array.isArray(paragraph.children) && paragraph.children.length > 1 && (
  222. <div className="mt-2">{paragraph.children.slice(1)}</div>
  223. )
  224. }
  225. </div>
  226. )
  227. }
  228. return <p>{paragraph.children}</p>
  229. }
  230. const Img = ({ src }: any) => {
  231. return <div className="markdown-img-wrapper"><ImageGallery srcs={[src]} /></div>
  232. }
  233. const Link = ({ node, children, ...props }: any) => {
  234. if (node.properties?.href && node.properties.href?.toString().startsWith('abbr')) {
  235. // eslint-disable-next-line react-hooks/rules-of-hooks
  236. const { onSend } = useChatContext()
  237. const hidden_text = decodeURIComponent(node.properties.href.toString().split('abbr:')[1])
  238. 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>
  239. }
  240. else {
  241. return <a {...props} target="_blank" className="cursor-pointer underline !decoration-primary-700 decoration-dashed">{children || 'Download'}</a>
  242. }
  243. }
  244. export function Markdown(props: { content: string; className?: string; customDisallowedElements?: string[] }) {
  245. const latexContent = flow([
  246. preprocessThinkTag,
  247. preprocessLaTeX,
  248. ])(props.content)
  249. return (
  250. <div className={cn('markdown-body', '!text-text-primary', props.className)}>
  251. <ReactMarkdown
  252. remarkPlugins={[
  253. RemarkGfm,
  254. [RemarkMath, { singleDollarTextMath: false }],
  255. RemarkBreaks,
  256. ]}
  257. rehypePlugins={[
  258. RehypeKatex,
  259. RehypeRaw as any,
  260. // The Rehype plug-in is used to remove the ref attribute of an element
  261. () => {
  262. return (tree) => {
  263. const iterate = (node: any) => {
  264. if (node.type === 'element' && node.properties?.ref)
  265. delete node.properties.ref
  266. if (node.type === 'element' && !/^[a-z][a-z0-9]*$/i.test(node.tagName)) {
  267. node.type = 'text'
  268. node.value = `<${node.tagName}`
  269. }
  270. if (node.children)
  271. node.children.forEach(iterate)
  272. }
  273. tree.children.forEach(iterate)
  274. }
  275. },
  276. ]}
  277. disallowedElements={['iframe', 'head', 'html', 'meta', 'link', 'style', 'body', ...(props.customDisallowedElements || [])]}
  278. components={{
  279. code: CodeBlock,
  280. img: Img,
  281. video: VideoBlock,
  282. audio: AudioBlock,
  283. a: Link,
  284. p: Paragraph,
  285. button: MarkdownButton,
  286. form: MarkdownForm,
  287. script: ScriptBlock as any,
  288. details: ThinkBlock,
  289. }}
  290. >
  291. {/* Markdown detect has problem. */}
  292. {latexContent}
  293. </ReactMarkdown>
  294. </div>
  295. )
  296. }
  297. // **Add an ECharts runtime error handler
  298. // Avoid error #7832 (Crash when ECharts accesses undefined objects)
  299. // This can happen when a component attempts to access an undefined object that references an unregistered map, causing the program to crash.
  300. export default class ErrorBoundary extends Component {
  301. constructor(props: any) {
  302. super(props)
  303. this.state = { hasError: false }
  304. }
  305. componentDidCatch(error: any, errorInfo: any) {
  306. this.setState({ hasError: true })
  307. console.error(error, errorInfo)
  308. }
  309. render() {
  310. // eslint-disable-next-line ts/ban-ts-comment
  311. // @ts-expect-error
  312. if (this.state.hasError)
  313. 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>
  314. // eslint-disable-next-line ts/ban-ts-comment
  315. // @ts-expect-error
  316. return this.props.children
  317. }
  318. }