markdown.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import ReactMarkdown from "react-markdown";
  2. import { Image } from "antd";
  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 RehypeHighlight from "rehype-highlight";
  8. import { useRef, useState, RefObject, useEffect, useMemo } from "react";
  9. import { copyToClipboard, useWindowSize } from "../utils";
  10. import mermaid from "mermaid";
  11. import LoadingIcon from "../icons/three-dots.svg";
  12. import React from "react";
  13. import { useDebouncedCallback } from "use-debounce";
  14. import { showImageModal, FullScreen } from "./ui-lib";
  15. import { ArtifactsShareButton, HTMLPreview } from "./artifacts";
  16. import { Plugin } from "../constant";
  17. import { useChatStore } from "../store";
  18. import "katex/dist/katex.min.css";
  19. export function Mermaid(props: { code: string }) {
  20. const ref = useRef<HTMLDivElement>(null);
  21. const [hasError, setHasError] = useState(false);
  22. useEffect(() => {
  23. if (props.code && ref.current) {
  24. mermaid
  25. .run({
  26. nodes: [ref.current],
  27. suppressErrors: true,
  28. })
  29. .catch((e) => {
  30. setHasError(true);
  31. console.error("[Mermaid] ", e.message);
  32. });
  33. }
  34. // eslint-disable-next-line react-hooks/exhaustive-deps
  35. }, [props.code]);
  36. function viewSvgInNewWindow() {
  37. const svg = ref.current?.querySelector("svg");
  38. if (!svg) return;
  39. const text = new XMLSerializer().serializeToString(svg);
  40. const blob = new Blob([text], { type: "image/svg+xml" });
  41. showImageModal(URL.createObjectURL(blob));
  42. }
  43. if (hasError) {
  44. return null;
  45. }
  46. return (
  47. <div
  48. className="no-dark mermaid"
  49. style={{
  50. cursor: "pointer",
  51. overflow: "auto",
  52. }}
  53. ref={ref}
  54. onClick={() => viewSvgInNewWindow()}
  55. >
  56. {props.code}
  57. </div>
  58. );
  59. }
  60. export function PreCode(props: { children: any }) {
  61. const ref = useRef<HTMLPreElement>(null);
  62. const refText = ref.current?.innerText;
  63. const [mermaidCode, setMermaidCode] = useState("");
  64. const [htmlCode, setHtmlCode] = useState("");
  65. const { height } = useWindowSize();
  66. const chatStore = useChatStore();
  67. const session = chatStore.currentSession();
  68. const plugins = session.mask?.plugin;
  69. const renderArtifacts = useDebouncedCallback(() => {
  70. if (!ref.current) return;
  71. const mermaidDom = ref.current.querySelector("code.language-mermaid");
  72. if (mermaidDom) {
  73. setMermaidCode((mermaidDom as HTMLElement).innerText);
  74. }
  75. const htmlDom = ref.current.querySelector("code.language-html");
  76. if (htmlDom) {
  77. setHtmlCode((htmlDom as HTMLElement).innerText);
  78. } else if (refText?.startsWith("<!DOCTYPE")) {
  79. setHtmlCode(refText);
  80. }
  81. }, 600);
  82. useEffect(() => {
  83. setTimeout(renderArtifacts, 1);
  84. // eslint-disable-next-line react-hooks/exhaustive-deps
  85. }, [refText]);
  86. const enableArtifacts = useMemo(
  87. () => plugins?.includes(Plugin.Artifacts),
  88. [plugins],
  89. );
  90. //Wrap the paragraph for plain-text
  91. useEffect(() => {
  92. if (ref.current) {
  93. const codeElements = ref.current.querySelectorAll(
  94. "code",
  95. ) as NodeListOf<HTMLElement>;
  96. const wrapLanguages = [
  97. "",
  98. "md",
  99. "markdown",
  100. "text",
  101. "txt",
  102. "plaintext",
  103. "tex",
  104. "latex",
  105. ];
  106. codeElements.forEach((codeElement) => {
  107. let languageClass = codeElement.className.match(/language-(\w+)/);
  108. let name = languageClass ? languageClass[1] : "";
  109. if (wrapLanguages.includes(name)) {
  110. codeElement.style.whiteSpace = "pre-wrap";
  111. }
  112. });
  113. }
  114. }, []);
  115. return (
  116. <>
  117. <pre ref={ref}>
  118. <span
  119. className="copy-code-button"
  120. onClick={() => {
  121. if (ref.current) {
  122. const code = ref.current.innerText;
  123. copyToClipboard(code);
  124. }
  125. }}
  126. ></span>
  127. {props.children}
  128. </pre>
  129. {mermaidCode.length > 0 && (
  130. <Mermaid code={mermaidCode} key={mermaidCode} />
  131. )}
  132. {htmlCode.length > 0 && enableArtifacts && (
  133. <FullScreen className="no-dark html" right={70}>
  134. <ArtifactsShareButton
  135. style={{ position: "absolute", right: 20, top: 10 }}
  136. getCode={() => htmlCode}
  137. />
  138. <HTMLPreview
  139. code={htmlCode}
  140. autoHeight={!document.fullscreenElement}
  141. height={!document.fullscreenElement ? 600 : height}
  142. />
  143. </FullScreen>
  144. )}
  145. </>
  146. );
  147. }
  148. function escapeDollarNumber(text: string) {
  149. let escapedText = "";
  150. for (let i = 0; i < text.length; i += 1) {
  151. let char = text[i];
  152. const nextChar = text[i + 1] || " ";
  153. if (char === "$" && nextChar >= "0" && nextChar <= "9") {
  154. char = "\\$";
  155. }
  156. escapedText += char;
  157. }
  158. return escapedText;
  159. }
  160. function escapeBrackets(text: string) {
  161. const pattern =
  162. /(```[\s\S]*?```|`.*?`)|\\\[([\s\S]*?[^\\])\\\]|\\\((.*?)\\\)/g;
  163. return text.replace(
  164. pattern,
  165. (match, codeBlock, squareBracket, roundBracket) => {
  166. if (codeBlock) {
  167. return codeBlock;
  168. } else if (squareBracket) {
  169. return `$$${squareBracket}$$`;
  170. } else if (roundBracket) {
  171. return `$${roundBracket}$`;
  172. }
  173. return match;
  174. },
  175. );
  176. }
  177. function _MarkDownContent(props: { content: string }) {
  178. const escapedContent = useMemo(() => {
  179. return escapeBrackets(escapeDollarNumber(props.content));
  180. }, [props.content]);
  181. return (
  182. <ReactMarkdown
  183. remarkPlugins={[RemarkMath, RemarkGfm, RemarkBreaks]}
  184. rehypePlugins={[
  185. RehypeKatex,
  186. [
  187. RehypeHighlight,
  188. {
  189. detect: false,
  190. ignoreMissing: true,
  191. },
  192. ],
  193. ]}
  194. // 控制不同标签的显示样式
  195. components={{
  196. pre: PreCode,
  197. p: (pProps) => <p {...pProps} dir="auto" />,
  198. a: (aProps) => {
  199. const href = aProps.href || "";
  200. const isInternal = /^\/#/i.test(href);
  201. const target = isInternal ? "_self" : aProps.target ?? "_blank";
  202. return <a {...aProps} target={target} />;
  203. },
  204. img: ({ src, alt }) => (
  205. <Image
  206. src={src}
  207. alt={alt}
  208. preview={{
  209. mask: null
  210. }}
  211. />
  212. ),
  213. }}
  214. >
  215. {escapedContent}
  216. </ReactMarkdown >
  217. );
  218. }
  219. export const MarkdownContent = React.memo(_MarkDownContent);
  220. export function Markdown(
  221. props: {
  222. content: string;
  223. loading?: boolean;
  224. fontSize?: number;
  225. fontFamily?: string;
  226. parentRef?: RefObject<HTMLDivElement>;
  227. defaultShow?: boolean;
  228. } & React.DOMAttributes<HTMLDivElement>,
  229. ) {
  230. const mdRef = useRef<HTMLDivElement>(null);
  231. return (
  232. <div
  233. className="markdown-body"
  234. style={{
  235. fontSize: `${props.fontSize ?? 14}px`,
  236. fontFamily: props.fontFamily || "inherit",
  237. }}
  238. ref={mdRef}
  239. onContextMenu={props.onContextMenu}
  240. onDoubleClickCapture={props.onDoubleClickCapture}
  241. dir="auto"
  242. >
  243. {props.loading ? (
  244. <LoadingIcon />
  245. ) : (
  246. <MarkdownContent content={props.content} />
  247. )}
  248. </div>
  249. );
  250. }