Description
Describe the bug
I'm using vitepress with math through markdown-it-mathjax3 as shown in the documentation. When I write equations with numbering and labels I get errors in dev mode about duplicate labels being defined. If I turn off duplicate label errors in the config then whenever there is an update in dev mode the equation numbering increases.
Reproduction
- Enable
math
and installmarkdown-it-mathjax3
- Create a simple page with an equation using
\begin{align} ... \label{eq} \end{align}
- Run vitepress in dev mode
Expected behavior
Equations should be shown and numbered starting at 1 even when updating the markdown page and using hmr.
System Info
System:
OS: macOS 15.4.1
CPU: (12) arm64 Apple M2 Pro
Memory: 168.23 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.14.0 - ~/.nvm/versions/node/v22.14.0/bin/node
Yarn: 4.5.1 - ~/.nvm/versions/node/v22.14.0/bin/yarn
npm: 10.9.2 - ~/.nvm/versions/node/v22.14.0/bin/npm
Browsers:
Chrome: 136.0.7103.93
Chrome Canary: 138.0.7168.0
Edge: 136.0.3240.50
Safari: 18.4
Safari Technology Preview: 18.4
Additional context
The problem is caused by mathjax needing a state reset (through texReset
) whenever content is re-rendered. Otherwise the state contains previously defined labels and equation numbering. I managed to work around this locally by forking markdown-it-mathjax3 and monkey patching md.render
so reset the InputJax state:
// Patch of original plugin. Monkey patch md.render and reset InputJax to fix equation label
// counts and refs
const originalRender = md.render;
md.render = function (src: string, env?: any) {
documentOptions.InputJax.reset();
return originalRender.call(this, src, env);
};
This isn't a proper solution of course, and I'm not sure there is something vitepress can do by itself, or if this would need to be fixed in the plugin and/or markdown-it.
Relevant markdown-it-mathjax3 issue: tani/markdown-it-mathjax3#55 (comment)
Validations
- Check if you're on the latest VitePress version.
- Follow our Code of Conduct
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.