#3225·tui.editor

可选支持使用 $$ 进行渲染的内联数学表达式

作者: a1473838623创建于 2024年2月5日更新于 2026年6月8日
标签EnhancementNeed Discussion

import { getLocale } from '@umijs/max'; import { Button } from 'antd'; import { useRef } from 'react'; import { Editor } from '@toast-ui/react-editor'; //@ts-ignore import katex from 'katex'; import { MdNode } from '@toast-ui/editor';

const EditBlog = () => { const editorRef = useRef(null); const lang = getLocale().toString();

function renderMathOnLeft(str: string) { const html = katex.renderToString(str, { throwOnError: false, displayMode: false, output: 'mathml', globalGroup: true, fleqn: true, }); return html; }

// function renderMathOnCenter(str: string) { // const html = katex.renderToString(str, { // throwOnError: false, // displayMode: false, // output: 'mathml', // globalGroup: true, // // this config can render math on center // fleqn: false // }); // return html; // }

let latexPart: string = ""; let ifEntering: boolean = false; let ifEven = true; let lastUncloseNode: MdNode | null = null; function getInlineMath(node: MdNode) { let noNeedNewLine = false; let str = node?.literal || ""; let prevIdx = 0; let nextIdx = -1; let ifLastNode = (node?.next === null); let count = 0; if (node !== null && node === node?.parent?.firstChild) { let n: MdNode | null = node; while (n !== null) { for (let i = 0; i < (n?.literal?.length ?? 0); i++) { if (n?.literal?.[i] === "$") { count++; } } if (count % 2 === 1) { lastUncloseNode = n; } else { lastUncloseNode = null; } n = …