Markup.Language behaviour (Markup v0.1.0)

Copy Markdown View Source

The behaviour a markup language package implements.

The core (Markup, Markup.Document) owns the document, the plugin pipeline, Access/Enumerable, traversal, streaming, sigils and the HEEx glue. A language owns its node structs, its options and its parser and renderer.

Discovery

A language package registers its language module in its application env, in mix.exs:

def application do
  [env: [markup_languages: [MyLang]]]
end

Markup.Languages.all/0 collects :markup_languages from every loaded application, so adding the package to your deps is all it takes. You can also list modules explicitly with config :markup, languages: [MyLang].

Callbacks

  • id/0 - lowercase ASCII letters only, e.g. :djot. Used for language: and the ~MU sigil modifier.
  • node_namespace/0 - bare-atom selectors that aren't a Markup.Node kind resolve under it (:div becomes Namespace.Div).
  • formats/0 - output formats; the first MUST be :html.
  • options_schema/0 - a NimbleOptions schema for the language's own option keys (e.g. :render, :parse). The core adds :plugins, :sanitize and :assigns.
  • parse/2 - parses source into top-level node structs.
  • render/3 - renders a document whose pipeline has already run. It applies syntax highlighting, sanitizes with Markup.Sanitize.prepare(Markup.Document.get_option(doc, :sanitize)) (a fully validated keyword list, or nil) and escapes HEEx braces when ctx.heex.
  • default_sanitize_options/0 - defaults merged under user sanitize: options. Keys follow Markup.Sanitize.schema/0.
  • safe_html/3 - sanitizes (sanitize is already merged and prepared: a map, or nil to skip) and optionally HTML-escapes a string.

Optional:

  • merge_option/3 - merges a new value for one of the language's option keys into the current one. Defaults to a deep Keyword.merge/2 when both are keyword lists, otherwise the new value replaces the old. The result is validated against the schema entry.
  • render_source/4 - a fast path that renders source directly. It's only called when the document has no nodes, has a binary source and has no pending steps. The language can still fall back to its tree path internally.
  • stream_blocks/2 - parses a stream buffer into top-level blocks. Defaults to parse/2.
  • heex_prepare/1 - prepares a document for HEEx rendering (for example by rewriting HEEx constructs in the source). Defaults to identity.

Summary

Types

format()

@type format() :: atom()

render_ctx()

@type render_ctx() :: %{heex: boolean()}

Callbacks

default_sanitize_options()

@callback default_sanitize_options() :: keyword()

formats()

@callback formats() :: [format()]

heex_prepare(t)

(optional)
@callback heex_prepare(Markup.Document.t()) :: Markup.Document.t()

id()

@callback id() :: atom()

merge_option(key, current, value)

(optional)
@callback merge_option(key :: atom(), current :: term(), value :: term()) :: term()

node_namespace()

@callback node_namespace() :: module()

options_schema()

@callback options_schema() :: keyword()

parse(source, t)

@callback parse(source :: String.t(), Markup.Document.t()) ::
  {:ok, [struct()]} | {:error, Exception.t()}

render(t, format, render_ctx)

@callback render(Markup.Document.t(), format(), render_ctx()) ::
  {:ok, String.t()} | {:error, Exception.t()}

render_source(t, t, format, render_ctx)

(optional)
@callback render_source(String.t(), Markup.Document.t(), format(), render_ctx()) ::
  {:ok, String.t()} | {:error, Exception.t()}

safe_html(html, sanitize, escape)

@callback safe_html(html :: String.t(), sanitize :: map() | nil, escape :: boolean()) ::
  {:ok, String.t()} | {:error, term()}

stream_blocks(buffer, t)

(optional)
@callback stream_blocks(buffer :: String.t(), Markup.Document.t()) ::
  {:ok, [struct()]} | {:error, Exception.t()}