Open History to see the last commit
1import { lazy, memo, Suspense } from "react";
2
3import { RenderErrorBoundary } from "../RenderErrorBoundary";
4
5const MathTypeset = lazy(() => import("./MathTypeset"));
6
7/**
8 * One formula from `remark-math`. Shows its TeX until KaTeX loads, and
9 * keeps showing it when KaTeX cannot parse it, such as mid-stream. Copying
10 * any part of a formula copies its whole TeX source.
11 */
12export const MarkdownMath = memo(function MarkdownMath(props: {
13 tex: string;
14 display: boolean;
15 focusable?: boolean;
16}) {
17 const tex = props.tex.trim();
18 const source = props.display ? `\\[\n${tex}\n\\]` : `\\(${tex}\\)`;
19 const fallback = <span className="whitespace-pre-wrap">{source}</span>;
20 return (
21 <span
22 className={
23 props.display
24 ? "chat-markdown-math-display my-[0.65rem] block in-[.chat-markdown-table-container]:max-w-none in-[.chat-markdown-table-container]:overflow-visible"
25 : "inline-block max-w-full align-middle whitespace-nowrap wrap-normal break-normal in-[.chat-markdown-table-container]:max-w-none in-[.chat-markdown-table-container]:overflow-visible"
26 }
27 data-markdown-math={props.display ? "display" : "inline"}
28 data-markdown-copy={props.display ? `${source}\n\n` : source}
29 role={props.focusable === false ? undefined : "group"}
30 aria-label={
31 props.focusable === false
32 ? undefined
33 : props.display
34 ? "Display equation"
35 : "Inline equation"
36 }
37 >
38 <span
39 className="block max-w-full overflow-x-auto overflow-y-hidden in-[.chat-markdown-table-container]:max-w-none in-[.chat-markdown-table-container]:overflow-visible px-0.5 py-0.75 focus-visible:outline-1 focus-visible:outline-solid focus-visible:-outline-offset-1 focus-visible:outline-border [&::-webkit-scrollbar]:h-2 [&::-webkit-scrollbar-thumb]:rounded [&::-webkit-scrollbar-thumb]:border [&::-webkit-scrollbar-thumb]:border-transparent [&::-webkit-scrollbar-thumb]:bg-clip-content"
40 tabIndex={props.focusable === false ? -1 : 0}
41 aria-label={
42 props.focusable === false
43 ? undefined
44 : props.display
45 ? "Scroll display equation"
46 : "Scroll inline equation"
47 }
48 >
49 <span className={props.display ? "inline-block w-max min-w-full" : "inline-block w-max"}>
50 <RenderErrorBoundary resetKeys={[tex]} fallback={fallback}>
51 <Suspense fallback={fallback}>
52 <MathTypeset tex={tex} display={props.display} fallback={fallback} />
53 </Suspense>
54 </RenderErrorBoundary>
55 </span>
56 </span>
57 </span>
58 );
59});