markdown.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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; className?: string }) {
  75. const [isSVG, setIsSVG] = useState(false)
  76. return (
  77. <div className={cn(props.className, 'markdown-body')}>
  78. <ReactMarkdown
  79. remarkPlugins={[[RemarkMath, { singleDollarTextMath: false }], RemarkGfm, RemarkBreaks]}
  80. rehypePlugins={[
  81. RehypeKatex,
  82. ]}
  83. components={{
  84. code({ inline, className, children, ...props }) {
  85. const match = /language-(\w+)/.exec(className || '')
  86. const language = match?.[1]
  87. const languageShowName = getCorrectCapitalizationLanguageName(language || '')
  88. return (!inline && match)
  89. ? (
  90. <div>
  91. <div
  92. className='flex justify-between h-8 items-center p-1 pl-3 border-b'
  93. style={{
  94. borderColor: 'rgba(0, 0, 0, 0.05)',
  95. }}
  96. >
  97. <div className='text-[13px] text-gray-500 font-normal'>{languageShowName}</div>
  98. <div style={{ display: 'flex' }}>
  99. {language === 'mermaid'
  100. && <SVGBtn
  101. isSVG={isSVG}
  102. setIsSVG={setIsSVG}
  103. />
  104. }
  105. <CopyBtn
  106. className={cn(s.copyBtn, 'mr-1')}
  107. value={String(children).replace(/\n$/, '')}
  108. isPlain
  109. />
  110. </div>
  111. </div>
  112. {(language === 'mermaid' && isSVG)
  113. ? (<Flowchart PrimitiveCode={String(children).replace(/\n$/, '')} />)
  114. : (<SyntaxHighlighter
  115. {...props}
  116. style={atelierHeathLight}
  117. customStyle={{
  118. paddingLeft: 12,
  119. backgroundColor: '#fff',
  120. }}
  121. language={match[1]}
  122. showLineNumbers
  123. PreTag="div"
  124. >
  125. {String(children).replace(/\n$/, '')}
  126. </SyntaxHighlighter>)}
  127. </div>
  128. )
  129. : (
  130. <code {...props} className={className}>
  131. {children}
  132. </code>
  133. )
  134. },
  135. img({ src, alt, ...props }) {
  136. return (
  137. // eslint-disable-next-line @next/next/no-img-element
  138. <img
  139. src={src}
  140. alt={alt}
  141. width={250}
  142. height={250}
  143. 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"
  144. {...props}
  145. />
  146. )
  147. },
  148. p: (paragraph) => {
  149. const { node }: any = paragraph
  150. if (node.children[0].tagName === 'img') {
  151. const image = node.children[0]
  152. return (
  153. <>
  154. {/* eslint-disable-next-line @next/next/no-img-element */}
  155. <img
  156. src={image.properties.src}
  157. width={250}
  158. height={250}
  159. 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"
  160. alt={image.properties.alt}
  161. />
  162. <p>{paragraph.children.slice(1)}</p>
  163. </>
  164. )
  165. }
  166. return <p>{paragraph.children}</p>
  167. },
  168. }}
  169. linkTarget='_blank'
  170. >
  171. {/* Markdown detect has problem. */}
  172. {props.content}
  173. </ReactMarkdown>
  174. </div>
  175. )
  176. }