Message Boards Message Boards

Markdown to Mathematica converter

POSTED BY: Anton Antonov
11 Replies
POSTED BY: Anton Antonov

A showcase

The notebook in the Community post "Workflows with LLM functions (in Raku)" was obtained by converting the Markdown file "Workflows with LLM functions" using the Raku package "Markdown::Grammar" discussed in this post.

That Raku-centric notebook was translated / rewritten into the corresponding WL-centic notebook "Workflows with LLM functions (in WL)".

POSTED BY: Anton Antonov

The blog post "Conversion and evaluation of Raku files" proclaims a presentation made with Mathematica that shows conversion of a Markdown file into a Mathematica notebook and a Jupyter notebook.

Ideally, the program jupytext will be extended to deal with Mathematica / WL notebooks.

Here is the presentation at YouTube:

enter image description here

POSTED BY: Anton Antonov

An addendum to the related work table, Connor's package md2nb

POSTED BY: Faizon Zaman

I did not know about Connor's package "md2nb" -- thanks for pointing that out!

(If I knew about it, most likely I would have not written my "Markdown::Grammar" package.)

POSTED BY: Anton Antonov

Nice, glad to see this! If I wanted to convert the markdown doc into a WL Tree, is it as simple as passing the generated Notebook object to ExpressionTree? Or would I need to do something with the grammar directly?

POSTED BY: Faizon Zaman

Nice, glad to see this!

Thanks!

If I wanted to convert the markdown doc into a WL Tree, is it as simple as passing the generated Notebook object to ExpressionTree? Or would I need to do something with the grammar directly?

I am not sure what you are asking -- I think you are interested in some form of the Abstract Syntax Tree (AST) of the Markdown document.

Of course, we can treat the obtained notebook expression as AST in the way you describe, see the attached screenshot.

If you have Raku installed then you can obtain the AST of the markdown document with the package command md-parse.

enter image description here

POSTED BY: Anton Antonov

Ah sorry, not a tree of the Notebook expression. I’m thinking at the level of the markdown elements themselves, like:

Tree[
    <|”H1” -> “My List”|>,
    Tree[
        <|
      ”p” -> “Here is my unordered list”,
      “ul” -> Tree[{“here is one item”, “Here is another”}]
        |>
      ]
    ]

POSTED BY: Faizon Zaman

For this, I assume I would need to then parse the AST?

Yes and no. The AST obtained from Raku can be converted into a hash (of nested hashes), and that is easily converted into JSON. This JSON output can be imported in Mathematica and then converted into the tree form you showed. We can say that that is parsing, but it can be done with none-parsing operations. (E.g. replacement rules.)

In all fairness, if that form is of (sufficient) interest, then we can make:

  1. A dedicated notebook expression converter
  2. Another interpreter in the Raku package
  3. Another Markdown parser-interpreter in Mathematica/WL.

I think option 1 is best. (Except, if you really want to use the Markdown file as a "single origin truth.") Also, for option 1, Kuba's package "M2MD" might be a good start.

POSTED BY: Anton Antonov

Here is another video showing how to convert Jupyter notebooks into Mathematica/WL notebooks:

enter image description here

POSTED BY: Anton Antonov

Warning: Installation of Raku on macOS (using rakudo.org) might be not that easy. I advice using rakubrew.

POSTED BY: Anton Antonov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract