wooorm / markdown-rs

CommonMark compliant markdown parser in Rust with ASTs and extensions
https://docs.rs/markdown/1.0.0-alpha.20/markdown/
MIT License
897 stars 48 forks source link

MathJax delimiters #124

Open ronnodas opened 4 weeks ago

ronnodas commented 4 weeks ago

It would be convenient if there was a compile option to have math delimited by \( \) and \[ \] (for inline and display respectively) instead of <code class="language-math math-*">. This is what MathJax uses by default for TeX input.

wooorm commented 4 weeks ago

a) if you want those delimiters, you can also actually write them, instead of using the $ math syntax? b) It’s not difficult for you to do that? Or to call mathjax yourself? c) I think it’s not good, what they do, in HTML there are elements, and semantically TeX is code?

ronnodas commented 4 weeks ago

a) Yes I could write those delimiters in the markdown (and in fact I would mildly prefer them over $'s because of the asymmetry) but the slashes require escaping, which makes it harder to compose/edit.

b) Possibly with a separate script, but instead of recreating what MathJax already does with a different set of delimiters it makes more sense to me pass it a thing it knows about. But maybe I don't understand what you want me to do exactly. Currently I'm using replace_all on the output of to_html, which is indeed not difficult but feels silly.

c) I agree with this in principle, and ideally I would want to tell MathJax to use the code tag + math-* class as delimiters. But that seems not possible: it specifically disallows tags in delimiters for apparently good reasons.

wooorm commented 4 weeks ago

but the slashes require escaping, which makes it harder to compose/edit.

Right, good point!

Currently I'm using replace_all on the output of to_html, which is indeed not difficult but feels silly.

I don’t think that’s too silly. But, you are including mathjax in the browser right? So why not include browser code to a) turn <code class="language-math"> into wrappers, b) call the mathjax API? This seems relating to your c) response too. It’s sad that the mathjax docs are not done, but it should be possible? I also use the API in Node.js or browsers: https://github.com/remarkjs/remark-math/blob/main/packages/rehype-mathjax.

ronnodas commented 4 weeks ago

I mean passing <code ...> to mathjax as in https://docs.mathjax.org/en/latest/options/input/tex.html#tex-inlinemath does not work.

Note that the delimiters can’t look like HTML tags (i.e., can’t include the less-than sign), as these would be turned into tags by the browser before MathJax has the chance to run. You can only include text, not tags, as your math delimiters.

Using browser code to call the mathjax api should work and I will consider the example you linked, but it seems better to me to change the generated html than to reimplement something mathjax is already doing. The only reason it feels silly to use replace_all is because it's (mildly) reparsing html that is freshly compiled from much more structured data and it would be more sensible to compile it the "way I want it" directly.

If your main objection to this potential feature is the loss of html semantics, would you be fine with keeping the tags but also adding the \( etc inside them? That is, generate something like <span ...> \( a = b \) </span>? I changed the code to span since MathJax does not process anything specifically inside code tags and this behavior seems to be undocumented, so not sure how easy it is to change it. I'm also not asking for this to be the default, but for an added compile option.

wooorm commented 4 weeks ago

You can still write several lines of JavaScript code in a browser to find code elements, and change them, by changing <code> to \(\). Essentially what you are doing with replace_all, but then in a browser.

My main objections are that: a) that alternative sounds viable (use the DOM, which is basically an AST, to generate what you want), b) different HTML output, as an alternative to regex, is more something for ASTs and plugins (https://github.com/wooorm/markdown-rs/issues/32) c) for a rust project, it seems more sensical to go in the direction of using rust to do syntax highlighting and math rendering instead of javascript