arowM / elm-markdown-builder / MarkdownBuilder

This library helps your library or application to generate valid markdown document programmatically.

Builder


type Builder parent elem

Markdown builder. You can build a valid markdown structure programmatically in your program:

import MarkdownAst as Ast

myMarkdown : Ast.Section
myMarkdown =
    root
        { title = "Markdown Builder"
        }
        |> editBody
        |> appendParagraph
            [ Ast.PlainText "Markdown Builder builds "
            , Ast.Emphasis "Markdown"
            , Ast.PlainText " programmatically."
            ]
        |> endAppendMode
        |> appendChildSection
            { title = "Builder"
            }
        |> editBody
        |> appendUnorderedList
        |> appendListItem
            [ Ast.PlainText "List Item "
            , Ast.StrongEmphasis "1"
            ]
        |> appendOrderedList
        |> appendListItem
            [ Ast.PlainText "Child item"
            ]
        |> break
        |> break
        |> break
        |> appendListItem
            [ Ast.PlainText "List Item 2"
            ]
        |> appendParagraph
            [ Ast.PlainText "Child paragraph."
            ]
        |> appendCodeBlock
            """elm
            type Builder parent elem =
                ...
                ...
            """
        |> break
        |> appendListItem
            [ Ast.PlainText "List Item 3"
            ]
        |> run

Ast.render myMarkdown
    |> String.lines
--> [ "# Markdown Builder"
--> , ""
--> , "Markdown Builder builds *Markdown* programmatically."
--> , ""
--> , "## Builder"
--> , ""
--> , "* List Item **1**"
--> , "    1. Child item"
--> , "* List Item 2"
--> , ""
--> , "    Child paragraph."
--> , ""
--> , "    ```elm"
--> , "    type Builder parent elem ="
--> , "        ..."
--> , "        ..."
--> , "    ```"
--> , "* List Item 3"
--> ]

break : Builder (Builder parent child) a -> Builder parent child

Quit editing current child, and return to edit its parent.

Root


type Root

Represents markdown root.

root : { title : String } -> Builder Root Section

Entry point for building markdown.

run : Builder parent a -> MarkdownAst.Section

Quit modifying markdown, and compile it to the valid markdown structure.

Section


type Section

Builder for section.

editBody : Builder parent Section -> Builder parent (AppendMode Section)

Start Append Mode for editing section body.

appendChildSection : { title : String } -> Builder p Section -> Builder (Builder p Section) Section

Append new child section to the current section, and change focus to the new one.

List Block


type ListBlock

Builder for a list block.


type ListItem

Builder for items in a list block.

appendListItem : List MarkdownAst.InlineElement -> Builder p ListBlock -> Builder (Builder p ListBlock) (AppendMode ListItem)

Append a list item, and change focus to it.

General Modifiers

Append Mode


type AppendMode a

Represents that a is in the Append Mode, which enalbes you to append some blocks to it.

endAppendMode : Builder parent (AppendMode a) -> Builder parent a

End append mode.

appendParagraph : List MarkdownAst.InlineElement -> Builder parent (AppendMode a) -> Builder parent (AppendMode a)

Append a paragraph block.

appendQuoteBlock : Builder parent (AppendMode a) -> Builder (Builder parent (AppendMode a)) (AppendMode QuoteBlock)

Append a quote block, and focus it.


type QuoteBlock

appendOrderedList : Builder parent (AppendMode a) -> Builder (Builder parent (AppendMode a)) ListBlock

Append an ordered list block, and change focus to it.

appendUnorderedList : Builder parent (AppendMode a) -> Builder (Builder parent (AppendMode a)) ListBlock

Append an unordered list block, and change focus to it.

appendCodeBlock : String -> Builder parent (AppendMode a) -> Builder parent (AppendMode a)

Append a code block.

Elm code:

appendCodeBlock
    """elm
    appendCodeBlock
        = Debug.todo ""
    """

Generated markdown:

```elm
appendCodeBlock
    = Debug.todo ""
```

appendBlocks : List MarkdownAst.BlockElement -> Builder parent (AppendMode a) -> Builder parent (AppendMode a)

Lower level API to append BlockElement.