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

A markup document: a tree of language nodes plus a Req-like pipeline of
options and steps.

The API mirrors `MDEx.Document`, so porting a plugin is mostly mechanical.
Each document belongs to one `Markup.Language` (`language`), and its node
structs live under that language's namespace (`Markup.Djot.Strong`,
`Markup.Markdown.Strong`, …).

## Tree

`nodes` holds the top-level blocks. Documents and nodes implement `Access`
and `Enumerable` (a pre-order walk, the root included):

    doc = Markup.parse_document!("Hello *world*", language: :djot)
    doc[:strong]
    #=> [%Markup.Djot.Strong{nodes: [%Markup.Djot.Text{literal: "world"}]}]

A selector can be a node module, a bare atom, a struct pattern whose
non-default fields must match (`%Markup.Djot.Heading{level: 2}`), a 1-arity
predicate, or an integer index into the pre-order walk. A bare atom that's
one of `Markup.Node.kinds/0` matches every node of that kind, whatever the
language calls it (`:emphasis` finds `Markup.Markdown.Emph`). Any other atom
resolves under the language's node namespace (`:div` → `Markup.Djot.Div`).

## Pipeline

A plugin is a module with an `attach/2` function (or just a 1-arity function)
that registers options, puts options and appends steps. A step is a 1-arity
function or `{module, function, args}`. It gets the document and returns it,
or returns `halt/2`'s `{document, exception}`.

    defmodule ShoutPlugin do
      alias Markup.Document

      def attach(doc, opts) do
        doc
        |> Document.register_options([:shout])
        |> Document.put_options(opts)
        |> Document.append_steps(shout: &shout/1)
      end

      defp shout(doc) do
        if Document.get_option(doc, :shout) do
          Document.update_nodes(doc, :text, &%{&1 | literal: String.upcase(&1.literal)})
        else
          doc
        end
      end
    end

### Private keys set by Markup

Steps can read these with `get_private/3`:

  * `:heex` - `true` while rendering for `Markup.to_heex/2` or `~MU"..."HEEX`.
    Raw HTML nodes are written out verbatim, so braces in element content would
    be read as HEEx expressions. Build that HTML with `Markup.safe_html(html, heex: true)`.
  * `:stream` - `%{id: id}` on each per-block document built by `Markup.stream/2`.
    Plugins that inject page-level content (scripts, styles) should skip these
    documents, and should put the id in element ids so they stay unique across blocks.

## Options

Every language takes `:plugins`, `:sanitize` (see `Markup.Sanitize`) and
`:assigns`, plus its own keys (`c:Markup.Language.options_schema/0`).

# `plugin`

```elixir
@type plugin() :: module() | {module(), keyword()} | (t() -&gt; t())
```

# `selector`

```elixir
@type selector() :: module() | atom() | struct() | (struct() -&gt; boolean()) | integer()
```

# `step`

```elixir
@type step() :: (t() -&gt; t() | {t(), Exception.t()}) | {module(), atom(), [term()]}
```

# `t`

```elixir
@type t() :: %Markup.Document{
  current_steps: [atom()],
  halted: boolean(),
  language: module() | nil,
  nodes: [struct()],
  options: keyword(),
  private: map(),
  registered_options: MapSet.t(atom()),
  source: String.t() | nil,
  steps: keyword(step())
}
```

# `append_steps`

```elixir
@spec append_steps(t(), keyword(step())) :: t()
```

Appends named steps to the pipeline.

# `assign`

```elixir
@spec assign(t(), map() | keyword()) :: t()
```

Merges assigns from a map or keyword list.

# `assign`

```elixir
@spec assign(t(), atom(), term()) :: t()
```

Adds one assign.

# `get_option`

```elixir
@spec get_option(t(), atom(), term()) :: term()
```

Returns an option value.

# `get_private`

```elixir
@spec get_private(t(), atom(), term()) :: term()
```

Reads private data.

# `halt`

```elixir
@spec halt(t()) :: t()
```

Stops the pipeline so no more steps run. The document still renders.

# `halt`

```elixir
@spec halt(t(), Exception.t()) :: {t(), Exception.t()}
```

Stops the pipeline with an exception; rendering returns `{:error, exception}`.

# `prepend_steps`

```elixir
@spec prepend_steps(t(), keyword(step())) :: t()
```

Prepends named steps to the pipeline.

# `put_node_in_document_root`

```elixir
@spec put_node_in_document_root(t(), struct(), :top | :bottom) :: t()
```

Inserts `node` at the `:top` (default) or `:bottom` of the document root.

# `put_options`

```elixir
@spec put_options(t(), keyword()) :: t()
```

Merges options into the document. `:plugins` get attached first, then the
rest of the keys are applied in order.

Language keys are merged with `c:Markup.Language.merge_option/3` (a deep
keyword merge by default) and then validated. `:sanitize` is merged over the
language's default sanitize options, and `nil` turns it off. `:assigns` are
map-merged.

Raises `ArgumentError` for any key that's neither built in nor registered.

# `put_plugins`

```elixir
@spec put_plugins(t(), [plugin()]) :: t()
```

Attaches plugins: `module` calls `module.attach(doc, [])`, `{module, opts}`
calls `module.attach(doc, opts)`, and a 1-arity function gets called with the doc.

# `put_private`

```elixir
@spec put_private(t(), atom(), term()) :: t()
```

Stores private (plugin-internal) data.

# `put_source`

```elixir
@spec put_source(t(), String.t() | nil) :: t()
```

Sets source to be parsed by `run/1`.

# `register_options`

```elixir
@spec register_options(t(), [atom()]) :: t()
```

Registers user option keys so `put_options/2` accepts them.

# `run`

```elixir
@spec run(t()) :: t() | {t(), Exception.t()}
```

Runs the pipeline. Any pending `source` is parsed with the document's language
and appended to `nodes`, then the pending steps run in order until one halts.

If parsing fails, you get `{document, exception}` back with `source` cleared.

# `update_nodes`

```elixir
@spec update_nodes(t(), selector(), (struct() -&gt; struct())) :: t()
```

Applies `fun` to every node matching `selector` (post-order).

# `update_private`

```elixir
@spec update_private(t(), atom(), term(), (term() -&gt; term())) :: t()
```

Updates private data with `fun`, starting from `default`.

# `wrap`

```elixir
@spec wrap(struct() | [struct()], atom()) :: t()
```

Wraps a node or a list of nodes in a document of `language` (an id such as
`:djot` or a language module).

---

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