Skip to content

Update MathJax to version 4 for format preview. - #1599

Merged
cpeel merged 3 commits into
DistributedProofreaders:masterfrom
mrducky4:mathjax4
Jul 29, 2026
Merged

Update MathJax to version 4 for format preview.#1599
cpeel merged 3 commits into
DistributedProofreaders:masterfrom
mrducky4:mathjax4

Conversation

@mrducky4

@mrducky4 mrducky4 commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator

Use npm to install mathjax locally, and serve it locally, no longer using the jsdelivr CDN.
Adjust async behavior to await for MathJax.
Update Content-Security-Policy to allow MathJax to dynamically load a worker and fonts.
Fix old bug that showed a mathjax error (this exists in production).

The motivation for this is to use the same version of mathjax in both Format Preview and also the separate m2svg tool. This helps to avoid inconsistencies between the two tools, which are often used together for the same project.

To reproduce the old bug with the mathjax error:

  1. Initial condition, Preview Math is disabled in format preview configuration.
  2. Start proofing a page.
  3. Click Preview to enter the format preview view.
  4. Click Configure, then enable Preview Math, then click OK.
  5. Expected: you see the preview view, with no error messages.
  6. Actual: you see a pop-up error about MathJax.typeset is not a function.

I tested with math markup from PR #1527, which also has the expected output in preview mode.

To verify new consistency with m2svg 3.4.0, use this markup:
Math with text inside: \(a + b = c + d + \text{\&c.}\)
Expected output is a + b = c + d + &c. where a b c d are italic, &c is not italic. (The production version, with mathjax 3, shows it as a + b = c + d + \&c.)

I also tried various sequences of enabling/disabling Preview Math in the format preview configuration.

Sandbox at: https://www.pgdp.org/~mrducky/c.branch/mathjax4

Adjust async behavior to await for MathJax.
Update Content-Security-Policy to allow MathJax to dynamically
load a worker and fonts.
@cpeel

cpeel commented Jul 26, 2026

Copy link
Copy Markdown
Member

Can we simplify the CSP by bundling mathjax with the code rather than pulling from the CDN? See commit b003380 in branch https://github.com/cpeel/dproofreaders/commits/mathjax4/ for how we could do that pretty easily. We do this for other JS code like d3.

Then the file is in node_modules/mathjax/tex-svg.js.

@mrducky4

Copy link
Copy Markdown
Collaborator Author

I thought that might be an option, I'll take a closer look.

@mrducky4

Copy link
Copy Markdown
Collaborator Author

Just checking - I think I should commit the corresponding update to package-lock.json too, right?

Mrducky added 2 commits July 26, 2026 20:43
Simplify Content-Security-Policy to remove jsdelivr.
Fix mathjax error when first enabling Preview Math.
@mrducky4
mrducky4 marked this pull request as ready for review July 26, 2026 21:02
@cpeel
cpeel requested review from 70ray, chrismiceli, cpeel and srjfoo July 26, 2026 22:46

@cpeel cpeel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Code looks good to me but I would appreciate @chrismiceli's eyes on it as he's our resident JS guru.

initView().then(function () {
writePreviewText();
hideConfig();
writePreviewText().then(function () {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Very minor, but if we are using async await above, we could here too I imagine.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I think so, but I didn't change it since it was already using .then(). It should be equivalent either way.

@cpeel
cpeel merged commit b9f2b85 into DistributedProofreaders:master Jul 29, 2026
12 checks passed
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.

5 participants