jxxcarlson/elm-markdown/8.0.0

NOTE: the latest version is 10.1.0

Markdown.Render

Simple Applications

When there is no interaction between the Markdown text and and an on-the-fly editor, use the withSimpleOptions function:

withSimpleOptions :
    MarkdownOption
    -> OutputOption
    -> String
    -> MarkdownOutput

For example:

withSimpleOptions Extended Basic "This is *italic text.*"
    |> content
    |> Html.map MarkdownMsg

where one has

import Markdown.Render exposing(withSimpleOptions, MarkdownMsg(..))
import Markdown.Options expsosing(..)


type Msg = MarkdownMsg MarkdownMsg | ...

Editor interaction

Use the withOptions and withOptionsFromAST functions if you need more control over the editing process (see the ./Editor folder for examples).

The function parse yields a syntax tree (AST: abstract syntax tree). The functions renderHtml, renderHtmlWithTOC, and renderHtmlWithExternaTOC render the AST in various forms, as described below.

This library

Optimizations

The idea of the optimizations used in app-demo-optimized (see the repo) is to parse the document text when the document is first opened. The resulting parse tree is stored as model.lastAst. Each block in the AST carries a label (version, id): (Int, Int), where the id is unique to each block. Each time the text changes, a new AST is computed with an incremented version number. The function Diff.mergeWith equals is applied to the old and new ASTs to compute an updated AST. The updated AST is identical to the new AST except for the id's. The id of a node in the updated AST is the same as in the old AST if and only if the type, level, and content of the node has not changed. This information is used to signal MathJax not to re-render mathematical text that is unchanged.

To see where these optimizations are applied, look for the places in app-demo-optimized/Main.elm where functions in the modules ParseWithId and Markdown.ElmWithId are called.

Types

= IDClicked String

Use Html MarkdownMsg so that user clicks on elements in the rendered text can be detected.

= Simple (Html MarkdownMsg)
| Composite DocumentParts

The kinds of Markdown output

type alias DocumentParts =
{ title : Html MarkdownMsg
, toc : Html MarkdownMsg
, document : Html MarkdownMsg
}

The type of a composite document

Rendering

toHtml : MarkdownOption -> String -> Html MarkdownMsg

Parse the input and render it to Html, e.g.,

toHtml ExtendedMath "Pythagoras said: $a^2 + b^2 c^2$."
withOptions : MarkdownOption -> OutputOption -> Id -> Int -> String -> MarkdownOutput

Render source text

Render source text with simplified opptions

Extracting content from a MarkdownOutput value

Extract the content from a MarkdownOutput value

Extract the title from a MarkdownOutput value

Extract the table of contents from a MarkdownOutput value

Utility

renderHtml : Id -> Tree MDBlockWithId -> Html MarkdownMsg

Render a parse tree to Html.

toHtmlWithId : Id -> Int -> MarkdownOption -> String -> Html MarkdownMsg

Parse the input and render it to Html, e.g.,

toHtmlWithId ( 0, 0 ) 0 ExtendedMath "Pythagoras said: $a^2 + b^2 c^2$."

Count the number of display math element blocks in the parse tree