markdown.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. // Available language https://github.com/react-syntax-highlighter/react-syntax-highlighter/blob/master/AVAILABLE_LANGUAGES_HLJS.MD
  16. const capitalizationLanguageNameMap: Record<string, string> = {
  17. sql: 'SQL',
  18. javascript: 'JavaScript',
  19. java: 'Java',
  20. typescript: 'TypeScript',
  21. vbscript: 'VBScript',
  22. css: 'CSS',
  23. html: 'HTML',
  24. xml: 'XML',
  25. php: 'PHP',
  26. python: 'Python',
  27. yaml: 'Yaml',
  28. mermaid: 'Mermaid',
  29. markdown: 'MarkDown',
  30. makefile: 'MakeFile',
  31. }
  32. const getCorrectCapitalizationLanguageName = (language: string) => {
  33. if (!language)
  34. return 'Plain'
  35. if (language in capitalizationLanguageNameMap)
  36. return capitalizationLanguageNameMap[language]
  37. return language.charAt(0).toUpperCase() + language.substring(1)
  38. }
  39. export function PreCode(props: { children: any }) {
  40. const ref = useRef<HTMLPreElement>(null)
  41. return (
  42. <pre ref={ref}>
  43. <span
  44. className="copy-code-button"
  45. onClick={() => {
  46. if (ref.current) {
  47. const code = ref.current.innerText
  48. // copyToClipboard(code);
  49. }
  50. }}
  51. ></span>
  52. {props.children}
  53. </pre>
  54. )
  55. }
  56. const useLazyLoad = (ref: RefObject<Element>): boolean => {
  57. const [isIntersecting, setIntersecting] = useState<boolean>(false)
  58. useEffect(() => {
  59. const observer = new IntersectionObserver(([entry]) => {
  60. if (entry.isIntersecting) {
  61. setIntersecting(true)
  62. observer.disconnect()
  63. }
  64. })
  65. if (ref.current)
  66. observer.observe(ref.current)
  67. return () => {
  68. observer.disconnect()
  69. }
  70. }, [ref])
  71. return isIntersecting
  72. }
  73. export function Markdown(props: { content: string; className?: string }) {
  74. const [isSVG, setIsSVG] = useState(false)
  75. return (
  76. <div className={cn(props.className, 'markdown-body')}>
  77. <ReactMarkdown
  78. remarkPlugins={[[RemarkMath, { singleDollarTextMath: false }], RemarkGfm, RemarkBreaks]}
  79. rehypePlugins={[
  80. RehypeKatex,
  81. ]}
  82. components={{
  83. code({ inline, className, children, ...props }) {
  84. const match = /language-(\w+)/.exec(className || '')
  85. const language = match?.[1]
  86. const languageShowName = getCorrectCapitalizationLanguageName(language || '')
  87. return (!inline && match)
  88. ? (
  89. <div>
  90. <div
  91. className='flex justify-between h-8 items-center p-1 pl-3 border-b'
  92. style={{
  93. borderColor: 'rgba(0, 0, 0, 0.05)',
  94. }}
  95. >
  96. <div className='text-[13px] text-gray-500 font-normal'>{languageShowName}</div>
  97. <div style={{ display: 'flex' }}>
  98. {language === 'mermaid'
  99. && <SVGBtn
  100. isSVG={isSVG}
  101. setIsSVG={setIsSVG}
  102. />
  103. }
  104. <CopyBtn
  105. className='mr-1'
  106. value={String(children).replace(/\n$/, '')}
  107. isPlain
  108. />
  109. </div>
  110. </div>
  111. {(language === 'mermaid' && isSVG)
  112. ? (<Flowchart PrimitiveCode={String(children).replace(/\n$/, '')} />)
  113. : (<SyntaxHighlighter
  114. {...props}
  115. style={atelierHeathLight}
  116. customStyle={{
  117. paddingLeft: 12,
  118. backgroundColor: '#fff',
  119. }}
  120. language={match[1]}
  121. showLineNumbers
  122. PreTag="div"
  123. >
  124. {String(children).replace(/\n$/, '')}
  125. </SyntaxHighlighter>)}
  126. </div>
  127. )
  128. : (
  129. <code {...props} className={className}>
  130. {children}
  131. </code>
  132. )
  133. },
  134. img({ src, alt, ...props }) {
  135. return (
  136. // eslint-disable-next-line @next/next/no-img-element
  137. <img
  138. src={src}
  139. alt={alt}
  140. width={250}
  141. height={250}
  142. className="max-w-full h-auto align-middle border-none rounded-lg shadow-md hover:shadow-lg transition-shadow duration-300 ease-in-out mt-2 mb-2"
  143. {...props}
  144. />
  145. )
  146. },
  147. p: (paragraph) => {
  148. const { node }: any = paragraph
  149. if (node.children[0].tagName === 'img') {
  150. const image = node.children[0]
  151. return (
  152. <>
  153. {/* eslint-disable-next-line @next/next/no-img-element */}
  154. <img
  155. src={image.properties.src}
  156. width={250}
  157. height={250}
  158. className="max-w-full h-auto align-middle border-none rounded-lg shadow-md hover:shadow-lg transition-shadow duration-300 ease-in-out mt-2 mb-2"
  159. alt={image.properties.alt}
  160. />
  161. <p>{paragraph.children.slice(1)}</p>
  162. </>
  163. )
  164. }
  165. return <p>{paragraph.children}</p>
  166. },
  167. }}
  168. linkTarget='_blank'
  169. >
  170. {/* Markdown detect has problem. */}
  171. {props.content}
  172. </ReactMarkdown>
  173. </div>
  174. )
  175. }