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:
Coelho.Schema— declaring the schema and exporting it to the browserCoelho.Document— validating, normalising, extracting plain textCoelho.Render— turning a validated document into HTML
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
@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.
@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.
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.
@spec to_html(map(), Coelho.Schema.t() | Coelho.Render.opts()) :: String.t()
@spec to_html(map(), Coelho.Schema.t(), Coelho.Render.opts()) :: String.t()
@spec to_text(map(), Coelho.Schema.t()) :: String.t()
Extracts the plain text of a document, for full text search.
@spec validate(term(), Coelho.Schema.t()) :: {:ok, map()} | {:error, [Coelho.Document.Error.t()]}
Validates and normalises a document against a schema.