memos/web/src/labs/marked/parser/BlockLatex.tsx
Kada Liao 9a0ada6756
feat: support LaTeX with react-katex (#2209)
feat: support latex

Co-authored-by: liaoxingyi <liaoxingyi@douban.com>
2023-09-12 23:53:46 +08:00

35 lines
845 B
TypeScript

import TeX from "@matejmazur/react-katex";
import "katex/dist/katex.min.css";
import { matcher } from "../matcher";
const BLOCK_LATEX_REG = new RegExp(
"\\$\\$(\\s*[^\\$\\s][^\\$]*?)\\$\\$|\\\\\\[(.+?)\\\\\\]|\\\\begin{equation}([\\s\\S]+?)\\\\end{equation}"
);
const blockRenderer = (rawStr: string) => {
const matchResult = matcher(rawStr, BLOCK_LATEX_REG);
if (!matchResult) {
return <>{rawStr}</>;
}
let latexCode = "";
if (matchResult[1]) {
// $$
latexCode = matchResult[1];
} else if (matchResult[2]) {
// \[ and \]
latexCode = matchResult[2];
} else if (matchResult[3]) {
// \begin{equation} and \end{equation}
latexCode = matchResult[3];
}
return <TeX block={true}>{latexCode}</TeX>;
};
export default {
name: "blockLatex",
regexp: BLOCK_LATEX_REG,
renderer: blockRenderer,
};