Message Boards Message Boards

Markdown to Mathematica converter

Posted 2 years ago

POSTED BY: Anton Antonov
11 Replies

Here is a recent application of the converter related to my "LLM aids for processing of the first Carlson-Putin interview" series of posts.

  1. Make the Raku Jupyter notebook
  2. Convert the Jupyter notebook into Markdown
    • Using Jupyter's built-in converter
  3. Publish the Markdown version to WordPress
  4. Convert the Markdown file into a Mathematica notebook
  5. Publish that to Wolfram Community
    • It was deleted by moderators, because it does not feature Wolfram Language (WL)
  6. Make the corresponding Mathematica notebook using WL functions
  7. Publish to Wolfram Community
  8. Make the Russian version with the Russian transcript
  9. Publish to Wolfram Community
    • It was deleted by moderators, because it is not in English
  10. Convert the Mathematica notebook to Markdown
    • Using Kuba Podkalicki's M2MD
  11. Publish to WordPress
  12. Convert the Markdown file to Jupyter
  13. Re-make the workflows using Raku

Here is the corresponding Mermaid-JS diagram (using the paclet MermaidJS or the function MermaidInk):

ResourceFunction["MermaidInk"]["
graph TD
  A[Make the Raku Jupyter notebook] --> B[Convert the Jupyter notebook into Markdown]
  B --> C[Publish to WordPress]
  C --> D[Convert the Markdown file into a Mathematica notebook]
  D --> E[Publish that to Wolfram Community]
  E --> F[Make the corresponding Mathematica notebook using WL functions]
  F --> G[Publish to Wolfram Community]
  G --> H[Make the Russian version with the Russian transcript]
  H --> I[Publish to Wolfram Community]
  I --> J[Convert the Mathematica notebook to Markdown]
  J --> K[Publish to WordPress]
  K --> L[Convert the Markdown file to Jupyter]
  L --> M[Re-make the workflows using Raku]
  C -.-> WordPress{{Word Press}}
  K -.-> WordPress
  E -.-> |Deleted:<br>features Raku| WolframCom{{Wolfram Community}}
  G -.-> WolframCom
  I -.-> |\"Deleted:<br>not in English\"|WolframCom
  D -.-> MG[[Markdown::Grammar]]
  B -.-> Ju{{Jupyter}}
  L -.-> jupytext[[jupytext]]
  J -.-> M2MD[[M2MD]]
  E -.-> RakuMode[[RakuMode]]
"]

enter image description here

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

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

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

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

POSTED BY: Faizon Zaman

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

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

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

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