One API for parsing, editing and rendering markup languages.
The markup package holds the shared machinery: Markup.Document (a node
tree plus a plugin pipeline), Access/Enumerable over nodes, traversal,
streaming, the ~MU sigil and HEEx rendering. The languages themselves live
in separate packages that implement Markup.Language:
markup_markdown- CommonMark/GFM via comrak (:markdown)markup_djot- Djot via jotdown (:djot)markup_carve- Carve via carve-rs (:carve)
Add a language package to your deps and it's picked up automatically (see
Markup.Languages). If you only have one language installed, every function
uses it. If you have several, pass language: :id or set a default:
config :markup, default_language: :markdownExamples
Markup.to_html!("Hello *world*", language: :djot)
#=> "<p>Hello <strong>world</strong></p>\n"
{:ok, doc} = Markup.parse_document("# Title", language: :markdown)
Markup.convert!(doc, :markdown)use Markup
use Markup, opts requires Markup, imports Markup.Sigil and stores opts
in @markup_options, which ~MU uses at compile time.
Summary
Functions
Renders source or a document to format, which has to be one of the
language's formats/0 (e.g. :html, :xml, :markdown, :plain_text).
opts are merged into the document options (see new/1).
Like convert/3 but raises on error.
The output formats of an installed language.
The ids of the installed languages.
Builds a document. :source gets parsed when the pipeline runs, and
:language picks the language (see Markup.Languages.resolve/2). Everything
else goes to Markup.Document.put_options/2.
Parses source into a Markup.Document and runs the pipeline.
Like parse_document/2 but raises on error.
Sanitizes and/or escapes an HTML string. Plugins use this when they build raw HTML nodes that need to be safe.
Streams chunks of source and emits {id, %Markup.Document{}} for each
top-level block that's new or has changed. A later emission with the same id
replaces the earlier one, and an empty document means the block was removed.
Renders markup with HEEx components and expressions into a
Phoenix.LiveView.Rendered.
Like to_heex/2 but raises on error.
Renders source or a document to HTML; see convert/3.
Like to_html/2 but raises on error.
Traverses and updates the tree post-order; returning nil or :pop removes a node.
Like traverse_and_update/2 with an accumulator; the function returns {node, acc}.
Types
@type source() :: String.t() | Markup.Document.t()
Source text or a document.
Functions
@spec convert(source(), atom(), keyword()) :: {:ok, String.t()} | {:error, Exception.t()}
Renders source or a document to format, which has to be one of the
language's formats/0 (e.g. :html, :xml, :markdown, :plain_text).
opts are merged into the document options (see new/1).
Like convert/3 but raises on error.
The output formats of an installed language.
Markup.formats(:carve)
#=> [:html, :markdown, :plain_text, :ansi, :carve]
@spec languages() :: [atom()]
The ids of the installed languages.
@spec new(keyword()) :: Markup.Document.t()
Builds a document. :source gets parsed when the pipeline runs, and
:language picks the language (see Markup.Languages.resolve/2). Everything
else goes to Markup.Document.put_options/2.
@spec parse_document(String.t(), keyword()) :: {:ok, Markup.Document.t()} | {:error, Exception.t()}
Parses source into a Markup.Document and runs the pipeline.
@spec parse_document!(String.t(), keyword()) :: Markup.Document.t()
Like parse_document/2 but raises on error.
Sanitizes and/or escapes an HTML string. Plugins use this when they build raw HTML nodes that need to be safe.
Options
:sanitize- options (seeMarkup.Sanitize) merged over the language's default sanitize options, ornilto skip sanitizing. Defaults to[](the language defaults).:escape- HTML-escape& < > " 'after sanitizing. Defaults tofalse.:heex- replace{and}with{/}as the last step, soto_heex/2doesn't read the HTML as HEEx expressions. From a step, passMarkup.Document.get_private(doc, :heex, false). Defaults tofalse.:language- the language whose sanitizer and defaults to use. Defaults to the first installed language.
@spec stream(Enumerable.t(), keyword()) :: Enumerable.t()
Streams chunks of source and emits {id, %Markup.Document{}} for each
top-level block that's new or has changed. A later emission with the same id
replaces the earlier one, and an empty document means the block was removed.
opts work as in new/1 (plugins attach once), except :source isn't accepted.
Renders markup with HEEx components and expressions into a
Phoenix.LiveView.Rendered.
You'll need {:phoenix_live_view, "~> 1.2"}. Which HEEx constructs get
recognized depends on the language. Assigns come from the :assigns option.
The template is compiled at runtime with the caller's environment, so your
imports (e.g. Phoenix.Component) apply. sanitize: isn't supported.
Like to_heex/2 but raises on error.
@spec to_html(source(), keyword()) :: {:ok, String.t()} | {:error, Exception.t()}
Renders source or a document to HTML; see convert/3.
Like to_html/2 but raises on error.
Traverses and updates the tree post-order; returning nil or :pop removes a node.
Like traverse_and_update/2 with an accumulator; the function returns {node, acc}.