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]]]
endMarkup.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 forlanguage:and the~MUsigil modifier.node_namespace/0- bare-atom selectors that aren't aMarkup.Nodekind resolve under it (:divbecomesNamespace.Div).formats/0- output formats; the first MUST be:html.options_schema/0- aNimbleOptionsschema for the language's own option keys (e.g.:render,:parse). The core adds:plugins,:sanitizeand: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 withMarkup.Sanitize.prepare(Markup.Document.get_option(doc, :sanitize))(a fully validated keyword list, ornil) and escapes HEEx braces whenctx.heex.default_sanitize_options/0- defaults merged under usersanitize:options. Keys followMarkup.Sanitize.schema/0.safe_html/3- sanitizes (sanitizeis already merged and prepared: a map, ornilto 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 deepKeyword.merge/2when 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 toparse/2.heex_prepare/1- prepares a document for HEEx rendering (for example by rewriting HEEx constructs in the source). Defaults to identity.
Summary
Types
Callbacks
@callback default_sanitize_options() :: keyword()
@callback formats() :: [format()]
@callback heex_prepare(Markup.Document.t()) :: Markup.Document.t()
@callback id() :: atom()
@callback node_namespace() :: module()
@callback options_schema() :: keyword()
@callback parse(source :: String.t(), Markup.Document.t()) :: {:ok, [struct()]} | {:error, Exception.t()}
@callback render(Markup.Document.t(), format(), render_ctx()) :: {:ok, String.t()} | {:error, Exception.t()}
@callback render_source(String.t(), Markup.Document.t(), format(), render_ctx()) :: {:ok, String.t()} | {:error, Exception.t()}
@callback stream_blocks(buffer :: String.t(), Markup.Document.t()) :: {:ok, [struct()]} | {:error, Exception.t()}