1. Radomir Dopieralski
  2. hatta
  3. Issues
Issue #87 resolved

Support mathjax

zarf
created an issue

It would be nice to have mathjax support integrated in hatta. Here is a link to the necessary code

Comments (11)

  1. Radomir Dopieralski repo owner

    I have several problems with this code:

    • it breaks the existing math formula handling,
    • it uses a single character for markup, which is against the ideas of WikiCreole,
    • it generates invalid HTML, as it doesn't escape &, < and > -- that also makes the whole wiki vulnerable to cross-site-scripting attacks,
    • it introduces more than one way of inserting math formulas, without any real difference between them, which is also agains the ideas of WikiCreole,
    • the display math can be opened with {{{$$}}} and closed with {{{]}}} and vice versa,
    • the class names use underscores, which is not recommended,
    • and finally, this is not the complete code needed to make it work -- the formulas just show up as entered in the source, I suppose you need to include and configure MathJax for it to work, but I have no idea how and their documentation is rather lengthy and opaque.

    So instead of using your code, I did the following:

    • Added a display math markup, using {{{$$}}} alone on a line.
    • Added an option for disabling formula processing, by setting {{{math_url}}} to an empty string.
    • Added an option for disabling formula processing, and inserting the {{{$}}} signs, by setting {{{math_url}}} to {{{$}}}.
    • Added command-line options {{{-M}}} and {{{--math-url}}} for setting {{{math_url}}}, as it was missing.

    I hope that this helps, and thank you for sharing the code. Please let me know if you would like to change or add something -- I'm working in the dark here, as I didn't get MathJax to work.

  2. zarf reporter

    Hi TheSheep, thanks for doing this. I'm not sure how much of my "monkeypatch" is, after your changes, still necessary to enable mathjax handling, but let me quickly comment on the good points you raise.

    • it breaks the existing math formula handling,

    Sorry that was unintentional (I think!); ideally there should of course be an option such as the one you added, that would both disable the existing formula handling and enable mathjax. (I don't think there is a point in enabling both at the same time.)

    • it uses a single character for markup, which is against the ideas of WikiCreole,

    • it introduces more than one way of inserting math formulas, without any real difference between them, which is also agains the ideas of WikiCreole,

    That seems unavoidable if one wants to use mathjax at all, whose main markup is $ $, with option to use \( \) instead.

    • it generates invalid HTML, as it doesn't escape &, < and > -- that also makes the whole wiki vulnerable to cross-site-scripting attacks,

    I'm not sure that's true -- if you look at the html source of this page you will see that the ampersands present in the wiki source have been replaced by &amp;

    • the display math can be opened with {{{$$}}} and closed with {{{]}}} and vice versa,

    I believe mathjax supports that, but we could enforce more consistency when used in hatta.

    • the class names use underscores, which is not recommended,

    Sorry! If you let me know of better names I'll be happy to adopt them.

    • and finally, this is not the complete code needed to make it work -- the formulas just show up as entered in the source, I suppose you need to include and configure MathJax for it to work, but I have no idea how and their documentation is rather lengthy and opaque.

    You're right, I forgot to mention my changes to base.html (see lines blamed on fz@22 and fz@55). Other than that, enabling mathjax is quite simple as explained here:

    $ git clone git://github.com/mathjax/MathJax.git ~/.local/repo/MathJax
    $ ln -s ~/.local/repo/MathJax ~/Sites/example.com/public/static/mathjax
    
  3. Radomir Dopieralski repo owner

    Thank you.

    I have now verified that after adding the mathjax scripts to the base.html template and setting math_url to "$" the formulas are displayed properly. You may still need some of your monkey-patching to have the alternate syntax (\(\) and \[\]) working.

    By the way, you don't need to put the style in the code -- you can just create a page style.css with your new style, and it will be used instead.

  4. Radomir Dopieralski repo owner
    • changed status to open

    Actually, it may be good to include MathJax with Hatta and use it by default, but to do that I need to work around two issues:

    • Only process the text inside the actual equations, not any $$ that may appear anywhere on the website. I think I can do that by disabling the initial parsing and only parsing the selected elements with my own JavaScript.
    • Work around the fact that MathJax is a whole bunch of different JavaScript files, some of them in subdirectories, and not easily served by Hatta's flat page structure.
  5. Radomir Dopieralski repo owner

    OK, now Hatta will use MathJax automatically when you set math_url=mathjax. It will only process the inline-math and display-math classes, and the code is taken from their CDN. If you want to use your local code, edit base.html to change the URL.

  6. Log in to comment