mhchem registers on the KaTeX the preview renders with - #747
Merged
Conversation
…745) KaTeX publishes every contrib file twice: .mjs behind the import condition, .js (CommonJS, require("katex")) behind require. The loader asked for katex/dist/contrib/mhchem.js, so the bundler gave mhchem the CommonJS copy of KaTeX and \ce landed on a parser the preview never calls; $$\ce{...}$$ rendered as a parse error. The bare katex/contrib/* subpaths resolve to the .mjs, which imports the same katex.mjs the preview does. The built app now carries one KaTeX 0.18.4 instead of two.
This was referenced Sep 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this is
Closes #745.
$$\ce{2H2 + O2 -> 2H2O}$$rendered as a red parse error, "Undefined control sequence: \ce", although mhchem has been loaded since #411. The screenshot in the issue is that error, not raw text. A second commit fixes what showed up once\ceworked: the label on->[燃烧]had its top clipped.Mechanism
Two copies of KaTeX. KaTeX publishes every file twice:
dist/katex.mjsbehind theimportcondition anddist/katex.js(CommonJS) behindrequire, and the contrib files come in both flavours. The loader asked forkatex/dist/contrib/mhchem.js— the CommonJS one, which doesrequire("katex")— so the production bundle carried two copies of KaTeX 0.18.4.import('katex')gave the preview the ESM copy; mhchem registered\ceon the CommonJS copy. Display math goes throughkatex.renderToStringon the preview's copy and never saw the macro. Inline\(\ce{…}\)happened to work because auto-render, loaded the same way, ran on the copy mhchem had patched.Counted in
build/_app/immutable/chunksbefore and after:The fix is the bare
katex/contrib/mhchemandkatex/contrib/auto-rendersubpaths: the exports map resolves them to the.mjs, which imports../katex.mjs.window.katexis dropped with it — it existed only for the CommonJS build's global fallback and nothing else read it. One copy of KaTeX 0.16.47 remains: mermaid's own nested dependency, untouched.The clipped label.
.katex-displaysetsoverflow-y: hiddenso a wide formula's horizontal scrollbar does not bring a vertical one, and hidden overflow clips at the padding edge. KaTeX sizes the block from its own font metrics and has none for CJK, so the label in->[燃烧]— set in the fallback font — drew taller than the box and lost its top. The block now carries0.4emof vertical padding as headroom, and the margin drops from1.5emto1.1em, so the distance to the neighbouring blocks is unchanged.Scope
No change to the math pipeline, the delimiters or the renderer options. The test is a source assertion, deliberately: vitest and node both dedupe the two copies, so the split is visible only in a production build.
scripts/katexContribFlavour.test.tsfails on master and passes here. The padding has no test — a layout fact, checked on a build.Verification
On a macOS build: both display formulas from the issue render, the arrow label is whole. The clipping was seen on the build before the second commit and is gone after it.