arowM/elm-markdown-builder/2.1.0

NOTE: the latest version is 3.1.2

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:

myMarkdown : Root
myMarkdown =
    root
        { title = "Markdown Builder"
        }
        |> editBody
        |> appendParagraph
        |> pushText
            "Markdown Builder builds"
        |> pushEmphasis
            "Markdown"
        |> pushText
            "programmatically."
        |> break
        |> endAppendMode
        |> appendChildSection
            { title = "Builder"
            }
        |> editBody
        |> appendUnorderedList
        |> appendListItem
        |> editListItemContent
        |> pushText "List Item 1"
        |> endPushMode
        |> editListItemChildren
        |> appendOrderedList
        |> appendListItem
        |> editListItemContent
        |> pushText "Child item"
        |> break
        |> break
        |> break
        |> appendListItem
        |> editListItemContent
        |> pushText "List Item 2"
        |> endPushMode
        |> editListItemChildren
        |> appendParagraph
        |> pushText
            "Child paragraph."
        |> break
        |> appendCodeBlock
            """elm
            type Builder parent elem =
                ...
                ...
            """
        |> break
        |> appendListItem
        |> editListItemContent
        |> pushText "List Item 3"
        |> run


toString 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 -> Root

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

toString : Root -> String

Render Root into markdown text string.

preview : Root -> Html msg

Preview markdown content as an HTML page.

Section

type Section

Represents markdown section.

# Title for Root Section


Paragraph text in root section body.


Another paragraph text in root section body.


* List item in root section body
* Another list item in root section body


## Title for Child Section


Paragraph text in child section body.


```json
{
  "message": "Code block in child section body."
}
```


## Title for Another Child Section


Paragraph text in another child section body.


1. Ordered list item in another child section body
1. Another ordered list item in another child section body

Start Append Mode for editing section body.

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

List Block

Represents markdown list block.

Represents an item in the ListBlock.

* First `ListItem` content for this unordered `ListBlock`
    Child element for the first `ListItem`.

    1. This `ListItem` is in the child `ListBlock` for the First `ListItem` content.
    1. This `ListItem` is also in the child `ListBlock` for the First `ListItem` content.

* Second `ListItem` content for this unordered `ListBlock`

Start Push Mode for editing list item content.

Start Append Mode for editing list item children.

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.

Append a paragraph block, and change to push mode for the paragraph.

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

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

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

Append a code block, and change focus to it.

Elm code:

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

Generated markdown:

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

Push Mode

type PushMode a

Represents that a is in the Push Mode, which enables you to push some inline stuffs to it.

End push mode.

String
-> Builder parent (PushMode a)
-> Builder parent (PushMode a)

Push a plain text.

String
-> Builder parent (PushMode a)
-> Builder parent (PushMode a)

Push an inline code.

`inline code`
String
-> Builder parent (PushMode a)
-> Builder parent (PushMode a)

Push an emphasized text.

*emphasized*
String
-> Builder parent (PushMode a)
-> Builder parent (PushMode a)

Push a strongly emphasized text.

**emphasized**
String
-> Builder parent (PushMode a)
-> Builder parent (PushMode a)

Push a strikethrough text.

~~strikethrough~~

Unknown

import MarkdownBuilder as MB


snippet : Builder Unknown ListItem -> Builder Unknown ListItem
snippet =
    MB.editListItemContent
        >> MB.pushText "foo "
        >> MB.pushEmphasis "bar"
        >> MB.endPushMode


myMarkdown : Root
myMarkdown =
    root
        { title = "Markdown Builder"
        }
        |> editBody
        |> appendOrderedList
        |> appendListItem
        |> applySnippet snippet
        |> break
        |> appendListItem
        |> applySnippet snippet
        |> break
        |> break
        |> endAppendMode
        |> appendChildSection
            { title = "Builder"
            }
        |> editBody
        |> appendUnorderedList
        |> appendListItem
        |> applySnippet snippet
        |> run


toString myMarkdown
    |> String.lines
--> [ "# Markdown Builder"
--> , ""
--> , "1. foo *bar*"
--> , "1. foo *bar*"
--> , ""
--> , "## Builder"
--> , ""
--> , "* foo *bar*"
--> ]
type Unknown