markdown.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import ReactMarkdown from 'react-markdown'
  2. import 'katex/dist/katex.min.css'
  3. import RemarkMath from 'remark-math'
  4. import RemarkBreaks from 'remark-breaks'
  5. import RehypeKatex from 'rehype-katex'
  6. import RemarkGfm from 'remark-gfm'
  7. import SyntaxHighlighter from 'react-syntax-highlighter'
  8. import { atelierHeathLight } from 'react-syntax-highlighter/dist/esm/styles/hljs'
  9. import type { RefObject } from 'react'
  10. import { useEffect, useRef, useState } from 'react'
  11. import cn from 'classnames'
  12. import CopyBtn from '@/app/components/app/chat/copy-btn'
  13. import SVGBtn from '@/app/components/app/chat/svg'
  14. import Flowchart from '@/app/components/app/chat/mermaid'
  15. import s from '@/app/components/app/chat/style.module.css'
  16. // Available language https://github.com/react-syntax-highlighter/react-syntax-highlighter/blob/master/AVAILABLE_LANGUAGES_HLJS.MD
  17. const capitalizationLanguageNameMap: Record<string, string> = {
  18. sql: 'SQL',
  19. javascript: 'JavaScript',
  20. java: 'Java',
  21. typescript: 'TypeScript',
  22. vbscript: 'VBScript',
  23. css: 'CSS',
  24. html: 'HTML',
  25. xml: 'XML',
  26. php: 'PHP',
  27. python: 'Python',
  28. yaml: 'Yaml',
  29. mermaid: 'Mermaid',
  30. markdown: 'MarkDown',
  31. makefile: 'MakeFile',
  32. }
  33. const getCorrectCapitalizationLanguageName = (language: string) => {
  34. if (!language)
  35. return 'Plain'
  36. if (language in capitalizationLanguageNameMap)
  37. return capitalizationLanguageNameMap[language]
  38. return language.charAt(0).toUpperCase() + language.substring(1)
  39. }
  40. export function PreCode(props: { children: any }) {
  41. const ref = useRef<HTMLPreElement>(null)
  42. return (
  43. <pre ref={ref}>
  44. <span
  45. className="copy-code-button"
  46. onClick={() => {
  47. if (ref.current) {
  48. const code = ref.current.innerText
  49. // copyToClipboard(code);
  50. }
  51. }}
  52. ></span>
  53. {props.children}
  54. </pre>
  55. )
  56. }
  57. const useLazyLoad = (ref: RefObject<Element>): boolean => {
  58. const [isIntersecting, setIntersecting] = useState<boolean>(false)
  59. useEffect(() => {
  60. const observer = new IntersectionObserver(([entry]) => {
  61. if (entry.isIntersecting) {
  62. setIntersecting(true)
  63. observer.disconnect()
  64. }
  65. })
  66. if (ref.current)
  67. observer.observe(ref.current)
  68. return () => {
  69. observer.disconnect()
  70. }
  71. }, [ref])
  72. return isIntersecting
  73. }
  74. export function Markdown(props: { content: string }) {
  75. const [isCopied, setIsCopied] = useState(false)
  76. const [isSVG, setIsSVG] = useState(false)
  77. return (
  78. <div className="markdown-body">
  79. <ReactMarkdown
  80. remarkPlugins={[RemarkMath, RemarkGfm, RemarkBreaks]}
  81. rehypePlugins={[
  82. RehypeKatex,
  83. ]}
  84. components={{
  85. code({ node, inline, className, children, ...props }) {
  86. const match = /language-(\w+)/.exec(className || '')
  87. const language = match?.[1]
  88. const languageShowName = getCorrectCapitalizationLanguageName(language || '')
  89. return (!inline && match)
  90. ? (
  91. <div>
  92. <div
  93. className='flex justify-between h-8 items-center p-1 pl-3 border-b'
  94. style={{
  95. borderColor: 'rgba(0, 0, 0, 0.05)',
  96. }}
  97. >
  98. <div className='text-[13px] text-gray-500 font-normal'>{languageShowName}</div>
  99. <div style={{ display: 'flex' }}>
  100. {language === 'mermaid'
  101. && <SVGBtn
  102. isSVG={isSVG}
  103. setIsSVG={setIsSVG}
  104. />
  105. }
  106. <CopyBtn
  107. className={cn(s.copyBtn, 'mr-1')}
  108. value={String(children).replace(/\n$/, '')}
  109. isPlain
  110. />
  111. </div>
  112. </div>
  113. { (language === 'mermaid' && isSVG)
  114. ? (<Flowchart PrimitiveCode={String(children).replace(/\n$/, '')} />)
  115. : (<SyntaxHighlighter
  116. {...props}
  117. style={atelierHeathLight}
  118. customStyle={{
  119. paddingLeft: 12,
  120. backgroundColor: '#fff',
  121. }}
  122. language={match[1]}
  123. showLineNumbers
  124. PreTag="div"
  125. >
  126. {String(children).replace(/\n$/, '')}
  127. </SyntaxHighlighter>)}
  128. </div>
  129. )
  130. : (
  131. <code {...props} className={className}>
  132. {children}
  133. </code>
  134. )
  135. },
  136. }}
  137. linkTarget={'_blank'}
  138. >
  139. {/* Markdown detect has problem. */}
  140. {props.content}
  141. </ReactMarkdown>
  142. </div>
  143. )
  144. }