pablohirafuji / elm-markdown / Markdown.Inline

Inline rendering and helpers.

Model


type Inline i
    = Text String
    | HardLineBreak
    | CodeInline String
    | Link String (Maybe String) (List (Inline i))
    | Image String (Maybe String) (List (Inline i))
    | HtmlInline String (List ( String, Maybe String )) (List (Inline i))
    | Emphasis Basics.Int (List (Inline i))
    | Custom i (List (Inline i))

The inline type.

Renders

toHtml : Inline i -> Html msg

Transform an Inline into Html using the default html elements.

toHtml (Text "Inner text") == Html.text "Inner text"
toHtml HardLineBreak == Html.br [] []
toHtml (CodeInline code) == Html.code [] [ Html.text code ]

defaultHtml : Maybe (Inline i -> Html msg) -> Inline i -> Html msg

Transform an Inline into a Html, optionally using custom html elements to render inner inlines.

upperText : Inline i -> Html msg
upperText inline =
    case inline of
        Text str ->
            Html.text (String.toUpper str)

        _ ->
            defaultHtml (Just upperText) inline


defaultHtml (Just upperText) (Text "hello") == text "hello"
defaultHtml (Just upperText) (Emphasis 2 [ Text "hello" ]) == strong [] [ text "HELLO" ]
upperText (Text "hello") == text "HELLO"

Note: If the first argument is Nothing, the default html elements will be used to render the inner inlines.

Helpers

extractText : List (Inline i) -> String

Extract the text from a list of inlines.

inlines : List (Inline i)
inlines =
    [ Text "Heading with "
    , Emphasis 1
        [ Text "emphasis" ]
    ]

extractText inlines == "Heading with emphasis"

-- Original string: "Heading with *emphasis*"