MathFmt
I like ASCIIMath - it's a super simple language, but it's far easier to write versus TeX formulas, but is still wildly expressive. For example, I find it way easier to type this:
int dx/sqrt(a^2 + x^x) = ln |{x+sqrt(a^2 + x^2)}/a| + C
To get this:
What I didn't like about the ASCIIMath library was the tooling - the official docs give you 2 paths forward:
- Use the official library.
- Pros: Fairly lightweight.
- Cons: The code is rough and difficult to understand. Also, you're expected to copy the source and host the file yourself, and customizing settings within the library requires editing the actual source code. Also, the library makes heavy use of the browser-only MathML element classes, which makes SSR tricky.
- Use the MathJax library.
- Pros: This is the recommended approach. Well supported, and everyone already uses MathJax.
- Cons: MathJax is a pretty heavy library - sometimes pulling in close to a MiB of JS files just to render the formula. It's also fairly slow. It's also a pain to configure. I also am not a huge fan of the output - the equations look pretty, but MathJax insists on adding special context menus, and about buttons, and help dialogs, and keyboard shortcuts, and.... GAH! I just want the formula!
I didn't like either of these paths forward, so I created my own library. The goals:
- Support 100% of the ASCIIMath syntax. (Other AM libraries would trim the feature set.)
- Keep things as terse and minimal as possible.
- Try to make things as speedy as I can, without making too much of a mess of the codebase.
- Only support MathML output for now, but leave things open for other renderers in the future.
- The code should work in both browsers and in servers, so SSR is possible.
The results:
- The library indeed supports all of the ASCIIMath syntax. (And then some - I extended it in a few places.)
- The library (minified) only weighs 15 KiB currently. Compare that to 40-50KiB for the official library, or up to a MiB for MathJax.
- The library is fast - around 4-6x faster than the official AM library, and like 20x faster than MathJax.
So, how did I get there?
