evancz/elm-markdown/1.1.4

NOTE: this package is not compatible with Elm 0.19.1

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

toElement : String -> Element

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

intro : Element
intro =
    Markdown.toElement """


# Time Travel Made Easy


Elm Reactor grew out of my internship working on Elm at Prezi this summer...


"""
toHtml : String -> Html

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

bodyParagraph : Html
bodyParagraph =
    Markdown.toHtml """


# Changing History


In addition to time travel, Elm Reactor lets you change history...


"""

Parsing with Custom Options

type alias Options =
{ githubFlavored :
Maybe
{ tables : Bool
, breaks : Bool
}
, 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.

* 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 }
, sanitize = False
, smartypants = False
}
toElementWith : Options -> String -> Element

Maybe you want to get prettier quotes with a simple syntax. You can use modified parsing options.

options : Options
options =
    { defaultOptions | smartypants <- True }


toSmartElement : String -> Element
toSmartElement markdown =
    Markdown.toElementWith options markdown
toHtmlWith : Options -> String -> Html

Maybe you want to parse user input into markdown. To stop them from adding <script> tags, you can use modified parsing options.

options : Options
options =
    { defaultOptions | sanitize <- True }


toMarkdown : String -> Html
toMarkdown userInput =
    Markdown.toHtmlWith options userInput