Skip to content

Support MathJax 4 in MJxPrep.js - #355

Merged
blarghmatey merged 5 commits into
masterfrom
mjxprep-mathjax4
Nov 16, 2026
Merged

blarghmatey merged 5 commits into
masterfrom
mjxprep-mathjax4

Conversation

@blarghmatey

Copy link
Copy Markdown
Member

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.Hub API. On MathJax 4, MJxPrep.js throws from MathJax.Hub.Queue every 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.

  • On MathJax 2.7, previews still re-render through Hub.Queue(Reprocess). On 4.x, MJxPrep.js waits for MathJax.startup.promise and replays a keyup on the inputs CAPA binds refreshMath to.
  • The AsciiMath check runs once as soon as the script loads, then every 200 ms for up to a minute. The interval is cleared before anything that can throw.
  • The console log line and window.MJxPrep.version carry the file's version, so a course's copy can be identified.
  • A Playwright job loads MJxPrep.js into a page that mimics a CAPA textline and formulaequationinput, using MathJax 2.7.5 and 4.1.2 served from node_modules at the CDN URLs edx-platform uses.
Implementation details
  • MathJax 4's input/asciimath component wraps the legacy v2 AsciiMath code, so MathJax.InputJax.AsciiMath.AM.newsymbol still exists and the symbol definitions are unchanged. The component also defines a legacy MathJax.Hub whose queue is undefined, so the code branches on MathJax.version rather than on whether Hub exists.
  • On 4.x, CAPA only re-renders a preview in Problem.refreshMath, bound to keyup on input.math, .formulaequationinput input (display.js after #33555). The correctness reset listens for input/change, not keyup, so the replay doesn't clear answer status.
  • The check at load matters because Problem.render runs the preprocessor script and then bind() 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 a TypeError while parsing {:hbar:}, and on 2.7 left the hidden _dynamath MathML wrong.
  • The tests run each MathJax version twice: once with MJxPrep.js loaded while MathJax is still loading, once after MathJax's first typeset. Any request outside the routed CDN paths fails the test. MJXPREP_JS=/path/to/copy runs them against a course's copy.
  • Renovate leaves mathjax and @mathjax/mathjax-newcm-font alone, since they mirror what edx-platform serves. The font is pinned to 4.1.2 through overrides, because ^4.1.2 resolved to 4.1.3 and MathJax warned about the mismatch. npm audit flags the pinned mathjax@2.7.5 (GHSA-v638-q856-grg8, ReDoS). It is only a test fixture for what the LMS served before #33555.
  • mitxgraders-js/MJxPrep.js and course/static/MJxPrep.js were already symlinks to the root file, so there is still one source.

How can this be tested?

cd mitxgraders-js
npm ci
npx playwright install chromium
npm test
npm run test:browser

Locally, Jest passed 29/29 and Playwright passed 4/4 (40/40 with --repeat-each=10). With MJXPREP_JS pointed at master's MJxPrep.js, all four browser tests fail. On 4.1.2 the previews stay empty and the page throws Cannot read properties of undefined (reading 'Push') every 200 ms. The new MathJax-Browser-Tests job 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_VERSION is 3.1.0, ahead of mitxgraders/version.py (3.0.0). The changelog lists 3.1.0 as unreleased. Bumping the version, building python_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.

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

@agent-optibot agent-optibot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  • mathjax2 adds 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

@agent-optibot agent-optibot Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@blarghmatey

Copy link
Copy Markdown
Member Author

Fixed in cca7684: docs/grading_math/renderer.md now says that on MathJax 4 the formulaequationinput preview is rendered client-side through AsciiMath, so MJxPrep's symbol definitions apply to it but the preprocessor doesn't (per refreshMath in edx-platform's display.js at 78b892c6e2d).

@blarghmatey
blarghmatey merged commit 8669719 into master Nov 16, 2026
30 of 31 checks passed
@blarghmatey
blarghmatey deleted the mjxprep-mathjax4 branch November 16, 2026 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant