Repository navigation
feat(markdown): $…$ and $$…$$ TeX math drawn as equations, kept as written when saved - #40
Open
pierreneter wants to merge 1 commit into
Open
pierreneter wants to merge 1 commit into
pierreneter wants to merge 1 commit into
Conversation
…itten when saved A zero-dependency TeX → MathML converter (src/core/math) draws the formulas of a Markdown file with the browser's own MathML: selectable, read by screen readers, printed and exported. Each formula is one object of the document (the equation model Word files already use), holding its TeX and delimiters, so an edit around it saves the formula back byte for byte; currency, \$ and code are left alone (pandoc's rules). A command it does not know, or TeX it cannot read, shows its source and never breaks the page. Download as Word gives real Office Math equations carrying the TeX in ignorable attributes; copy / paste keeps formulas (MathML holding TeX on the clipboard). @p10node/markdown renders math too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Fixes #33.
Markdown math in the viewer:
$…$and display$$…$$render as native MathML.There are no new dependencies: the TeX → MathML converter is our own, DOM-free code.
Design
Same model as Word equations. A formula is the existing atomic
{ t: 'ref' }inline, the one OMML equations already use, with a newmath: { tex, delim }field. Caret movement, delete, undo, collaboration and write-back by source run work as they do for equations.DOCX round trip.
m:oMath, orm:oMathParafor display math.oe:texandoe:delimattributes in our own namespace. The root declares that namespace asmc:Ignorable, so Word can still open the file. , so whitespace inside a$$block is kept byte for byte.Recognition follows pandoc's rules (
src/core/math/scan.ts):$must be followed by a non-space;$must be preceded by a non-space and not followed by a digit, so currency text such as "costs $5 and $10" is not math;\$is a dollar sign;$$block can span lines, but not a blank line.\(…\)and\[…\]are not supported: in Markdown they are escaped brackets. This is documented.Fallback. An unknown command shows as
<merror>while the rest of the formula renders. Malformed TeX shows its source as a subtlemerror, with the reason in a tooltip. Work is bounded at 20,000 characters, 40 levels of nesting and 50,000 elements. The converter never throws: a fuzz test runs 3,000 random inputs plus a list of broken ones.Accessibility, print and export. The TeX is kept as
<annotation encoding="application/x-tex">. Formulas appear in print and PDF, in the HTML export and on the clipboard as MathML, and in plain-text export as TeX with its delimiters.Pasting. Pasted MathML that carries a TeX annotation (from our own clipboard, KaTeX or Wikipedia) becomes a formula.
packages/markdownrenders math with the same converter, behind a newmathoption that is on by default. Its output stays safe: every TeX string is escaped, and only a fixed allow-list of MathML elements and attributes is emitted, which a test checks. The package grows from about 39 KB to 53 KB gzip.Main files
src/core/math/(new):tex.ts: the converter;symbols.ts: symbol tables, and the math alphabets mapped to Unicode;scan.ts: recognition in Markdown;omml.ts: MathML → OMML.convert.ts,html.ts, the DOCXparser,spec,writerandtypes,plainmerge.ts,render.tsx(MathView),styles.css,file/text.ts,DocxEditor.copy.README.md, a new "Math in Markdown" section indocs/features.md(what is supported, what falls back, what the DOCX download contains, limitations),docs/api.md,docs/development.md,packages/markdown/README.md.convert.ts: the lazy list-continuation.replace()now uses a function, so a$&in text is no longer read as a replacement pattern.Tests
test/math.test.ts(31 tests): the MathML structure for each construct family, errors, limits, the fuzz test, and OMML.test/markdown-math.test.ts(21 tests):$;e2e/markdown-math.spec.ts(6 tests in Chromium):Results
In the Linux test VM with Bun 1.4.4:
bun testmarkdown-mathviewerdocxfile-menusecuritysdkdocx-qa2bun run buildbun run build:packagesAfter rebasing onto
af19290, which only changespackage.json, I re-ran typecheck and the math unit tests.Limitations
These are documented in
docs/features.md.```mathblocks and$`…`$;\(…\)and\[…\];\newcommand),\href,\ce.$$block inside a list item or a quote is not recognised. One-line formulas work there.\$escape becomes$;*$x$*) is dropped;$HOME/$USERis read as math, as pandoc does.\boxedand\canceldraw nothing;A merge conflict with the syntax-highlighting branch (#32) is likely in
convert.tsmarkdownToHtmland intext.tsmdInline.🤖 Generated with Claude Code