jxxcarlson/elm-markdown/3.0.1

NOTE: the latest version is 10.1.0

Markdown.ElmWithId

Use this module if you need to edit math + markdown and require optimizations for speed and a smooth editing experience.

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.

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.

parse : Int -> Option -> String -> Tree MDBlockWithId

Given a version number, an option defining a flavor of Markdown, and a text string, return a parse tree.

renderHtml : Tree MDBlockWithId -> Html msg

Render a parse tree to Html.

renderHtmlWithTOC : String -> Tree MDBlockWithId -> Html msg

Like renderHtml, but constructs a table of contents.

String
-> Tree MDBlockWithId
-> { title : Html msg
, toc : Html msg
, document : Html msg
}

Like renderHtmlWithTOC, but transforms a parser three into a record, with fields for the document title, the table of contents, and the body of the document.

searchAST : String -> Tree MDBlockWithId -> Maybe Id

Search the AST for nodes whose label contains the given string, returning the Id of the first node found, if any.

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