dillonkearns / elm-markdown / Markdown.Renderer


type alias Renderer view =
{ heading : { level : Markdown.Block.HeadingLevel
, rawText : String
, children : List view } -> view
, paragraph : List view -> view
, blockQuote : List view -> view
, html : Markdown.Html.Renderer (List view -> view)
, text : String -> view
, codeSpan : String -> view
, strong : List view -> view
, emphasis : List view -> view
, strikethrough : List view -> view
, hardLineBreak : view
, link : { title : Maybe String
, destination : String } -> List view -> view
, image : { alt : String
, src : String
, title : Maybe String } -> view
, unorderedList : List (Markdown.Block.ListItem view) -> view
, orderedList : Basics.Int -> List (List view) -> view
, codeBlock : { body : String
, language : Maybe String } -> view
, thematicBreak : view
, table : List view -> view
, tableHeader : List view -> view
, tableBody : List view -> view
, tableRow : List view -> view
, tableCell : Maybe Markdown.Block.Alignment -> List view -> view
, tableHeaderCell : Maybe Markdown.Block.Alignment -> List view -> 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:

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

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

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/.

Attaching Metadata to Blocks

renderWithMeta : (meta -> Renderer view) -> List ( Markdown.Block.Block, meta ) -> Result String (List view)

Render Tuples of Blocks with arbitrary metadata. See examples/src/Slugs.elm for a full example that shows how to add metadata to blocks.

import Markdown.Parser
import Markdown.Renderer exposing (defaultHtmlRenderer)

markdownInput
    |> Markdown.Parser.parse
    |> Result.map gatherHeadingOccurrences
    |> Result.mapError deadEndsToString
    |> Result.andThen
        (\ast ->
            Markdown.Renderer.renderWithMeta
                (\maybeSlug ->
                    { defaultHtmlRenderer
                        | heading =
                            \{ level, children } ->
                                Html.h1
                                    [ Attr.id (maybeSlug |> Maybe.withDefault "")
                                    ]
                                    children
                    }
                )
                ast
        )