# Math Markdown Parser

**URL:** <https://discourse.elm-lang.org/t/math-markdown-parser/4104>\
**Category:** Request Feedback\
**Created:** [August 6, 2019, 4:10pm UTC](https://discourse.elm-lang.org/t/math-markdown-parser/4104 "2019-08-06T16:10:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jxxcarlson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jxxcarlson/32/866_2.png) [@jxxcarlson](https://discourse.elm-lang.org/u/jxxcarlson)\
**Post date:** [August 6, 2019, 4:10pm UTC](https://discourse.elm-lang.org/t/math-markdown-parser/4104/1 "2019-08-06T16:10:20Z")

</div>

Hi all, I’ve written an experimental Markdown parser that will also handle TeX/LaTeX style math formulas. Here is a [demo app](https://markdown.minilatex.app/), with the code at [jxxcarlson/math-markdown](https://package.elm-lang.org/packages/jxxcarlson/math-markdown/latest/).

I’d be very interested in feedback, especially regarding Markdown text that is not properly handled. Markdown has a quite irregular syntax, and I’m not confident that I have handled it sufficiently well.

The idea with this is to have a convenient way of writing lightweight math articles – short class notes and assignments, for example – where the full power of LaTeX is not needed.

The parser is pure Elm, and can be used without the math part. See the two examples in the [source code](https://github.com/jxxcarlson/math-markdown): `./app-math` and `./app-plain`. In the former, the `index.html` file calls on `MathJax.js` via a custom element (thanks @luke !); in the latter, then `index.html` file only defines some CSS styles and calls on highlight.js for syntax highlighting

---

<div class="post-metadata">

**Author:** ![brian](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/brian/32/2180_2.png) [@brian](https://discourse.elm-lang.org/u/brian)\
**Post date:** [August 6, 2019, 10:11pm UTC](https://discourse.elm-lang.org/t/math-markdown-parser/4104/2 "2019-08-06T22:11:25Z")

</div>

This looks wonderful!

What would you think about approaching this slightly differently? What if the math and poetry notations were plugins to a general Markdown parser/renderer? You’ve already done the hard part in writing the parser, and you already have both an inline and block extension that you know you want!

Oh, and what version of markdown does this use other than the extensions? Commonmark? `markdown.pl`?

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jxxcarlson/32/866_2.png) [@jxxcarlson](https://discourse.elm-lang.org/u/jxxcarlson)\
**Post date:** [August 7, 2019, 7:06am UTC](https://discourse.elm-lang.org/t/math-markdown-parser/4104/3 "2019-08-07T07:06:43Z")

</div>

Hi Brian, thanks so much for the comments. I’ve been playing around with the your plugin suggestion, which I like very much. The approach I’ve taken for the moment is to add `type Option = OMath | OVerbatim | OPoetry` and then add an `option : Option` parameter to the functions that need it. The problem I am struggling with at the moment is that `option` has to be threaded through the entire parser module and a couple of others as well. Lots of functions. Is there a better approach?

I tried to follow [Commonmark](https://commonmark.org/), but I must say that Markdown is such a hairy beast that I have only made a first step towards compliance. More work needed.

I’ve found some interesting reading on the subject (see below), and am going to cogitate more on this before I try to adhere better to the standards. I do have tests, but need many more

[Beyond Markdown: MacFarlane](https://johnmacfarlane.net/beyond-markdown.html)

[Coding Horror on Standard Markdown](https://blog.codinghorror.com/standard-flavored-markdown/)

[Standard Markdown](https://standardmarkdown.com/)

---

<div class="post-metadata">

**Author:** ![brian](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/brian/32/2180_2.png) [@brian](https://discourse.elm-lang.org/u/brian)\
**Post date:** [August 7, 2019, 10:24am UTC](https://discourse.elm-lang.org/t/math-markdown-parser/4104/4 "2019-08-07T10:24:38Z")

</div>

> [@jxxcarlson](#):
>
> The problem I am struggling with at the moment is that `option` has to be threaded through the entire parser module and a couple of others as well. Lots of functions. Is there a better approach?

nope, that sounds about right 😅. I can think of a couple variations:

1. pass in a `Parser a` instead, which can be like `oneOf [math, poetry]`.
2. return an AST instead: `Markdown`, where members are like `Paragraph Inline | List Inline (List Inline)`, et cetera. Callers perform further parsing on the result.
3. do both (my fav) and make `parse : Parser a -> String -> Markdown a`. Lets you render using things that aren’t `elm/html` too. 🙂

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jxxcarlson/32/866_2.png) [@jxxcarlson](https://discourse.elm-lang.org/u/jxxcarlson)\
**Post date:** [August 7, 2019, 11:37am UTC](https://discourse.elm-lang.org/t/math-markdown-parser/4104/5 "2019-08-07T11:37:39Z")

</div>

Thanks! I think I will go with something like your option (3). I realized while doing errands to day that I can render the AST to LaTeX which believe it or not has advantages, e.g., ship it out to make PDF.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [August 17, 2019, 11:37am UTC](https://discourse.elm-lang.org/t/math-markdown-parser/4104/6 "2019-08-17T11:37:41Z")

</div>

Closed on August 17, 2019, 11:37am UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
