# `Markup`
[🔗](https://github.com/paradox460/markup/blob/markup-v0.1.0/core/lib/markup.ex#L1)

One API for parsing, editing and rendering markup languages.

The `markup` package holds the shared machinery: `Markup.Document` (a node
tree plus a plugin pipeline), Access/Enumerable over nodes, traversal,
streaming, the `~MU` sigil and HEEx rendering. The languages themselves live
in separate packages that implement `Markup.Language`:

  * `markup_markdown` - CommonMark/GFM via comrak (`:markdown`)
  * `markup_djot` - [Djot](https://djot.net) via jotdown (`:djot`)
  * `markup_carve` - [Carve](https://markup-carve.github.io/carve/) via carve-rs (`:carve`)

Add a language package to your deps and it's picked up automatically (see
`Markup.Languages`). If you only have one language installed, every function
uses it. If you have several, pass `language: :id` or set a default:

    config :markup, default_language: :markdown

## Examples

    Markup.to_html!("Hello *world*", language: :djot)
    #=> "<p>Hello <strong>world</strong></p>\n"

    {:ok, doc} = Markup.parse_document("# Title", language: :markdown)
    Markup.convert!(doc, :markdown)

## `use Markup`

`use Markup, opts` requires `Markup`, imports `Markup.Sigil` and stores `opts`
in `@markup_options`, which `~MU` uses at compile time.

# `source`

```elixir
@type source() :: String.t() | Markup.Document.t()
```

Source text or a document.

# `convert`

```elixir
@spec convert(source(), atom(), keyword()) ::
  {:ok, String.t()} | {:error, Exception.t()}
```

Renders source or a document to `format`, which has to be one of the
language's `formats/0` (e.g. `:html`, `:xml`, `:markdown`, `:plain_text`).
`opts` are merged into the document options (see `new/1`).

# `convert!`

```elixir
@spec convert!(source(), atom(), keyword()) :: String.t()
```

Like `convert/3` but raises on error.

# `formats`

```elixir
@spec formats(atom()) :: [atom()]
```

The output formats of an installed language.

    Markup.formats(:carve)
    #=> [:html, :markdown, :plain_text, :ansi, :carve]

# `languages`

```elixir
@spec languages() :: [atom()]
```

The ids of the installed languages.

# `new`

```elixir
@spec new(keyword()) :: Markup.Document.t()
```

Builds a document. `:source` gets parsed when the pipeline runs, and
`:language` picks the language (see `Markup.Languages.resolve/2`). Everything
else goes to `Markup.Document.put_options/2`.

# `parse_document`

```elixir
@spec parse_document(String.t(), keyword()) ::
  {:ok, Markup.Document.t()} | {:error, Exception.t()}
```

Parses source into a `Markup.Document` and runs the pipeline.

# `parse_document!`

```elixir
@spec parse_document!(String.t(), keyword()) :: Markup.Document.t()
```

Like `parse_document/2` but raises on error.

# `safe_html`

```elixir
@spec safe_html(String.t(), keyword()) :: String.t()
```

Sanitizes and/or escapes an HTML string. Plugins use this when they build raw
HTML nodes that need to be safe.

## Options

  * `:sanitize` - options (see `Markup.Sanitize`) merged over the language's
    default sanitize options, or `nil` to skip sanitizing. Defaults to `[]`
    (the language defaults).
  * `:escape` - HTML-escape `& < > " '` after sanitizing. Defaults to `false`.
  * `:heex` - replace `{` and `}` with `&lbrace;` / `&rbrace;` as the last
    step, so `to_heex/2` doesn't read the HTML as HEEx expressions. From a
    step, pass `Markup.Document.get_private(doc, :heex, false)`. Defaults to `false`.
  * `:language` - the language whose sanitizer and defaults to use. Defaults
    to the first installed language.

# `stream`

```elixir
@spec stream(Enumerable.t(), keyword()) :: Enumerable.t()
```

Streams chunks of source and emits `{id, %Markup.Document{}}` for each
top-level block that's new or has changed. A later emission with the same id
replaces the earlier one, and an empty document means the block was removed.

`opts` work as in `new/1` (plugins attach once), except `:source` isn't accepted.

# `to_heex`
*macro* 

Renders markup with HEEx components and expressions into a
`Phoenix.LiveView.Rendered`.

You'll need `{:phoenix_live_view, "~> 1.2"}`. Which HEEx constructs get
recognized depends on the language. Assigns come from the `:assigns` option.
The template is compiled at runtime with the caller's environment, so your
imports (e.g. `Phoenix.Component`) apply. `sanitize:` isn't supported.

# `to_heex!`
*macro* 

Like `to_heex/2` but raises on error.

# `to_html`

```elixir
@spec to_html(source(), keyword()) :: {:ok, String.t()} | {:error, Exception.t()}
```

Renders source or a document to HTML; see `convert/3`.

# `to_html!`

```elixir
@spec to_html!(source(), keyword()) :: String.t()
```

Like `to_html/2` but raises on error.

# `traverse_and_update`

Traverses and updates the tree post-order; returning `nil` or `:pop` removes a node.

# `traverse_and_update`

Like `traverse_and_update/2` with an accumulator; the function returns `{node, acc}`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
