Skip to content

Commit

Permalink
Delete unused code from tex.ts (Khan#927)
Browse files Browse the repository at this point in the history
## Summary:
There was a lot of code in this file related to our old KaTeX+MathJax2
renderer. Now that we have switched to MathJax 3, we can simplify things. I've
also deleted a couple functions that simply weren't used anywhere.

I changed the type of `processMath`'s `text` param to `string | number` because
the code that generates Graphie labels sometimes passes a number.

Issue: none

Test plan:

Review storybook stories that use TeX labels:

- Grapher widget
- Interactive Graph widget
- Number Line widget

The graph labels should display in the correct place.

Author: benchristel

Reviewers: benchristel, jeremywiebe

Required Reviewers:

Approved By: jeremywiebe

Checks: ✅ codecov/project, ✅ codecov/patch, ✅ Upload Coverage, ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Extract i18n strings (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Jest Coverage (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ gerald

Pull Request URL: Khan#927
  • Loading branch information
benchristel authored Jan 18, 2024
1 parent 4c2c2ab commit 1e17919
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 656 deletions.
6 changes: 6 additions & 0 deletions .changeset/proud-donkeys-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@khanacademy/perseus": patch
---

Remove unused code related to KaTeX and MathJax 2. It's no longer needed
because all callers have upgraded to MathJax 3.
95 changes: 11 additions & 84 deletions packages/perseus/src/util/tex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {getDependencies} from "../dependencies";
import KhanMath from "./math";
import reactRender from "./react-render";

declare const MathJax: any;

function findChildOrAdd(elem: any, className: string) {
const $child = $(elem).find("." + className);
if ($child.length === 0) {
Expand All @@ -16,27 +14,6 @@ function findChildOrAdd(elem: any, className: string) {
return $child;
}

function doCallback(
elem: HTMLElement,
callback: (() => unknown) | (() => void),
) {
let tries = 0;
(function check() {
const height = elem.scrollHeight;
// Heuristic to guess if the font has kicked in
// so we have box metrics (magic number ick,
// but this seems to work mostly-consistently)
if (height > 18 || tries >= 10) {
callback();
} else {
tries++;
// TODO(jeff, CP-3128): Use Wonder Blocks Timing API
// eslint-disable-next-line no-restricted-syntax
setTimeout(check, 100);
}
})();
}

export default {
// Process a node and add math inside of it. This attempts to use KaTeX to
// format the math, and if that fails it falls back to MathJax.
Expand All @@ -55,37 +32,22 @@ export default {
// processed
processMath: async function (
elem: HTMLElement,
text: string,
text: string | number,
force?: boolean,
callback?: () => unknown,
) {
const $elem = $(elem);

// Only process if it hasn't been done before, or it is forced
if ($elem.attr("data-math-formula") == null || force) {
const $katexHolder = findChildOrAdd($elem, "katex-holder");
const $mathjaxHolder = findChildOrAdd($elem, "mathjax-holder");

// Search for MathJax-y script tags inside of the node. These are
// used by MathJax to denote the formula to be typeset. Before, we
// would update the formula by updating the contents of the script
// tag, which shouldn't happen any more, but we manage them just in
// case.
const script: HTMLElement | undefined = $mathjaxHolder.find(
"script[type='math/tex']",
)[0];

// If text wasn't provided, we look in two places
if (text == null) {
if ($elem.attr("data-math-formula")) {
// The old typeset formula
// @ts-expect-error - TS2322 - Type 'string | undefined' is not assignable to type 'string'.
text = $elem.attr("data-math-formula");
} else if (script) {
// The contents of the <script> tag
// @ts-expect-error - TS2339 - Property 'text' does not exist on type 'HTMLElement'.
text = script.text || script.textContent;
}
const $texHolder = findChildOrAdd($elem, "tex-holder");

// If text wasn't provided, we use the cached text.
// TODO(benchristel): I'm not sure if text can ever be null. It's
// possible we don't need this check.
if (text == null && $elem.attr("data-math-formula")) {
// @ts-expect-error - TS2322 - Type 'string | undefined' is not assignable to type 'string'.
text = $elem.attr("data-math-formula");
}

text = text != null ? text + "" : "";
Expand All @@ -102,45 +64,10 @@ export default {
reactRender(
React.createElement(TeX, {
children: text,
onRender: () => {
if (callback) {
doCallback(elem, callback);
}
},
onRender: callback,
}),
$katexHolder[0],
$texHolder[0],
);
}
},

// Function to restore a node to a non-math-processed state
cleanupMath: function (elem: HTMLElement): HTMLElement {
const $elem = $(elem);

// Only mess with it if it's been processed before
if ($elem.attr("data-math-formula")) {
// Remove MathJax remnants
if (typeof MathJax !== "undefined") {
const jax = MathJax.Hub.getJaxFor($elem.find("script")[0]);
if (jax) {
const e = jax.SourceElement();
if (e.previousSibling && e.previousSibling.className) {
jax.Remove();
}
}
}

// @ts-expect-error - TS2769 - No overload matches this call.
$elem.text($elem.attr("data-math-formula"));
$elem.attr("data-math-formula", null);
$elem.attr("data-math-type", null);
}

return elem;
},

// Function to retrieve the formula of a typeset math node
retrieveMathFormula: function (elem: HTMLElement): any {
return $(elem).attr("data-math-formula");
},
};
Loading

0 comments on commit 1e17919

Please sign in to comment.