Coelho (coelho v0.1.0)

Copy Markdown View Source

Structured rich text for Elixir.

Coelho stores a rich text document as a validated tree — the same shape ProseMirror produces — rather than as a blob of HTML. A schema, written once in Elixir, says which nodes and marks exist; validating a document against it is the sanitisation step, and rendering it is a pure function the application can override node by node.

This module is the convenience surface over the three that do the work:

Every function here defaults to Coelho.Schema.default/0; applications with their own schema call the underlying modules directly.

iex> document = %{
...>   "type" => "doc",
...>   "content" => [
...>     %{"type" => "paragraph", "content" => [%{"type" => "text", "text" => "hello"}]}
...>   ]
...> }
iex> {:ok, document} = Coelho.validate(document)
iex> Coelho.to_html(document)
"<p>hello</p>"

Summary

Functions

The empty document of a schema.

Converts existing HTML into a validated document.

Renders a validated document to HTML.

Extracts the plain text of a document, for full text search.

Validates and normalises a document against a schema.

Functions

empty(schema \\ Schema.default())

@spec empty(Coelho.Schema.t()) :: map()

The empty document of a schema.

The child is derived from the top node's content expression rather than assumed to be a paragraph, so a schema that calls its block node something else still gets a document its own validate/2 accepts.

from_html(html, schema \\ Schema.default())

@spec from_html(String.t(), Coelho.Schema.t()) :: {:ok, map()} | {:error, term()}

Converts existing HTML into a validated document.

The migration path for content already stored as HTML. Requires the optional :floki dependency; see Coelho.HTML for what the import does with markup the schema does not know.

to_html(document)

@spec to_html(map()) :: String.t()

Renders a validated document to HTML.

The schema may be left out, in which case the render options can be passed straight as the second argument.

to_html(document, opts)

@spec to_html(map(), Coelho.Schema.t() | Coelho.Render.opts()) :: String.t()

to_html(document, schema, opts)

@spec to_html(map(), Coelho.Schema.t(), Coelho.Render.opts()) :: String.t()

to_text(document, schema \\ Schema.default())

@spec to_text(map(), Coelho.Schema.t()) :: String.t()

Extracts the plain text of a document, for full text search.

validate(document, schema \\ Schema.default())

@spec validate(term(), Coelho.Schema.t()) ::
  {:ok, map()} | {:error, [Coelho.Document.Error.t()]}

Validates and normalises a document against a schema.