Skip to content

fix(math-block): export the source of a formula that does not compile - #3389

Merged
nperez0111 merged 2 commits into
mainfrom
fix/math-invalid-formula-export
Nov 24, 2026
Merged

nperez0111 merged 2 commits into
mainfrom
fix/math-invalid-formula-export

Conversation

@YousefED

@YousefED YousefED commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A math block whose formula does not compile makes the HTML export and the markdown export throw. Inline math with such a formula is not in the export. A user makes this state while typing, for example \frac{1}{.

KaTeX gives no math element for such a formula, and the two export components then rendered nothing. For such a formula, they now render a math element with the source as text and the application/x-tex annotation. Thus the HTML parse gives the formula back, and the markdown export writes the source.

Testing

  • Five new tests for the block and the inline content. They fail on main.
  • The unit and format tests pass, except one Typst test that needs a package that is not built locally (the same on main). No browser run was made locally.

Notes

latexToMathMLElement is a public export of @blocknote/math-block. Its return type is now { mathMLElement } | { error }. Thus TypeScript code outside this repository that reads mathMLElement without a check of error does not compile. The alternative is to keep the old type and to make the check only in the two components.

Summary by CodeRabbit

  • Bug Fixes
    • Invalid or malformed math formulas are now preserved in HTML and Markdown exports instead of being lost when rendering fails.
    • HTML exports display the original formula as readable error text, and importing that HTML retains the math content.
    • Math blocks and inline formulas remain enclosed in their appropriate display- or inline-math delimiters in Markdown exports.

…s text

A math block with a formula that KaTeX cannot render (for example
"\frac{1}{", a state that a user makes with each keystroke) stopped the
external HTML export and the Markdown export of the full document:
"Cannot read properties of null (reading 'classList')". Inline math with
such a formula was not in the export, without an error.

Cause: for such a formula, the output of KaTeX has no math element. The
adapter latexToMathMLElement had a cast that hid this case. The
components for the external HTML then rendered nothing, and the
serializer of core needs an element for each block.

Fix: latexToMathMLElement now returns a result type, as latexToMathML
of the other exporters does: the element, or a typed error if KaTeX has
no MathML for the formula. Thus the compiler makes each caller handle
that case. The new function mathMLExportContent gives the content of
the math element for the two cases. Without MathML from KaTeX, the
content is an merror element with the source as text, and the
application/x-tex annotation with the source. Thus, the HTML parse
reads the formula back as the same block (or inline content) with the
same source, and the Markdown export writes the source between dollar
signs, as for a formula that compiles.

Not changed: a formula that KaTeX renders in part (for example
"x \badcommand y", with the unknown command in red) exports its MathML
as before.
@vercel

vercel Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
blocknote Ready Ready Preview Nov 24, 2026 1:50pm UTC
blocknote-website Ready Ready Preview Nov 24, 2026 1:50pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

No actionable comments were generated in the recent review.

Walkthrough

Math export now preserves malformed LaTeX in generated MathML error content. Block and inline HTML exporters use the shared content helper. Tests check HTML export, HTML round trips, and Markdown export for malformed formulas.

Changes: MathML export fallback

Layer / File(s) Summary
Conversion results and error content
packages/math-block/src/helpers/toExternalHTML/latexToMathMLElement.ts, packages/math-block/src/helpers/toExternalHTML/mathMLExportContent.ts
latexToMathMLElement now returns either a MathML element or a parser error. mathMLExportContent returns converted MathML or error content containing the source as text and an application/x-tex annotation.
Block and inline export handling
packages/math-block/src/block/helpers/toExternalHTML/BlockMathMLElement.tsx, packages/math-block/src/inlineContent/helpers/toExternalHTML/InlineMathMLElement.tsx, packages/math-block/src/block/createReactMathBlockSpec.test.tsx, packages/math-block/src/inlineContent/createReactMathInlineContentSpec.test.tsx
Block and inline exporters use mathMLExportContent instead of returning no element when conversion has no MathML output. Tests check malformed formulas in HTML and Markdown exports, including HTML round trips.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: nperez0111, matthewlipski

Merge Risk: Minimal · previous review 1ce72

Malformed math formulas now export their source instead of throwing or being dropped in HTML and Markdown exports. The change is narrow and tested. The only visible compatibility effect is that callers of the exported conversion helper must now check the error case.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the export of a math formula that does not compile now emits its source. It uses the conventional fix(scope) form and matches the objectives, which describe rendering …
Description check Passed The description follows the template's main sections: Summary, Testing, and Notes. It explains the problem, the fix, the tests with their status, and a public API change. It omits the Rationale, Impac…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the export of a math formula that does not compile now emits its source. It uses the conventional fix(scope) form and matches the objectives, which describe rendering the formula source as text in a math element when KaTeX gives no MathML.

Full details: Description check

Explanation

The description follows the template's main sections: Summary, Testing, and Notes. It explains the problem, the fix, the tests with their status, and a public API change. It omits the Rationale, Impact, Screenshots/Video, Checklist, and Additional Notes headings. The Impact content is partly covered in Notes, but the Rationale and Checklist are missing.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

I found a formula gone astray,
Its source stays safe in MathML.
Block and inline exports keep it,
Round trips bring the text back home.
I twitch my nose and hop away.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ffbdb8aa-4930-4f3f-b877-1529cdbe4066
Commits

Reviewing files that changed from the base of the PR and between bd39cd4 and 1ce7278.

Files selected for processing: 6

  • packages/math-block/src/block/createReactMathBlockSpec.test.tsx
  • packages/math-block/src/block/helpers/toExternalHTML/BlockMathMLElement.tsx
  • packages/math-block/src/helpers/toExternalHTML/latexToMathMLElement.ts
  • packages/math-block/src/helpers/toExternalHTML/mathMLExportContent.ts
  • packages/math-block/src/inlineContent/createReactMathInlineContentSpec.test.tsx
  • packages/math-block/src/inlineContent/helpers/toExternalHTML/InlineMathMLElement.tsx

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.

@pkg-pr-new

pkg-pr-new Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@blocknote/ariakit

npm i https://pkg.pr.new/@blocknote/ariakit@3389

@blocknote/code-block

npm i https://pkg.pr.new/@blocknote/code-block@3389

@blocknote/core

npm i https://pkg.pr.new/@blocknote/core@3389

@blocknote/diagram-block

npm i https://pkg.pr.new/@blocknote/diagram-block@3389

@blocknote/mantine

npm i https://pkg.pr.new/@blocknote/mantine@3389

@blocknote/math-block

npm i https://pkg.pr.new/@blocknote/math-block@3389

@blocknote/react

npm i https://pkg.pr.new/@blocknote/react@3389

@blocknote/server-util

npm i https://pkg.pr.new/@blocknote/server-util@3389

@blocknote/shadcn

npm i https://pkg.pr.new/@blocknote/shadcn@3389

@blocknote/xl-ai

npm i https://pkg.pr.new/@blocknote/xl-ai@3389

@blocknote/xl-docx-exporter

npm i https://pkg.pr.new/@blocknote/xl-docx-exporter@3389

@blocknote/xl-email-exporter

npm i https://pkg.pr.new/@blocknote/xl-email-exporter@3389

@blocknote/xl-multi-column

npm i https://pkg.pr.new/@blocknote/xl-multi-column@3389

@blocknote/xl-odt-exporter

npm i https://pkg.pr.new/@blocknote/xl-odt-exporter@3389

@blocknote/xl-pdf-exporter

npm i https://pkg.pr.new/@blocknote/xl-pdf-exporter@3389

@blocknote/xl-typst-exporter

npm i https://pkg.pr.new/@blocknote/xl-typst-exporter@3389

commit: 1ce7278

@YousefED
YousefED marked this pull request as ready for review November 21, 2026 19:54
@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
PR Preview Action v1.8.2

QR code for preview link

🚀 View preview at
https://TypeCellOS.github.io/BlockNote/pr-preview/pr-3389/

Built to branch gh-pages at 2026-11-24 13:55 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@nperez0111
nperez0111 merged commit 54dbeab into main Nov 24, 2026
12 of 14 checks passed
@nperez0111
nperez0111 deleted the fix/math-invalid-formula-export branch November 24, 2026 13:48
YousefED added a commit that referenced this pull request Nov 25, 2026
Main has the new test layout (tests/src/integration, Node or browser tests,
setupEditor) and the Knip audit. Conflicts in 21 files:

- package.json: the devDependencies of main (knip, postcss), and the two
  of the lens (@vitest/coverage-v8, eslint-plugin-perfectionist).
- packages/xl-ai/package.json: the dependencies of main (no
  headers-polyfill), and the text-lens exports and the Yjs 14 peers of
  the lens.
- packages/xl-ai/vitestSetup.ts: the version of main (getTestOptionsHost
  works without a window too).
- vite.config.ts: the lint config of main, and the lens plugins and the
  two lens overrides. The lens override also has the rule of main that
  forbids an import of a test file.
- Table/block.ts: the colwidth helper of the lens, with the colspan limit
  of main (as in the merge of 21 November).
- defaultBlocks.ts: the imports of the two sides.
- createLinkWithCaption.ts: the version of the lens. Main has only the
  half without a caption (#3387); the lens also writes a figure for a
  caption.
- math-block (3 files): the version of main (#3389, the reviewed form of
  the lens commit 82b18c6). The three test files of that lens commit
  stay. Three of their cases expect an merror for a formula that KaTeX
  renders in part (x \badcommand y). Main keeps the MathML of KaTeX for
  such a formula, thus these three cases fail.
- BlockNoteEditor.test.ts (removed by main): the block ID tests of the
  lens move to BlockNoteEditor.node.test.ts (headless) and
  BlockNoteEditor.browser.test.ts (mounted), with setupEditor.
- DiffVersioningExtension.test.ts, utils.test.ts: the setupEditor form of
  main, with the tests of the lens in that form.
- v1BindingSnapshotDiff.test.ts: the version of main. The change of the
  lens (the second version is an edit that keeps the block ID) is now in
  v1BindingSnapshotDiff.fixture.ts.
- tests/src/unit/... (5 files): main moved them to tests/src/integration.
  The new markdownSyntaxText copy snapshots are at the new path. The
  toggle round-trip case of the lens is in htmlRoundTripCases. The two
  noCaption snapshots of main and of the lens are equal.
- pnpm-lock.yaml, playground/src/examples.gen.tsx: the files of main,
  then made again by pnpm install and vp run gen. The text-lens example
  has no ariakit and shadcn dependencies now (the template of main).
YousefED added a commit that referenced this pull request Nov 25, 2026
…wser tests

Main runs the tests of math-block in Node, and a test that needs a DOM is
a browser test. The three test files of the lens commit 82b18c6 export
and parse HTML. They are now X.browser.test.ts, without the jsdom
docblock. The tests do not change.

Three cases fail with the code of main (#3389): for a formula that KaTeX
renders in part (x \badcommand y), main keeps the MathML of KaTeX, and
these cases expect an merror element.
YousefED added a commit that referenced this pull request Nov 25, 2026
Main has the fix for a formula that does not compile (#3389) with its own
browser tests. The three test files of the lens branch were for the older
form of that fix, and three of their cases expect the older result. Without
them, math-block is equal to main.

This branch was successfully deployed

2 active deployments
Preview – blocknote-website — 2fb0468e Deployed Nov 24, 2026 by vercel[bot]
Preview – blocknote — 2fb0468e Deployed Nov 24, 2026 by vercel[bot]
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.

2 participants