CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/components/math/ssr.tsx
Views: 687
1
/*
2
3
Simple synchronous math formula component, which works fine on the frontend or
4
the backend (nodejs). We also use a custom component in the frontend app via
5
FileContext when we need something more sophisticated, e.g., fallback to async
6
mathjax.
7
8
*/
9
10
import { math_escape, math_unescape } from "@cocalc/util/markdown-utils";
11
import { remove_math, replace_math } from "@cocalc/util/mathjax-utils";
12
import { latexMathToHtml } from "@cocalc/frontend/misc/math-to-html";
13
import { replace_all } from "@cocalc/util/misc";
14
import { replaceMathBracketDelims } from "./util";
15
16
interface Props {
17
data: string;
18
inMarkdown?: boolean; // ignored, since
19
}
20
21
export default function DefaultMath({ data }: Props) {
22
data = replaceMathBracketDelims(data);
23
const [text, math] = remove_math(math_escape(data));
24
if (math.length == 0) {
25
// no math
26
return <>{data}</>;
27
}
28
for (let i = 0; i < math.length; i++) {
29
math[i] = latexMathToHtml(math[i]);
30
}
31
// Substitute processed math back in.
32
const __html = replace_all(
33
math_unescape(replace_math(text, math)),
34
"\\$",
35
"$"
36
);
37
return <span dangerouslySetInnerHTML={{ __html }}></span>;
38
}
39
40