jxxcarlson/elm-markdown/8.0.0

NOTE: the latest version is 10.1.0

Markdown.Parse

The purpose of this module is to parse a Document, that is, a string, into an abstract syntax tree (AST) which can then be further transformed or passed on to a rendering function. The AST is a rose tree of MDBlockWithId — short for "Markdown Blocks."

See the documentation at the head of module Markdown.ElmWithId for the rationale for this module.

Create or use AST

toMDBlockTree : Int -> MarkdownOption -> Document -> Tree MDBlockWithId

Parse a string using a Markdown flavor option, returning the AST. Example:

Parse.toMDBlockTree 1 Extended "This **is** a test."
--> Tree (MDBlockWithId (0,1)
-->    (MarkdownBlock Root) 0 (M (Paragraph [
-->       Line [OrdinaryText "DOCUMENT"]]))) [
-->          Tree (MDBlockWithId (1,1)
-->            (MarkdownBlock Plain) 1 (M (Paragraph [
-->                Line [  OrdinaryText ("This ")
-->              , BoldText ("is ")
-->              , OrdinaryText ("a test.")]
-->              , Line []
-->            ])))
-->      []]
--> : Tree.Tree Parse.MDBlockWithId
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.

sourceMap : Tree MDBlockWithId -> BiDict String String

Create a sourceMap from the AST: a dictionary whose keys are text strings and whose values ids of the corresponding elements in the DOM

Get lead ing text element from AST

Types

type MDBlock
= MDBlock BlockType Level BlockContent

An MDBlock (Markdown block) carries

- the type of the block
- its level
- its unparsed content
= MDBlockWithId Id BlockType Level BlockContent

An MBlockWithId is like na MDBlock, except that it has an

Id : (Int, Int)

which should be thought of as

( version, id )

where the id is unique to each block.

= M MDInline
| T String

The type of a parsed Block

type alias Id =
( Int, Int )

Used to generate Ids of Html elements and to implement differential rendering. The first Int is a version number, incremented after each edit. The second is a an integer representing position in a traversal of the tree of blocks obtained by parsing the text.

Ids

getId : String -> BiDict String String -> ( String, Maybe String )

Given a string s, return (ss, id), where ss is a string containing s and id a Maybe value representing the id of the corresponding element in the rendered text.

idFromString : String -> ( Int, Int )

Compute id from a string representation of that id, e.g..

idFromString "i5v3"
--> (5,3)
stringFromId : ( Int, Int ) -> String

Compute a string representation of an id. Thus

stringFromId (5,3)
-> "i5v3"

Return the id of a block

Comparison

Check for equality of

- blockType
- level
- content

ignoring the id.

Return true of the blocks have the same id

Tools

Project an MDBlockWithId to an MDBlock by omitting its id

Return a string representing the content of a block if it is of type T. Otherwise, return the empty string.

A string representation of an MDBlockTree. Useful for verifying the validity of the AST.