mweiss/elm-rte-toolkit/1.0.3

NOTE: the latest version is 1.0.4

RichText.Config.MarkDefinition

A mark definition describes how to encode and decode a mark.

Mark

type alias MarkDefinition =
MarkDefinition

A mark definition defines how a mark is encoded an decoded.

{ name : String
, toHtmlNode : MarkToHtml
, fromHtmlNode : HtmlToMark
}
-> MarkDefinition

Defines a mark. The arguments are as follows:

  • name - The unique name for this mark. This should be something like 'bold' or 'link'.

  • toHtmlNode - The function that converts the mark to html. This is used in rendering, DOM validation, and path translation.

  • fromHtmlNode - The function that converts html to marks. This is used in things like paste to determine the editor nodes from html.

    code : MarkDefinition
    code =
        markDefinition {name="code", toHtmlNode=codeToHtmlNode, fromHtmlNode=htmlNodeToCode}
type alias MarkToHtml =
Mark -> Array HtmlNode -> HtmlNode

Type alias for a mark encoding function

codeToHtmlNode : MarkToHtml
codeToHtmlNode _ children =
    ElementNode "code" [] children
type alias HtmlToMark =
MarkDefinition -> HtmlNode -> Maybe ( Mark, Array HtmlNode )

Type alias for a mark decoding function

htmlNodeToCode : HtmlToMark
htmlNodeToCode definition node =
    case node of
        ElementNode name _ children ->
            if name == 'code' then
                Just ( mark def [], children )


            else
                Nothing


        _ ->
            Nothing
name : MarkDefinition -> String

Name of the mark this mark definition defines.

name code
--> "code"
toHtmlNode : MarkDefinition -> MarkToHtml

Function which encodes a mark to Html

fromHtmlNode : MarkDefinition -> HtmlToMark

Function which decodes a mark from Html

Struts

defaultMarkDefinition : String -> MarkDefinition

Creates a mark definition which assumes the name of the mark is the same as the name of the html node.

defaultMarkDefinition "b"
--> definition which encodes to <b>...</b> and decodes from "<b>...</b>"
defaultMarkToHtml : String -> Mark -> Array HtmlNode -> HtmlNode

Creates an MarkToHtml function that will encode a mark to html with the same name as the mark.

defaultMarkToHtml "b"
--> returns a function which encodes to "<b>...</b>"
String
-> MarkDefinition
-> Maybe ( Mark, Array HtmlNode )

Creates an HtmlToMark function that will decode a mark from the tag name specified.

defaultHtmlToMark "b"
--> returns a function which decodes from "<b>...</b>"