Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
katex server side stem math rendering in HTML5 (PROTOTYPE) #3338
Related issue: #1735
Server side rendering is awesome as it is faster on the browser, so the page doesn't keep reflowing if you have a ton of formulas
This is now just a prototype, first install katex with:
then convert the
Outcome: the block katex works, inline not yet.
If people think that this is of interest, I am willing to clean it up into a proper version with some help, since this is relatively high interest to me. Otherwise I'll likely just extract into an extension.
You can test katex on the CLI with:
referenced this pull request
Jun 13, 2019
@mojavelinux ah thanks, I hadn't seen that one.
After a quick look, basically, the only thing they can be doing fundamentally significantly better is using https://github.com/glebm/katex-ruby which uses https://github.com/rails/execjs to call katex instead of popen3 as I do here (relates to the https://github.com/Shopify/schmooze point above, but katex ruby gem is even better)
My approach avoids adding a lot of dependencies to this project, their approach likely runs faster in a document with a ton of maths since it should not start a process for every math like I do here.
I will benchmark this on a huge test document vs the existing mathjax to see if I can observe a significant performance difference, if not I would recommend just starting with pipes due to simplicity.
I did a benchmark as follows:
So to my surprise, my naive pipe approach was about 4.5x faster!
The it is interesting to open all the output documents:
I also did a quick katex benchmark in isolation just to confirm that all the slowness is there:
and it takes about the same as the previous document rendering on node v10.15.1.
However, if I do:
then it is only 0.7 seconds, so all the slowness comes from katex startup time, which is what I thought the point of https://github.com/rails/execjs would be, maybe that just spawns multiple node commands?
OK, schmooze integration had good perf!!! https://github.com/Shopify/schmooze
Time: 0.5s! at schmooze (0.2.0)
For 1M equations: 124s.
OK, I can live with that, I would then recommend a Schmooze integration for now.
I had a quick look at execjs, but can't find easily how to do multiple calls on a single nodejs instance, maybe just to little docs. If I try:
it blows up, so
Tested on: Ubuntu 19.04, Lenovo ThinkPad P51 laptop with CPU: Intel Core i7-7820HQ CPU (4 cores / 8 threads), RAM: 2x Samsung M471A2K43BB1-CRC (2x 16GiB, 2400 Mbps), SSD: Samsung MZVLB512HAJQ-000L7 (512GB, 3,000 MB/s).
referenced this pull request
Jun 14, 2019
The main issue with this change is going to be security. Right now, Asciidoctor doesn't use any libraries (that the user doesn't supply) and doesn't make any system calls. This is a hard requirement for Asciidoctor working on GitHub.
We could tie this into the safe mode so that it only works in unsafe mode (to start). We will also probably need a very lightweight adapter / manager class for calling katex so that it can be replaced in different environments with a different strategy (perhaps using a service or something). It would probably be something along the lines of what we have for the syntax highlighter integrations.
Yes, this is definitely unsafe only stuff, I'm considering it for GitHub pages.
Although at this speed, I wouldn't even bother supporting it... it would make the dev cycle unbearable.
Shame, since server side math is kind of the holy grail of web maths, so sad.
Dan, I've updated the benchmark comment with new findings.
As expected KaTeX slowness is only due to startup: once loaded renders are fast, so it is a "perf bug" on asciidoctor-katex's integration / stack.
Then I tried schmooze instead of execjs, and I got good usable perf!
I understand your concerns about this feature, let me know if you think it is worth merging to master for unsafe only, or if I should just start with an extension.
The extension I think I already know how to do basically :-) Master merge a bit more involved but I will do with help, as this is math nirvana.
That's great news about the perf.…