Skip to content

Commit 5e7a608

Browse files
authored
Remove obsolete references to KaTeX (#2303)
## Summary: In 2023, we deprecated our usage of KaTeX in favor of MathJax 3. This PR cleans up references to KaTeX in Perseus. Issue: LEMS-1608 ## Test plan: - Deploy a ZND - Verify that you can see math in an exercise - ...and in the exercise editor - ...and in the article editor. - If you create a math element with invalid syntax, like `$#$`, you should see a "TeX Errors" box appear that lists the errors and their locations. Author: benchristel Reviewers: benchristel, handeyeco, SonicScrewdriver, jeremywiebe Required Reviewers: Approved By: handeyeco Checks: ✅ 8 checks were successful Pull Request URL: #2303
1 parent 0d5ab0b commit 5e7a608

File tree

13 files changed

+16
-63
lines changed

13 files changed

+16
-63
lines changed

.changeset/ten-adults-act.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@khanacademy/perseus": major
3+
"@khanacademy/perseus-editor": major
4+
---
5+
6+
Drop support for using KaTeX as a math renderer. You may encounter styling issues or TeX syntax errors if you try to implement `PerseusDependencies.TeX` using KaTeX.

packages/perseus-editor/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@khanacademy/perseus-core": "workspace:*",
4444
"@khanacademy/perseus-score": "workspace:*",
4545
"@khanacademy/pure-markdown": "workspace:*",
46+
"katex": "0.11.1",
4647
"mafs": "^0.19.0"
4748
},
4849
"devDependencies": {
@@ -68,7 +69,6 @@
6869
"aphrodite": "catalog:",
6970
"classnames": "catalog:",
7071
"jquery": "catalog:",
71-
"katex": "0.11.1",
7272
"perseus-build-settings": "workspace:*",
7373
"prop-types": "catalog:",
7474
"react": "catalog:",

packages/perseus-editor/src/widgets/label-image/answer-choices.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type AddAnswerProps = {
1818
};
1919

2020
type AnswerProps = {
21-
// The answer string, can be plain text or a KaTeX expression.
21+
// The answer string, can be plain text or a TeX expression.
2222
answer: string;
2323
// Callback for when an answer is changed.
2424
onChange: (answer: string) => void;

packages/perseus/src/__tests__/renderer.test.tsx

-20
Original file line numberDiff line numberDiff line change
@@ -736,26 +736,6 @@ describe("renderer", () => {
736736
expect(container).toMatchSnapshot();
737737
});
738738

739-
it("should replace deprecated alignment tags in inline math", async () => {
740-
// Arrange
741-
const question = {
742-
content:
743-
"Hello $\\begin{align}\n2\\text{HCl}(\\text{aq})+\\text{Ca}(\\text{OH})_2(\\text{aq})\\rightarrow\\text{Ca}(\\text{s})+2\\text H_2\\text O(\\text l)+\\text{Cl}_2(\\text g)\n\\end{align}$",
744-
images: {},
745-
widgets: {},
746-
} as const;
747-
748-
// Act
749-
renderQuestion(question);
750-
751-
// Assert
752-
await waitFor(() => {
753-
expect(
754-
screen.getByText(/\\begin\{aligned\}.*\\end\{aligned\}/),
755-
).toBeInTheDocument();
756-
});
757-
});
758-
759739
it("should replace deprecated alignment tags in block math", async () => {
760740
// Arrange
761741
const question = {

packages/perseus/src/components/zoomable-tex.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ const computeMathBounds = (
2222
parentNode: HTMLElement,
2323
parentBounds: {width: number; height: number},
2424
) => {
25-
const textElement =
26-
parentNode.querySelector(".katex-html") ||
27-
parentNode.querySelector(".MathJax");
25+
const textElement = parentNode.querySelector(".MathJax");
2826
const textBounds = {
2927
// @ts-expect-error - TS2531 - Object is possibly 'null'. | TS2339 - Property 'offsetWidth' does not exist on type 'Element'.
3028
width: textElement.offsetWidth,

packages/perseus/src/renderer.tsx

+1-8
Original file line numberDiff line numberDiff line change
@@ -1219,14 +1219,7 @@ class Renderer
12191219
);
12201220
}
12211221
if (node.type === "math") {
1222-
// Replace uses of \begin{align}...\end{align} which KaTeX doesn't
1223-
// support (yet) with \begin{aligned}...\end{aligned} which renders
1224-
// the same is supported by KaTeX. It does the same for align*.
1225-
// TODO(kevinb) update content to use aligned instead of align.
1226-
// TODO(LEMS-1608) Remove this replacement as MathJax supports the
1227-
// "align" macro correctly (and, in fact, it is not synonymous with
1228-
// "aligned").
1229-
const tex = node.content.replace(/\{align[*]?\}/g, "{aligned}");
1222+
const tex = node.content;
12301223

12311224
// We render math here instead of in perseus-markdown.jsx
12321225
// because we need to pass it our onRender callback.

packages/perseus/src/styles/styles.less

+2-12
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,10 @@
7070
}
7171

7272
// Inline math should have the same font-size as the surrounding text.
73-
// There is no wrapping element around .katex to indicate that it is
73+
// There is no wrapping element around TeX to indicate that it is
7474
// inline, so we rely on the "actual paragraph" check above
7575
// (a .paragraph that contains another .paragraph instead of a
7676
// .perseus-block-math).
77-
.katex,
7877
mjx-container {
7978
font-size: 100%;
8079
}
@@ -242,7 +241,6 @@
242241
.paragraph {
243242
.text(@choiceTextSize, @choiceLineHeight, @gray17);
244243

245-
.katex,
246244
mjx-container {
247245
color: @gray17;
248246
}
@@ -392,7 +390,6 @@
392390
.paragraph {
393391
.text(@choiceTextSize, @choiceLineHeight, @gray17);
394392

395-
.katex,
396393
mjx-container {
397394
color: @gray17;
398395
}
@@ -420,24 +417,17 @@
420417

421418
.math(@blockMathTextSize; @blockMathLineHeight;
422419
@inlineMathTextSize; @inlineMathLineHeight) {
423-
:is(.katex, mjx-container):not(.mafs-graph *) {
420+
mjx-container:not(.mafs-graph *) {
424421
font-size: @inlineMathTextSize;
425422
line-height: @inlineMathLineHeight;
426423
color: @gray17;
427424
}
428425

429-
.perseus-block-math .katex,
430426
.perseus-block-math mjx-container {
431427
font-size: @blockMathTextSize;
432428
line-height: @blockMathLineHeight;
433429
}
434430

435-
// HACK(charlie): I am ashamed. This seems to be the only way to revert
436-
// KaTeX rendered as Graphie labels to the KaTeX defaults, taken from:
437-
// https://github.com/Khan/KaTeX/blob/965b8a6164ee54212bf382108f256c6920eb7a70/static/katex.less#L18.
438-
// It's dangerous to resize labels, since they're deliberately
439-
// positioned relative to their background images.
440-
.graphie-label .katex,
441431
.graphie-label mjx-container {
442432
font-size: 1.21em;
443433
line-height: 1.2;

packages/perseus/src/styles/widgets/interactive-graph.less

-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
border-radius: 5px;
4343
bottom: 50px;
4444

45-
.katex,
4645
mjx-container {
4746
color: @kaGreen !important;
4847
}
@@ -57,7 +56,6 @@
5756
text-align: center;
5857
}
5958

60-
.graphie-label .katex,
6159
.graphie-label mjx-container {
6260
color: inherit !important;
6361
}

packages/perseus/src/styles/widgets/label-image.less

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
.perseus-block-math {
3838
padding: 0;
3939

40-
.katex,
4140
mjx-container {
4241
font-size: initial !important;
4342
line-height: initial !important;

packages/perseus/src/styles/widgets/passage.less

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
}
5151
}
5252
}
53-
.katex,
5453
mjx-container {
5554
line-height: @line-height - 2;
5655
}

packages/perseus/src/styles/widgets/plotter.less

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
border: solid 0.5px @gray76;
2424
border-radius: 4px;
2525

26-
.graphie-label .katex,
2726
.graphie-label mjx-container {
2827
color: @gray41;
2928
}

pnpm-lock.yaml

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testing/test-dependencies.tsx

+1-10
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,7 @@ export const testDependencies: PerseusDependencies = {
3636
addComponent: (renderer) => -1,
3737
removeComponentAtIndex: (index) => {},
3838
},
39-
// The KaTeX used in the 'should replace deprecated alignment tags in inline
40-
// math' test uses the `align` environment. This results in `array` nodes in
41-
// the parsed KaTeX node tree. When the Tex component tries to build an a11y
42-
// string for it, KaTeX throws an error because render-a11y-string.js doesn't
43-
// support `array` nodes. It then logs the error it to the server. However,
44-
// there is throttling code in the logKaTeXError() function which only reports
45-
// the error 1% of the time... so this causes _very_ rare test failures.
46-
// Mocking this here so that we don't fail because of this issue.
47-
logKaTeXError: (expression: string, error: Error): Promise<any> =>
48-
Promise.resolve({}),
39+
4940
TeX: ({children}: {children: React.ReactNode}) => {
5041
return <span className="mock-TeX">{children}</span>;
5142
},

0 commit comments

Comments
 (0)