Path: blob/master/src/packages/frontend/components/math/ssr.tsx
1503 views
/*12Simple synchronous math formula component, which works fine on the frontend or3the backend (nodejs). We also use a custom component in the frontend app via4FileContext when we need something more sophisticated, e.g., fallback to async5mathjax.67*/89import { math_escape, math_unescape } from "@cocalc/util/markdown-utils";10import { remove_math, replace_math } from "@cocalc/util/mathjax-utils";11import { latexMathToHtml } from "@cocalc/frontend/misc/math-to-html";12import { replace_all } from "@cocalc/util/misc";13import { replaceMathBracketDelims } from "./util";1415interface Props {16data: string;17inMarkdown?: boolean; // ignored, since18}1920export default function DefaultMath({ data }: Props) {21data = replaceMathBracketDelims(data);22const [text, math] = remove_math(math_escape(data));23if (math.length == 0) {24// no math25return <>{data}</>;26}27for (let i = 0; i < math.length; i++) {28math[i] = latexMathToHtml(math[i]);29}30// Substitute processed math back in.31const __html = replace_all(32math_unescape(replace_math(text, math)),33"\\$",34"$"35);36return <span dangerouslySetInnerHTML={{ __html }}></span>;37}383940