Skip to content

equation numbering in math with markdown-it-mathjax3 triggers duplicate label errors #4740

Open
@jkieboom

Description

@jkieboom

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

  1. Enable math and install markdown-it-mathjax3
  2. Create a simple page with an equation using \begin{align} ... \label{eq} \end{align}
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    upstreamRelated to the dependencies

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions