Repository navigation
Support MathJax 4 in MJxPrep.js - #355
Conversation
openedx/openedx-platform#33555 moves the LMS from MathJax 2.7.5 to 4.1.2 and drops the MathJax.Hub API. MJxPrep.js re-rendered previews with MathJax.Hub.Queue before clearing its poll, so on MathJax 4 answer previews for saved answers stay empty in every course that uses it. MathJax 4's AsciiMath component still exposes the legacy AM object (and a legacy MathJax.Hub), so the symbol definitions carry over unchanged. The re-render now branches on MathJax.version: v2 keeps Hub.Queue(Reprocess), v4 waits for MathJax.startup.promise and replays a keyup on the inputs CAPA binds Problem.refreshMath to. The poll stops before anything that can throw and gives up after a minute, and the console log carries the file's version (also on window.MJxPrep.version) so a course's copy can be identified. Claude-Session: https://claude.ai/code/session_01FGZebFEDxey1jTnNLNZAbD
The Jest tests run in jsdom, which can't run MathJax, so nothing caught MJxPrep.js breaking on MathJax 4. This adds a Playwright test that loads MJxPrep.js into a page mimicking a CAPA textline problem, with the MathJax config and CAPA refreshMath from before and after openedx/openedx-platform#33555. MathJax is requested from the same CDN URLs the LMS uses and served from node_modules, so the test runs offline. It checks that saved answers preview with MJxPrep's preprocessor and AsciiMath definitions, that there are no console errors, and that the AsciiMath poll stops. MJXPREP_JS points it at another copy, e.g. a course's. Renovate leaves the MathJax packages alone because they mirror what edx-platform serves. Claude-Session: https://claude.ai/code/session_01FGZebFEDxey1jTnNLNZAbD
CAPA loads the preprocessor script right before it renders saved
answers. When MathJax 4 has already started by then, the first poll
tick 200 ms later is too late: the previews render with stock
AsciiMath first, and MathJax logs a TypeError parsing {:hbar:}. On
MathJax 2.7 the hidden dynamath MathML kept the stock rendering, since
Reprocess only redraws the visible preview.
Claude-Session: https://claude.ai/code/session_01FGZebFEDxey1jTnNLNZAbD
Each MathJax version now also runs with MJxPrep.js loaded after MathJax's first typeset, the order that exposed the AsciiMath timing bug, and checks a formulaequationinput preview on MathJax 4, which goes through AsciiMath without a preprocessor. The MathJax 4 font package is pinned to 4.1.2 to match MathJax (npm resolved ^4.1.2 to 4.1.3, which MathJax warned about), and the test fails on that warning. The poll check now also requires that the poll started. Claude-Session: https://claude.ai/code/session_01FGZebFEDxey1jTnNLNZAbD
There was a problem hiding this comment.
Summary
Supports MathJax 4 while preserving answer previews on MathJax 2.7.
- Adds: MathJax 4 startup handling and keyup replay for saved answer previews.
- Fixes: AsciiMath polling that throws repeatedly or continues forever when MathJax is unavailable.
- Adds: version reporting through the console and
window.MJxPrep.version.
Dependencies changed: mitxgraders-js/package-lock.json, mitxgraders-js/package.json
Details for reviewers
Risks
mathjax2adds a known ReDoS-vulnerable test fixture, although it is used only for browser tests.
Core files: MJxPrep.js, .github/workflows/ci.yaml
More changes
- Adds: browser coverage for MathJax 2.7.5 and 4.1.2 with early and late script loading. (
mjxprep.spec.js,playwright.config.js,ci.yaml) - Adds: pinned MathJax fixtures and Playwright dependencies for offline browser tests. (
package.json,package-lock.json,renovate.json) - Changes: the changelog to document MathJax 4 support and the course copy update requirement. (
changelog.md)
Unchanged
- MathJax 2.7 previews still re-render through
MathJax.Hub.Queue. - Custom AsciiMath symbol definitions remain unchanged.
Summary of 19b91c7 · React 👍 or 👎 on my comments to tune what I raise · Commands · Review config
There was a problem hiding this comment.
Status: Code Looks Good 👍
No blocking issues found.
1 finding outside this PR's diff
Caused by these changes but in code the PR doesn't touch — so they can't be inline.
docs/grading_math/renderer.md:11
This paragraph still says formulaequationinput previews are server-side and "we are unable to enhance the display at all", but on MathJax 4 (post openedx/openedx-platform#33555) CAPA previews that input client-side through AsciiMath, and MJxPrep's definitions do apply — the new browser test asserts exactly this (arcsech rendering in the formulaequationinput preview). Update the paragraph to note the MathJax 4 behavior so course authors know the definitions now help formulaequationinput too.
[Documentation] · non-blocking — stale doc text; no code or rendering behavior affected
— disagree? Dismiss
📌 Reviewed up to the latest commit · ⏱️ Review completed in 7 minutes
- 🔄 re-review this PR
On MathJax 4, CAPA renders formulaequationinput previews client-side with AsciiMath, so MJxPrep's symbol definitions apply to them, which the renderer docs said was impossible. Claude-Session: https://claude.ai/code/session_01FGZebFEDxey1jTnNLNZAbD
|
Fixed in cca7684: |
What are the relevant tickets?
https://github.mit.edu/mitxonline/mitxonline-issues/issues/1652
Description (What does it do?)
openedx/edx-platform#33555 moves the LMS from MathJax 2.7.5 to 4.1.2 and drops the
MathJax.HubAPI. On MathJax 4, MJxPrep.js throws fromMathJax.Hub.Queueevery 200 ms and never stops polling, so the answer previews of saved answers stay empty. This makes MJxPrep.js work on both versions and adds a browser test against real MathJax.Hub.Queue(Reprocess). On 4.x, MJxPrep.js waits forMathJax.startup.promiseand replays a keyup on the inputs CAPA bindsrefreshMathto.window.MJxPrep.versioncarry the file's version, so a course's copy can be identified.node_modulesat the CDN URLs edx-platform uses.Implementation details
input/asciimathcomponent wraps the legacy v2 AsciiMath code, soMathJax.InputJax.AsciiMath.AM.newsymbolstill exists and the symbol definitions are unchanged. The component also defines a legacyMathJax.Hubwhose queue is undefined, so the code branches onMathJax.versionrather than on whetherHubexists.Problem.refreshMath, bound to keyup oninput.math, .formulaequationinput input(display.js after #33555). The correctness reset listens forinput/change, not keyup, so the replay doesn't clear answer status.Problem.renderruns the preprocessor script and thenbind()straight away. If MathJax has already started, the old 200 ms delay let saved answers render with stock AsciiMath first, which on 4.1.2 logs aTypeErrorwhile parsing{:hbar:}, and on 2.7 left the hidden_dynamathMathML wrong.MJXPREP_JS=/path/to/copyruns them against a course's copy.mathjaxand@mathjax/mathjax-newcm-fontalone, since they mirror what edx-platform serves. The font is pinned to 4.1.2 throughoverrides, because^4.1.2resolved to 4.1.3 and MathJax warned about the mismatch.npm auditflags the pinnedmathjax@2.7.5(GHSA-v638-q856-grg8, ReDoS). It is only a test fixture for what the LMS served before #33555.mitxgraders-js/MJxPrep.jsandcourse/static/MJxPrep.jswere already symlinks to the root file, so there is still one source.How can this be tested?
Locally, Jest passed 29/29 and Playwright passed 4/4 (40/40 with
--repeat-each=10). WithMJXPREP_JSpointed at master's MJxPrep.js, all four browser tests fail. On 4.1.2 the previews stay empty and the page throwsCannot read properties of undefined (reading 'Push')every 200 ms. The newMathJax-Browser-Testsjob runs the same tests in CI.This hasn't been checked in a real LMS yet. That comes next, on the demo course on rc once rc is back on an edx-platform build with MathJax 4.
Additional Context
MJXPREP_VERSIONis3.1.0, ahead ofmitxgraders/version.py(3.0.0). The changelog lists 3.1.0 as unreleased. Bumping the version, buildingpython_lib.zip, and replacing the copy in each course run are separate follow-up work. Courses keep their own copy, so this fixes nothing for learners until each course run's asset is replaced.