Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

markdown.tsx 12KB

2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
Introduce Plugins (#13836) Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: xhe <xw897002528@gmail.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: takatost <takatost@gmail.com> Co-authored-by: kurokobo <kuro664@gmail.com> Co-authored-by: Novice Lee <novicelee@NoviPro.local> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: AkaraChen <akarachen@outlook.com> Co-authored-by: Yi <yxiaoisme@gmail.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: JzoNg <jzongcode@gmail.com> Co-authored-by: twwu <twwu@dify.ai> Co-authored-by: Hiroshi Fujita <fujita-h@users.noreply.github.com> Co-authored-by: AkaraChen <85140972+AkaraChen@users.noreply.github.com> Co-authored-by: NFish <douxc512@gmail.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Novice <857526207@qq.com> Co-authored-by: Hiroki Nagai <82458324+nagaihiroki-git@users.noreply.github.com> Co-authored-by: Gen Sato <52241300+halogen22@users.noreply.github.com> Co-authored-by: eux <euxuuu@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: lotsik <lotsik@mail.ru> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: gakkiyomi <gakkiyomi@aliyun.com> Co-authored-by: CN-P5 <heibai2006@gmail.com> Co-authored-by: CN-P5 <heibai2006@qq.com> Co-authored-by: Chuehnone <1897025+chuehnone@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Kevin9703 <51311316+Kevin9703@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Boris Feld <lothiraldan@gmail.com> Co-authored-by: mbo <himabo@gmail.com> Co-authored-by: mabo <mabo@aeyes.ai> Co-authored-by: Warren Chen <warren.chen830@gmail.com> Co-authored-by: JzoNgKVO <27049666+JzoNgKVO@users.noreply.github.com> Co-authored-by: jiandanfeng <chenjh3@wangsu.com> Co-authored-by: zhu-an <70234959+xhdd123321@users.noreply.github.com> Co-authored-by: zhaoqingyu.1075 <zhaoqingyu.1075@bytedance.com> Co-authored-by: 海狸大師 <86974027+yenslife@users.noreply.github.com> Co-authored-by: Xu Song <xusong.vip@gmail.com> Co-authored-by: rayshaw001 <396301947@163.com> Co-authored-by: Ding Jiatong <dingjiatong@gmail.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: JasonVV <jasonwangiii@outlook.com> Co-authored-by: le0zh <newlight@qq.com> Co-authored-by: zhuxinliang <zhuxinliang@didiglobal.com> Co-authored-by: k-zaku <zaku99@outlook.jp> Co-authored-by: luckylhb90 <luckylhb90@gmail.com> Co-authored-by: hobo.l <hobo.l@binance.com> Co-authored-by: jiangbo721 <365065261@qq.com> Co-authored-by: 刘江波 <jiangbo721@163.com> Co-authored-by: Shun Miyazawa <34241526+miya@users.noreply.github.com> Co-authored-by: EricPan <30651140+Egfly@users.noreply.github.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: sino <sino2322@gmail.com> Co-authored-by: Jhvcc <37662342+Jhvcc@users.noreply.github.com> Co-authored-by: lowell <lowell.hu@zkteco.in> Co-authored-by: Boris Polonsky <BorisPolonsky@users.noreply.github.com> Co-authored-by: Ademílson Tonato <ademilsonft@outlook.com> Co-authored-by: Ademílson Tonato <ademilson.tonato@refurbed.com> Co-authored-by: IWAI, Masaharu <iwaim.sub@gmail.com> Co-authored-by: Yueh-Po Peng (Yabi) <94939112+y10ab1@users.noreply.github.com> Co-authored-by: Jason <ggbbddjm@gmail.com> Co-authored-by: Xin Zhang <sjhpzx@gmail.com> Co-authored-by: yjc980121 <3898524+yjc980121@users.noreply.github.com> Co-authored-by: heyszt <36215648+hieheihei@users.noreply.github.com> Co-authored-by: Abdullah AlOsaimi <osaimiacc@gmail.com> Co-authored-by: Abdullah AlOsaimi <189027247+osaimi@users.noreply.github.com> Co-authored-by: Yingchun Lai <laiyingchun@apache.org> Co-authored-by: Hash Brown <hi@xzd.me> Co-authored-by: zuodongxu <192560071+zuodongxu@users.noreply.github.com> Co-authored-by: Masashi Tomooka <tmokmss@users.noreply.github.com> Co-authored-by: aplio <ryo.091219@gmail.com> Co-authored-by: Obada Khalili <54270856+obadakhalili@users.noreply.github.com> Co-authored-by: Nam Vu <zuzoovn@gmail.com> Co-authored-by: Kei YAMAZAKI <1715090+kei-yamazaki@users.noreply.github.com> Co-authored-by: TechnoHouse <13776377+deephbz@users.noreply.github.com> Co-authored-by: Riddhimaan-Senapati <114703025+Riddhimaan-Senapati@users.noreply.github.com> Co-authored-by: MaFee921 <31881301+2284730142@users.noreply.github.com> Co-authored-by: te-chan <t-nakanome@sakura-is.co.jp> Co-authored-by: HQidea <HQidea@users.noreply.github.com> Co-authored-by: Joshbly <36315710+Joshbly@users.noreply.github.com> Co-authored-by: xhe <xw897002528@gmail.com> Co-authored-by: weiwenyan-dev <154779315+weiwenyan-dev@users.noreply.github.com> Co-authored-by: ex_wenyan.wei <ex_wenyan.wei@tcl.com> Co-authored-by: engchina <12236799+engchina@users.noreply.github.com> Co-authored-by: engchina <atjapan2015@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: 呆萌闷油瓶 <253605712@qq.com> Co-authored-by: Kemal <kemalmeler@outlook.com> Co-authored-by: Lazy_Frog <4590648+lazyFrogLOL@users.noreply.github.com> Co-authored-by: Yi Xiao <54782454+YIXIAO0@users.noreply.github.com> Co-authored-by: Steven sun <98230804+Tuyohai@users.noreply.github.com> Co-authored-by: steven <sunzwj@digitalchina.com> Co-authored-by: Kalo Chin <91766386+fdb02983rhy@users.noreply.github.com> Co-authored-by: Katy Tao <34019945+KatyTao@users.noreply.github.com> Co-authored-by: depy <42985524+h4ckdepy@users.noreply.github.com> Co-authored-by: 胡春东 <gycm520@gmail.com> Co-authored-by: Junjie.M <118170653@qq.com> Co-authored-by: MuYu <mr.muzea@gmail.com> Co-authored-by: Naoki Takashima <39912547+takatea@users.noreply.github.com> Co-authored-by: Summer-Gu <37869445+gubinjie@users.noreply.github.com> Co-authored-by: Fei He <droxer.he@gmail.com> Co-authored-by: ybalbert001 <120714773+ybalbert001@users.noreply.github.com> Co-authored-by: Yuanbo Li <ybalbert@amazon.com> Co-authored-by: douxc <7553076+douxc@users.noreply.github.com> Co-authored-by: liuzhenghua <1090179900@qq.com> Co-authored-by: Wu Jiayang <62842862+Wu-Jiayang@users.noreply.github.com> Co-authored-by: Your Name <you@example.com> Co-authored-by: kimjion <45935338+kimjion@users.noreply.github.com> Co-authored-by: AugNSo <song.tiankai@icloud.com> Co-authored-by: llinvokerl <38915183+llinvokerl@users.noreply.github.com> Co-authored-by: liusurong.lsr <liusurong.lsr@alibaba-inc.com> Co-authored-by: Vasu Negi <vasu-negi@users.noreply.github.com> Co-authored-by: Hundredwz <1808096180@qq.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
8 månader sedan
Introduce Plugins (#13836) Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: xhe <xw897002528@gmail.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: takatost <takatost@gmail.com> Co-authored-by: kurokobo <kuro664@gmail.com> Co-authored-by: Novice Lee <novicelee@NoviPro.local> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: AkaraChen <akarachen@outlook.com> Co-authored-by: Yi <yxiaoisme@gmail.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: JzoNg <jzongcode@gmail.com> Co-authored-by: twwu <twwu@dify.ai> Co-authored-by: Hiroshi Fujita <fujita-h@users.noreply.github.com> Co-authored-by: AkaraChen <85140972+AkaraChen@users.noreply.github.com> Co-authored-by: NFish <douxc512@gmail.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Novice <857526207@qq.com> Co-authored-by: Hiroki Nagai <82458324+nagaihiroki-git@users.noreply.github.com> Co-authored-by: Gen Sato <52241300+halogen22@users.noreply.github.com> Co-authored-by: eux <euxuuu@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: lotsik <lotsik@mail.ru> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: gakkiyomi <gakkiyomi@aliyun.com> Co-authored-by: CN-P5 <heibai2006@gmail.com> Co-authored-by: CN-P5 <heibai2006@qq.com> Co-authored-by: Chuehnone <1897025+chuehnone@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Kevin9703 <51311316+Kevin9703@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Boris Feld <lothiraldan@gmail.com> Co-authored-by: mbo <himabo@gmail.com> Co-authored-by: mabo <mabo@aeyes.ai> Co-authored-by: Warren Chen <warren.chen830@gmail.com> Co-authored-by: JzoNgKVO <27049666+JzoNgKVO@users.noreply.github.com> Co-authored-by: jiandanfeng <chenjh3@wangsu.com> Co-authored-by: zhu-an <70234959+xhdd123321@users.noreply.github.com> Co-authored-by: zhaoqingyu.1075 <zhaoqingyu.1075@bytedance.com> Co-authored-by: 海狸大師 <86974027+yenslife@users.noreply.github.com> Co-authored-by: Xu Song <xusong.vip@gmail.com> Co-authored-by: rayshaw001 <396301947@163.com> Co-authored-by: Ding Jiatong <dingjiatong@gmail.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: JasonVV <jasonwangiii@outlook.com> Co-authored-by: le0zh <newlight@qq.com> Co-authored-by: zhuxinliang <zhuxinliang@didiglobal.com> Co-authored-by: k-zaku <zaku99@outlook.jp> Co-authored-by: luckylhb90 <luckylhb90@gmail.com> Co-authored-by: hobo.l <hobo.l@binance.com> Co-authored-by: jiangbo721 <365065261@qq.com> Co-authored-by: 刘江波 <jiangbo721@163.com> Co-authored-by: Shun Miyazawa <34241526+miya@users.noreply.github.com> Co-authored-by: EricPan <30651140+Egfly@users.noreply.github.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: sino <sino2322@gmail.com> Co-authored-by: Jhvcc <37662342+Jhvcc@users.noreply.github.com> Co-authored-by: lowell <lowell.hu@zkteco.in> Co-authored-by: Boris Polonsky <BorisPolonsky@users.noreply.github.com> Co-authored-by: Ademílson Tonato <ademilsonft@outlook.com> Co-authored-by: Ademílson Tonato <ademilson.tonato@refurbed.com> Co-authored-by: IWAI, Masaharu <iwaim.sub@gmail.com> Co-authored-by: Yueh-Po Peng (Yabi) <94939112+y10ab1@users.noreply.github.com> Co-authored-by: Jason <ggbbddjm@gmail.com> Co-authored-by: Xin Zhang <sjhpzx@gmail.com> Co-authored-by: yjc980121 <3898524+yjc980121@users.noreply.github.com> Co-authored-by: heyszt <36215648+hieheihei@users.noreply.github.com> Co-authored-by: Abdullah AlOsaimi <osaimiacc@gmail.com> Co-authored-by: Abdullah AlOsaimi <189027247+osaimi@users.noreply.github.com> Co-authored-by: Yingchun Lai <laiyingchun@apache.org> Co-authored-by: Hash Brown <hi@xzd.me> Co-authored-by: zuodongxu <192560071+zuodongxu@users.noreply.github.com> Co-authored-by: Masashi Tomooka <tmokmss@users.noreply.github.com> Co-authored-by: aplio <ryo.091219@gmail.com> Co-authored-by: Obada Khalili <54270856+obadakhalili@users.noreply.github.com> Co-authored-by: Nam Vu <zuzoovn@gmail.com> Co-authored-by: Kei YAMAZAKI <1715090+kei-yamazaki@users.noreply.github.com> Co-authored-by: TechnoHouse <13776377+deephbz@users.noreply.github.com> Co-authored-by: Riddhimaan-Senapati <114703025+Riddhimaan-Senapati@users.noreply.github.com> Co-authored-by: MaFee921 <31881301+2284730142@users.noreply.github.com> Co-authored-by: te-chan <t-nakanome@sakura-is.co.jp> Co-authored-by: HQidea <HQidea@users.noreply.github.com> Co-authored-by: Joshbly <36315710+Joshbly@users.noreply.github.com> Co-authored-by: xhe <xw897002528@gmail.com> Co-authored-by: weiwenyan-dev <154779315+weiwenyan-dev@users.noreply.github.com> Co-authored-by: ex_wenyan.wei <ex_wenyan.wei@tcl.com> Co-authored-by: engchina <12236799+engchina@users.noreply.github.com> Co-authored-by: engchina <atjapan2015@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: 呆萌闷油瓶 <253605712@qq.com> Co-authored-by: Kemal <kemalmeler@outlook.com> Co-authored-by: Lazy_Frog <4590648+lazyFrogLOL@users.noreply.github.com> Co-authored-by: Yi Xiao <54782454+YIXIAO0@users.noreply.github.com> Co-authored-by: Steven sun <98230804+Tuyohai@users.noreply.github.com> Co-authored-by: steven <sunzwj@digitalchina.com> Co-authored-by: Kalo Chin <91766386+fdb02983rhy@users.noreply.github.com> Co-authored-by: Katy Tao <34019945+KatyTao@users.noreply.github.com> Co-authored-by: depy <42985524+h4ckdepy@users.noreply.github.com> Co-authored-by: 胡春东 <gycm520@gmail.com> Co-authored-by: Junjie.M <118170653@qq.com> Co-authored-by: MuYu <mr.muzea@gmail.com> Co-authored-by: Naoki Takashima <39912547+takatea@users.noreply.github.com> Co-authored-by: Summer-Gu <37869445+gubinjie@users.noreply.github.com> Co-authored-by: Fei He <droxer.he@gmail.com> Co-authored-by: ybalbert001 <120714773+ybalbert001@users.noreply.github.com> Co-authored-by: Yuanbo Li <ybalbert@amazon.com> Co-authored-by: douxc <7553076+douxc@users.noreply.github.com> Co-authored-by: liuzhenghua <1090179900@qq.com> Co-authored-by: Wu Jiayang <62842862+Wu-Jiayang@users.noreply.github.com> Co-authored-by: Your Name <you@example.com> Co-authored-by: kimjion <45935338+kimjion@users.noreply.github.com> Co-authored-by: AugNSo <song.tiankai@icloud.com> Co-authored-by: llinvokerl <38915183+llinvokerl@users.noreply.github.com> Co-authored-by: liusurong.lsr <liusurong.lsr@alibaba-inc.com> Co-authored-by: Vasu Negi <vasu-negi@users.noreply.github.com> Co-authored-by: Hundredwz <1808096180@qq.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
8 månader sedan
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. }