jxxcarlson / elm-markdown / 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 : Basics.Int -> Markdown.Option.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

getLeadingTextFromAST : Tree MDBlockWithId -> String

Get lead ing text element from AST

toTextTree : Tree MDBlock -> Tree String

Map a (Tree MDBlock) to a (Tree String)

Types


type MDBlock
    = MDBlock BlockType Level BlockContent

An MDBlock (Markdown block) carries

- the type of the block
- its level
- its unparsed content


type MDBlockWithId
    = 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

( elementId, version )

where the id is unique to each block.


type BlockContent
    = M MDInline
    | T String

The type of a parsed Block


type alias Id =
( Basics.Int, Basics.Int )

Used to generate Ids of Html elements and to implement differential rendering. An Id has the form

(elementId, version) : (Int, Int

The elementId is a an integer representing position in a traversal of the tree of blocks obtained by parsing the text. The version number is incremented after each edit.

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 -> ( Basics.Int, Basics.Int )

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

idFromString "i5v3"
--> (5,3)

stringFromId : ( Basics.Int, Basics.Int ) -> String

Compute a string representation of an id. Thus

stringFromId (5,3)
-> "i5v3"

idOfBlock : MDBlockWithId -> Id

Return the id of a block

incrementVersion : Id -> Tree MDBlockWithId -> Tree MDBlockWithId

Scan the tree, incrementing the version of the target Id if found. This function is used to update the AST int order to highlight the rendered text which belongs to the target Id. Use

Markdown.Render.fromASTWithOptions
    outputOption
    targetId
    ast

Comparison

equalContent : MDBlockWithId -> MDBlockWithId -> Basics.Bool

Check for equality of

- blockType
- level
- content

ignoring the id.

equalIds : MDBlockWithId -> MDBlockWithId -> Basics.Bool

Return true if the blocks have the same id

Tools

project : MDBlockWithId -> MDBlock

Project an MDBlockWithId to an MDBlock by omitting its id

projectedStringOfBlockContent : BlockContent -> String

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

stringOfMDBlockTree : Tree MDBlockWithId -> String

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

getArgPair : String -> String -> Maybe ( String, String )

Get arguments that are used constructs like @xlink[ arg1 > arg2 ]