New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
katex server side stem math rendering in HTML5 (PROTOTYPE) #3338
base: main
Are you sure you want to change the base?
Conversation
I haven't taken a deep look, but is this a better approach than https://github.com/jirutka/asciidoctor-katex? |
@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. |
Benchmarks I did a benchmark as follows:
Results:
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
main.rb
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). |
I'm not all that surprised by this because Ruby system integration is very good. You are going to get the best result by using the native calls rather than introducing layers in between. |
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. 🚀
As to whether this should go in core or be an extension, that's a great
(and tough) question. I really need to think hard about that one, but I
really do want to make this easy on users, so I will take that into
consideration. Regardless, it should be part of the Asciidoctor org).
Thanks for getting this moving! I really appreciate your work. 😁
… |
A quick progress report: At https://github.com/cirosantilli/cirosantilli.github.io/blob/fd11b321c5e4075509db2c4d52249c94d90040bd/katex.rb I have pushed the plugin as far as I can go without solving "possibly not easy upstream questions" mentioned in the TODO part of that file:
Besides those however, it the extension is already working pretty well, and if those points were implemented, we would have, I believe, the best HTML ath typesetting system created so far, opening the way to destroy LaTeX and world domination. Even without those points I think it is already good enough for me to publish it as a gem. |
WORKING EXTENSION WITH SOME TODOs: https://github.com/cirosantilli/asciidoctor-katex-2
er 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
a.adoc
test from this PR.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:
TODOs:
def convert
get aninline_katex
block yetstem: katex
not working yet to make it the default forstem:
throughout documentstem: katex
or if there is at least onekatex:
in document. LIkewise for mathjax.Bibliography: