Skip to content

Commit eb9f3f9

Browse files
authored
Remove 'katex' from @khanacademy/perseus (#1428)
## Summary: KaTeX was retired from use in Perseus in 2023. Today, I noticed that we still reference the `katex` package in the perseus `package.json`, but we never import it anywhere in the code. So I'm removing that dependency. I've also updated some code comments and storybook references to not mention KaTeX anymore. I've left some references to CSS classes and the KaTeX_Main font ref. I'm unsure if they might be important somewhere (especially if host applications may still set it outside of Perseus). So this doesn't fully remove all references to katex, but it moves us another step closer. Issue: "none" ## Test plan: `yarn tsc` `yarn lint` I ran storybook and opened the `ZoomableTex` stories and played with them. Note(jeremy): I've restored the code that swaps `\begin{align}` to `\begin{aligned}` and noted LEMS-1608 based on comments from Ben (thanks!). ~I also went to the `EditorPage` story and pasted in the following Tex into the **Question** field (which uses the macro that we used to have to replace for KaTeX, but MathJax supports properly: switching `\begin{align}` to `\begin{aligned}`).~ Author: jeremywiebe Reviewers: jeremywiebe, benchristel, Myranae, nicolecomputer Required Reviewers: Approved By: benchristel Checks: ✅ codecov/project, ✅ codecov/patch, ✅ Upload Coverage (ubuntu-latest, 20.x), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Jest Coverage (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: #1428
1 parent 8b02682 commit eb9f3f9

File tree

13 files changed

+52
-20
lines changed

13 files changed

+52
-20
lines changed

.changeset/mighty-news-repair.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@khanacademy/perseus": minor
3+
---
4+
5+
Drop katex dependency - no longer used

packages/perseus/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
"create-react-class": "15.6.3",
7676
"intersection-observer": "^0.12.0",
7777
"jquery": "^2.1.1",
78-
"katex": "0.11.1",
7978
"lodash.debounce": "^4.0.8",
8079
"perseus-build-settings": "^0.4.1",
8180
"prop-types": "15.6.1",

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

+28-3
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,29 @@ describe("renderer", () => {
713713
it("should replace deprecated alignment tags in inline math", async () => {
714714
// Arrange
715715
const question = {
716+
content:
717+
"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}$",
718+
images: {},
719+
widgets: {},
720+
} as const;
721+
722+
// Act
723+
renderQuestion(question);
724+
725+
// Assert
726+
await waitFor(() => {
727+
expect(
728+
screen.getByText(/\\begin\{aligned\}.*\\end\{aligned\}/),
729+
).toBeInTheDocument();
730+
});
731+
});
732+
733+
it("should replace deprecated alignment tags in block math", async () => {
734+
// Arrange
735+
const question = {
736+
// Math that exists by itself in a paragraph is considered
737+
// block math, even if it isn't surrounded by the block math
738+
// delimeters (`$$$...$$$`).
716739
content:
717740
"$\\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}$",
718741
images: {},
@@ -723,9 +746,11 @@ describe("renderer", () => {
723746
renderQuestion(question);
724747

725748
// Assert
726-
expect(
727-
screen.getByText(/\\begin\{aligned\}.*\\end\{aligned\}/),
728-
).toBeInTheDocument();
749+
await waitFor(() => {
750+
expect(
751+
screen.getByText(/\\begin\{aligned\}.*\\end\{aligned\}/),
752+
).toBeInTheDocument();
753+
});
729754
});
730755
});
731756

packages/perseus/src/components/__stories__/graph.stories.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as React from "react";
22

33
import Graph from "../graph";
4-
// TODO(scottgrant): Katex is unavailable here. Fix!
54

65
type StoryArgs = Record<any, any>;
76

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ const ForceZoomWrapper = ({children}: Props): React.ReactElement => (
2323
</>
2424
);
2525

26-
export const KaTeX = (args: StoryArgs): React.ReactElement => {
26+
export const Tex = (args: StoryArgs): React.ReactElement => {
2727
return (
2828
<ForceZoomWrapper>
2929
<ZoomableTex children="\sum_{i=1}^\infty\frac{1}{n^2} =\frac{\pi^2}{6}" />
3030
</ForceZoomWrapper>
3131
);
3232
};
3333

34-
export const ComplexKaTeX = (args: StoryArgs): React.ReactElement => {
34+
export const ComplexTex = (args: StoryArgs): React.ReactElement => {
3535
return (
3636
<ForceZoomWrapper>
3737
{" "}

packages/perseus/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export {
8484
*/
8585
export {default as Util} from "./util";
8686
export {default as KhanColors} from "./util/colors";
87-
export {default as preprocessTex} from "./util/katex-preprocess";
87+
export {default as preprocessTex} from "./util/tex-preprocess";
8888
export {registerAllWidgetsForTesting} from "./util/register-all-widgets-for-testing";
8989
export * as SizingUtils from "./util/sizing-utils";
9090
export {

packages/perseus/src/interactive2/movable-point.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,9 @@ export class MovablePoint {
521521
* Displays a tooltip above the point, replacing any previous contents. If
522522
* there is no tooltip initialized, adds the tooltip.
523523
*
524-
* If the type of contents is string, the contents will be rendered with
525-
* KaTeX. Otherwise, the content will be assumed to be a DOM node and will
526-
* be appended inside the tooltip.
524+
* If the type of contents is string, the contents will be rendered as TeX
525+
* Otherwise, the content will be assumed to be a DOM node and will be
526+
* appended inside the tooltip.
527527
*/
528528
_showTooltip(contents) {
529529
if (!this._tooltip) {

packages/perseus/src/perseus-types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ export const PerseusExpressionAnswerFormConsidered = [
436436
] as const;
437437

438438
export type PerseusExpressionAnswerForm = {
439-
// The Katex form of the expression. e.g. "x\\cdot3=y"
439+
// The TeX form of the expression. e.g. "x\\cdot3=y"
440440
value: string;
441441
// The Answer expression must have the same form
442442
form: boolean;
@@ -889,7 +889,7 @@ export type PerseusGraphTypeRay = {
889889
} & PerseusGraphTypeCommon;
890890

891891
export type PerseusLabelImageWidgetOptions = {
892-
// Translatable Text; Katex representation of choices
892+
// Translatable Text; Tex representation of choices
893893
choices: ReadonlyArray<string>;
894894
// The URL of the image
895895
imageUrl: string;

packages/perseus/src/renderer.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import PerseusMarkdown from "./perseus-markdown";
2626
import QuestionParagraph from "./question-paragraph";
2727
import TranslationLinter from "./translation-linter";
2828
import Util from "./util";
29-
import preprocessTex from "./util/katex-preprocess";
29+
import preprocessTex from "./util/tex-preprocess";
3030
import WidgetContainer from "./widget-container";
3131
import * as Widgets from "./widgets";
3232

@@ -1303,6 +1303,9 @@ class Renderer extends React.Component<Props, State> {
13031303
// support (yet) with \begin{aligned}...\end{aligned} which renders
13041304
// the same is supported by KaTeX. It does the same for align*.
13051305
// TODO(kevinb) update content to use aligned instead of align.
1306+
// TODO(LEMS-1608) Remove this replacement as MathJax supports the
1307+
// "align" macro correctly (and, in fact, it is not synonymous with
1308+
// "aligned").
13061309
const tex = node.content.replace(/\{align[*]?\}/g, "{aligned}");
13071310

13081311
// We render math here instead of in perseus-markdown.jsx

packages/perseus/src/types.ts

-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ export type APIOptions = Readonly<{
318318

319319
type TeXProps = {
320320
children: string;
321-
katexOptions?: any;
322321
onClick?: () => unknown;
323322
onRender?: (root?: any) => unknown;
324323
style?: any;
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/**
2-
* Preprocess TeX code to convert things that KaTeX doesn't know how to handle
2+
* Preprocess TeX code to convert things that MathJax doesn't know how to handle
33
* to things is does.
44
*/
5-
65
export default (texCode: string): string =>
76
texCode
87
// Replace uses of \begin{align}...\end{align} which KaTeX doesn't
98
// support (yet) with \begin{aligned}...\end{aligned} which renders
109
// the same is supported by KaTeX. It does the same for align*.
1110
// TODO(kevinb) update content to use aligned instead of align.
11+
// TODO(LEMS-1608) Remove this replacement as MathJax supports the
12+
// "align" macro correctly (and, in fact, it is not synonymous with
13+
// "aligned").
1214
.replace(/\{align[*]?\}/g, "{aligned}")
1315
// Replace non-breaking spaces with regular spaces.
1416
.replace(/[\u00a0]/g, " ");

packages/perseus/src/util/tex.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ function findChildOrAdd(elem: any, className: string) {
1616
}
1717

1818
export default {
19-
// Process a node and add math inside of it. This attempts to use KaTeX to
20-
// format the math, and if that fails it falls back to MathJax.
19+
// Process a node and add math inside of it. This uses MathJax to
20+
// format the math.
2121
//
2222
// elem: The element which the math should be added to.
2323
//

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {usePerseusI18n} from "../../components/i18n-context";
1414
import Renderer from "../../renderer";
1515

1616
export type AnswerType = {
17-
// The answer string, can be plain text or a KaTeX expression.
17+
// The answer string, can be plain text or a TeX expression.
1818
content: string;
1919
// Whether the answer is selected.
2020
checked: boolean;

0 commit comments

Comments
 (0)