jxxcarlson / elm-markdown

Pure elm markdown parser with optional extensions for math, poetry, etc.
https://markdown.minilatex.app/
BSD 3-Clause "New" or "Revised" License
29 stars 5 forks source link

jxxcarlson/elm-markdown

Elm package

The aim of this Markdown library is to provide a pure Elm implementation of Markdown which offers a small set of optional extensions:

How to use it

For simple applications, follow the code in ./example/src, e.g,

view model =
    Html.div []
        [ Markdown.Render.toHtml ExtendedMath sourceText
            |> Html.map MarkdownMsg
        ]

This example shows how to include mathematical formulas and SVG figures. To run the example, do this:

$ cd example
$ sh make.sh

Then open public/index.html with your browser.

Demos

See the ./Demos folder for examples. Best to look at ./Demos/simplest first.
The other examples include extra bells, whistles and optimizations that are useful for interactive editing environments and documents with a lot of mathematics.

Links to live demos:

This package is still evolving. I regret publishing so many updates, but I am using it in several apps, and this is the only way I know how to encapsulate the complexity, work with the CI build systems, and keep my sanity

Installing a Demo

For example:

$ cd to ./Editors/fancy

$ vr make

NOTES

Style

The style used by the library is entirely determined by the definitions of the CSS classes that you refer to in your index.html. The ones used for the demo apps are found in ./public/assets/style.css when you are in the folder for one of the Editor examples. You can easily reconfigure the CSS to satsify your own esthetics.

Markdown extensions

Here are the main additions:

SVG

You can add SVG images like this:

@@svg
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="blue" stroke-width="3" fill="cyan" />
</svg>

Images

The usual ![My favorite image](imageUrl) does the usual thing, with the image scaled to 100% of the width. You can also say ![My favorite image::left](imageUrl) or ![My favorite image::right](imageUrl) to float the image left or right at 40% width. The widths are defined in style.css.

Advanced Usage: AST

If you wish to write your own renderer, or do other fancy things, you will want to produce and manpulate the AST:

Markdown.Parse.toMDBlockTree : Version 
       -> Option -> Document -> Tree MDBlock

where Version is an integer and Document is a type alias for String.
This is also useful if you wish to transform the abstract syntax tree before rendering it. The Version parameter may be set to zero if you do not have to worry about updated thd ids of rendered elements in an interactive editing environment.

Editor

The fancy demo app now uses pure Elm text editor. It is very much a work in progress.

Bugs and whatnot

Please write me at jxxcarlson@gmail.com or post an issue on the Github repository regarding bugs or anything else. I will steer this library towards the Commonmark spec to the greatest extent possible by the method of successive approximations

Recent Changes

Thanks

Thanks to Folkert de Vries and Luke Westby. A shout-out to Folkert for an optimiztaion of the pure text rendering (10 x speedup). A thankyou to Anton-4 for significant code cleanup and adding nolang support for the syntax highlighter.