dillonkearns/elm-markdown/2.0.1

NOTE: the latest version is 7.0.1

Markdown.Parser

type alias Renderer view =
{ heading :
{ level : Int
, rawText : String
, children : List view
}
-> view
, raw : List view -> view
, html : Renderer (List view -> view)
, plain : String -> view
, code : String -> view
, bold : String -> view
, italic : String -> view
, link :
{ title : Maybe String
, destination : String
}
-> List view
-> Result String view
, image : { src : String } -> String -> Result String view
, unorderedList : List (List view) -> view
, orderedList : Int -> List (List view) -> view
, codeBlock :
{ body : String
, language : Maybe String
}
-> view
, thematicBreak : view
}

A record with functions that define how to render all possible markdown blocks. These renderers are composed together to give you the final rendered output.

You could render to any type you want. Here are some useful things you might render to:

  • Html (using the defaultHtmlRenderer provided by this module)
  • Custom Html
  • Elements from mdgriffith/elm-ui
  • Types from other custom HTML replacement libraries, like rtfeldman/elm-css or tesk9/accessible-html
  • Raw Strings with ANSI color codes for setting rich colors in terminal (CLI) output
  • Plain text with any formatting stripped away (maybe for a String search feature)
defaultHtmlRenderer : Renderer (Html msg)

This renders Html in an attempt to be as close as possible to the HTML output in https://github.github.com/gfm/.

deadEndToString : DeadEnd String Problem -> String

Turn a parsing problem into the default String representation.

parse : String -> Result (List (DeadEnd String Problem)) (List Block)

Try parsing a markdown String into Markdown.Block.Blocks.

Often you'll want to render these Blocks directly:

render renderer markdown =
    markdown
        |> Markdown.parse
        |> Result.mapError deadEndsToString
        |> Result.andThen (\ast -> Markdown.render renderer ast)


deadEndsToString deadEnds =
    deadEnds
        |> List.map deadEndToString
        |> String.join "\n"

But you can also do a lot with the Blocks before passing them through:

render : Renderer view -> List Block -> Result String (List view)

Apply a Markdown.Parser.Renderer to turn parsed Blocks into your rendered markdown view.