commonmark / commonmark-java

Java library for parsing and rendering CommonMark (Markdown)
BSD 2-Clause "Simplified" License
2.29k stars 288 forks source link

Escape characters #136

Closed noties closed 6 years ago

noties commented 6 years ago

Hello,

I've been trying to implement a simple LaTeX formula render in my Markwon library and found out that commonmark-java modifies input. So, I wanted to ask if it's possible to not escape input when in some specific block/node?

I'm using DelimiterProcessor now.

Input:

\begin{array}{l}\forall\varepsilon\in\mathbb{R}_+^*\ \exists\eta>0\ |x-x_0|\leq\eta\Longrightarrow|f(x)-f(x_0)|\leq\varepsilon\\\det\begin{bmatrix}a_{11}&a_{12}&\cdots&a_{1n}\\a_{21}&\ddots&&\vdots\\\vdots&&\ddots&\vdots\\a_{n1}&\cdots&\cdots&a_{nn}\end{bmatrix}\overset{\mathrm{def}}{=}\sum_{\sigma\in\mathfrak{S}_n}\varepsilon(\sigma)\prod_{k=1}^n a_{k\sigma(k)}\\\sideset{_\alpha^\beta}{_\gamma^\delta}{\begin{pmatrix}a&b\\c&d\end{pmatrix}}\\\int_0^\infty{x^{2n} e^{-a x^2}\,dx} = \frac{2n-1}{2a} \int_0^\infty{x^{2(n-1)} e^{-a x^2}\,dx} = \frac{(2n-1)!!}{2^{n+1}} \sqrt{\frac{\pi}{a^{2n+1}}}\\\int_a^b{f(x)\,dx} = (b - a) \sum\limits_{n = 1}^\infty  {\sum\limits_{m = 1}^{2^n  - 1} {\left( { - 1} \right)^{m + 1} } } 2^{ - n} f(a + m\left( {b - a} \right)2^{-n} )\\\int_{-\pi}^{\pi} \sin(\alpha x) \sin^n(\beta x) dx = \textstyle{\left \{ \begin{array}{cc} (-1)^{(n+1)/2} (-1)^m \frac{2 \pi}{2^n} \binom{n}{m} & n \mbox{ odd},\ \alpha = \beta (2m-n) \\ 0 & \mbox{otherwise} \\ \end{array} \right .}\\L = \int_a^b \sqrt{ \left|\sum_{i,j=1}^ng_{ij}(\gamma(t))\left(\frac{d}{dt}x^i\circ\gamma(t)\right)\left(\frac{d}{dt}x^j\circ\gamma(t)\right)\right|}\,dt\\\begin{array}{rl} s &= \int_a^b\left\|\frac{d}{dt}\vec{r}\,(u(t),v(t))\right\|\,dt \\ &= \int_a^b \sqrt{u'(t)^2\,\vec{r}_u\cdot\vec{r}_u + 2u'(t)v'(t)\, \vec{r}_u\cdot\vec{r}_v+ v'(t)^2\,\vec{r}_v\cdot\vec{r}_v}\,\,\, dt. \end{array}\\\end{array}

Output:

\begin{array}{l}\forall\varepsilon\in\mathbb{R}+^*\ \exists\eta>0\ |x-x_0|\leq\eta\Longrightarrow|f(x)-f(x_0)|\leq\varepsilon\\det\begin{bmatrix}a{11}&a_{12}&\cdots&a_{1n}\a_{21}&\ddots&&\vdots\\vdots&&\ddots&\vdots\a_{n1}&\cdots&\cdots&a_{nn}\end{bmatrix}\overset{\mathrm{def}}{=}\sum_{\sigma\in\mathfrak{S}n}\varepsilon(\sigma)\prod{k=1}^n a_{k\sigma(k)}\\sideset{\alpha^\beta}{\gamma^\delta}{\begin{pmatrix}a&b\c&d\end{pmatrix}}\\int_0^\infty{x^{2n} e^{-a x^2},dx} = \frac{2n-1}{2a} \int_0^\infty{x^{2(n-1)} e^{-a x^2},dx} = \frac{(2n-1)!!}{2^{n+1}} \sqrt{\frac{\pi}{a^{2n+1}}}\\int_a^b{f(x),dx} = (b - a) \sum\limits_{n = 1}^\infty  {\sum\limits_{m = 1}^{2^n  - 1} {\left( { - 1} \right)^{m + 1} } } 2^{ - n} f(a + m\left( {b - a} \right)2^{-n} )\\int_{-\pi}^{\pi} \sin(\alpha x) \sin^n(\beta x) dx = \textstyle{\left { \begin{array}{cc} (-1)^{(n+1)/2} (-1)^m \frac{2 \pi}{2^n} \binom{n}{m} & n \mbox{ odd},\ \alpha = \beta (2m-n) \ 0 & \mbox{otherwise} \ \end{array} \right .}\L = \int_a^b \sqrt{ \left|\sum_{i,j=1}^ng_{ij}(\gamma(t))\left(\frac{d}{dt}x^i\circ\gamma(t)\right)\left(\frac{d}{dt}x^j\circ\gamma(t)\right)\right|},dt\\begin{array}{rl} s &= \int_a^b\left|\frac{d}{dt}\vec{r},(u(t),v(t))\right|,dt \ &= \int_a^b \sqrt{u'(t)^2,\vec{r}_u\cdot\vec{r}_u + 2u'(t)v'(t), \vec{r}_u\cdot\vec{r}_v+ v'(t)^2,\vec{r}_v\cdot\vec{r}_v},,, dt. \end{array}\\end{array}

What surprises me is removed _ char, which doesn't sound that dangerous...

Anyway, I would really love to get some feedback about this. Can we specify when input should not be processed but kept as-is?

Thanks!

noties commented 6 years ago

Hey!

Turns out my problem was choosing delimiter processor instead of using a block parser. So, now everything works.

*And removed _ char is not a surprise as inline parser thinks that it found an emphasis node.

Thanks for the library!

robinst commented 6 years ago

👍 yeah adding a custom block parser is the right thing for this!