I wonder which markdown library is used in this stackexchange site? In particular I like how it allows LaTeX to be used for Maths. There are many libraries like marked. Many that knows how to render GitHub Markdown even, or some extended Markdown syntax. But which in particular does things for maths.
3 Answers
SE uses two Markdown processors:
- server-side Markdownsharp, in C#
- client-side previewer Pagedown, in JavaScript. (Incidentally, I mentioned its history yesterday).
As others mentioned, math rendering is done by MathJax. But the question
how [Markdown library] allows LaTeX to be used for Maths
is still nontrivial. By itself, Markdown processor does not know anything about MathJax. If it was called after MathJax, it would remove all <span>
tags with formulas, as it removes most HTML tags. If it was called before MathJax, it would destroy some TeX formulas by replacing, e.g., the valid formula $f *g* h$
with $f <em>g</em> h$
.
The solution implemented on StackExchange is to preprocess user input by a third script (about 130 lines, named mathjax-editing.js
) which escapes the text within $...$
or $$...$$
from Markdown. This way, formulas go intact through Markdown processing, and eventually get handled by MathJax. For example, $f *g* h$ renders correctly.
For various reasons, LaTeX formula delimiters \( \)
and \[ \]
are not supported by this setup. The most one can do is to type \\[ \\]
to escape backslashes manually, but this leaves the content of the formula exposed to Markdown processor: for example,
\\[ f *g* h \\]
becomes \[ f g h \]. So much for good LaTeX practice.
-
$\begingroup$ Oh I was wondering if there was some parser that could handle Markdown+MathJax, but looks like its still done separately $\endgroup$ May 21, 2013 at 13:38
-
$\begingroup$ Why would they need a server side processor? They can just store the markdown. Can't they? @75064 $\endgroup$ Jun 6, 2018 at 20:56
You might also be interested to know that you can right-click on the formatted mathematics to bring up a menu: