Skip to content

rehype-mathjax: fix SVG output with global font cache - #123

Open
mkantautas wants to merge 1 commit into
remarkjs:mainfrom
mkantautas:fix/mathjax-global-font-cache
Open

mkantautas wants to merge 1 commit into
remarkjs:mainfrom
mkantautas:fix/mathjax-global-font-cache

Conversation

@mkantautas

Copy link
Copy Markdown

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and discussions and couldn’t find anything or linked relevant results below
  • I made sure the docs are up to date
  • I included tests (or that’s not needed)

Description of changes

With rehype-mathjax/svg and svg: {fontCache: 'global'}, the glyphs of all math are missing from the output:

const file = await unified()
  .use(remarkParse)
  .use(remarkMath)
  .use(remarkRehype)
  .use(rehypeMathjaxSvg, {svg: {fontCache: 'global'}})
  .use(rehypeStringify)
  .process('$\\alpha$')
// Before: `<use data-c="1D6FC" xlink:href="#MJX-TEX-I-1D6FC"></use>`, and no element with that `id`
// After:  the same `<use>`, plus `<svg style="display: none;" id="MJX-SVG-global-cache"><defs><path id="MJX-TEX-I-1D6FC" …`

With a global font cache, each glyph is a <use> that points into one shared <defs>.
MathJax returns that cache from pageElements() and adds it to <body> when it typesets a page itself.
rehype-mathjax only added the style sheet, so in browsers the math shows up blank: only rules, such as fraction bars, are drawn.

This adds a pageElements step next to styleSheet.
The hidden <svg> is appended to <body>, or to the end of the tree for fragments.
CHTML, fontCache: 'local' (the default), and 'none' have no page elements, so their output does not change.

The two new fixtures (a fragment and a complete document) fail without the change.
Rendered in Chromium 149 and Firefox 151, none of the 21 glyphs of a remark-math example are drawn before the change, and all 21 after.

npm test passes with Node 18.20.8 and dev-dependencies from npm install --before=2025-02-21.
With current dependencies, the existing rehype-katex test “should handle errors, support errorColor” also fails on main (newer vfile-message fields), as in the CI run of #122.

@github-actions github-actions Bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Oct 23, 2026
@ChristianMurphy

Copy link
Copy Markdown
Member

Thanks for the write-up. Did you hit this in a project? fontCache: 'global' isn't the default, and I couldn't find an issue about it. This adds a renderer step we'd maintain from here on. Could you open an issue with your use case first, as the contributing guide suggests, so we can agree on the approach?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤞 phase/open Post is being triaged manually

Development

Successfully merging this pull request may close these issues.

2 participants