Files
deer-flow/web/tests/markdown-katex.test.ts

40 lines
1.2 KiB
TypeScript
Raw Normal View History

import { describe, it } from "node:test";
import assert from "node:assert/strict";
import katex from "katex";
fix: resolve math formula display abnormal after editing report This fix addresses the issue where math formulas become corrupted or incorrectly displayed after editing the generated report in the editor. **Root Cause:** The issue occurred due to incompatibility between markdown processing in the display component and the Tiptap editor: 1. Display component used \[\] and \(\) LaTeX delimiters 2. Tiptap Mathematics extension expects $ and 70868 delimiters 3. tiptap-markdown didn't have built-in math node serialization 4. Math syntax was lost/corrupted during editor save operations **Solution Implemented:** 1. Created MathematicsWithMarkdown extension that adds markdown serialization support to Tiptap's Mathematics nodes 2. Added math delimiter normalization functions: - normalizeMathForEditor(): Converts LaTeX delimiters to $/70868 - normalizeMathForDisplay(): Standardizes all delimiters to 70868 3. Updated Markdown component to use new normalization 4. Updated ReportEditor to normalize content before loading **Changes:** - web/src/components/editor/math-serializer.ts (new) - web/src/components/editor/extensions.tsx - web/src/components/editor/index.tsx - web/src/components/deer-flow/markdown.tsx - web/src/core/utils/markdown.ts - web/tests/markdown-math-editor.test.ts (new) - web/tests/markdown-katex.test.ts **Testing:** - Added 15 comprehensive tests for math normalization round-trip - All tests passing (math editor + existing katex tests) - Verified TypeScript compilation and linting Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
2025-10-15 08:13:49 +08:00
import { katexOptions } from "../src/core/markdown/katex.ts";
function render(expression: string) {
return katex.renderToString(expression, {
...katexOptions,
displayMode: true,
});
}
describe("markdown physics katex support", () => {
it("renders vector calculus operators", () => {
assert.doesNotThrow(() => {
render("\\curl{\\vect{B}} = \\mu_0 \\vect{J} + \\mu_0 \\varepsilon_0 \\pdv{\\vect{E}}{t}");
});
});
it("renders quantum mechanics bra-ket notation", () => {
const html = render("\\braket{\\psi}{\\phi}");
assert.ok(html.includes("⟨") && html.includes("⟩"));
});
it("renders vector magnitude formula with subscripts and square root", () => {
const html = render("(F_1) (F_2), (F=\\sqrt{F_1^2+F_2^2})");
assert.ok(html.includes("F"));
assert.ok(html.includes("₁") || html.includes("sub")); // subscript check
assert.ok(html.includes("√") || html.includes("sqrt")); // square root check
});
it("renders chemical equations via mhchem", () => {
assert.doesNotThrow(() => {
render("\\ce{H2O ->[\\Delta] H+ + OH-}");
});
});
});