dillonkearns/elm-markdown/6.0.0

NOTE: the latest version is 7.0.1

Markdown.Renderer

type alias Renderer view =
{ heading :
{ level : HeadingLevel
, rawText : String
, children : List view
}
-> view
, paragraph : List view -> view
, blockQuote : List view -> view
, 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 (ListItem view) -> view
, orderedList : 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 Alignment -> List view -> view
, tableHeaderCell : Maybe 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:

  • 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)
render : Renderer view -> List 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

(meta -> Renderer view)
-> List ( 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
        )