elm-explorations/markdown/1.0.0

Markdown

A library for markdown parsing. This is just an Elm API built on top of the marked project which focuses on speed.

Parsing Markdown

toHtml : List (Attribute msg) -> String -> Html msg

Turn a markdown string into an HTML element, using the defaultOptions.

recipe : Html msg
recipe =
   Markdown.toHtml [class "recipe"] """


# Apple Pie Recipe


First, invent the universe. Then bake an apple pie.


"""

Parsing with Custom Options

type alias Options =
{ githubFlavored :
Maybe
{ tables : Bool
, breaks : Bool
}
, defaultHighlighting : Maybe String
, sanitize : Bool
, smartypants : Bool
}

Some parser options so you can tweak things for your particular case.

* githubFlavored — overall reasonable improvements on the original markdown parser as described here. This includes stuff like fenced code blocks. There are some odd parts though, such as tables and a setting to turn all newlines into newlines in the resulting output, so there are settings to turn those on or off based on your preference.

* defaultHighlighting — a default language to use for code blocks that do not have a language tag. So setting this to Just "elm" will treat all unlabeled code blocks as Elm code. (This relies on highlight.js as explained in the README here.)

* sanitize — this determines if all HTML should be escaped. If you are parsing user markdown or user input can somehow reach the markdown parser, you should almost certainly turn on sanitation. If it is just you writing markdown, turning sanitation off is a nice way to do some HTML tricks if it is needed.

* smartypants — This will automatically upgrade quotes to the prettier versions and turn dashes into em dashes or en dashes

defaultOptions : Options

The Options used by the toElement and toHtml functions.

{ githubFlavored = Just { tables = False, breaks = False }
, defaultHighlighting = Nothing
, sanitize = True
, smartypants = False
}
toHtmlWith : Options -> List (Attribute msg) -> String -> Html msg

Maybe you want to allow tables in your markdown?

myOptions : Options
myOptions =
    { githubFlavored = Just { tables = True, breaks = False }
    , defaultHighlighting = Nothing
    , sanitize = True
    , smartypants = False
    }


toMarkdown : String -> Html
toMarkdown userInput =
    Markdown.toHtmlWith myOptions [] userInput