Markup (Markup v0.1.0)

Copy Markdown View Source

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: :markdown

Examples

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

Types

Source text or a document.

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).

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.

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.

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

source()

@type source() :: String.t() | Markup.Document.t()

Source text or a document.

Functions

convert(source_or_doc, format, opts \\ [])

@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).

convert!(source_or_doc, format, opts \\ [])

@spec convert!(source(), atom(), keyword()) :: String.t()

Like convert/3 but raises on error.

formats(language)

@spec formats(atom()) :: [atom()]

The output formats of an installed language.

Markup.formats(:carve)
#=> [:html, :markdown, :plain_text, :ansi, :carve]

languages()

@spec languages() :: [atom()]

The ids of the installed languages.

new(opts \\ [])

@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.

parse_document(source, opts \\ [])

@spec parse_document(String.t(), keyword()) ::
  {:ok, Markup.Document.t()} | {:error, Exception.t()}

Parses source into a Markup.Document and runs the pipeline.

parse_document!(source, opts \\ [])

@spec parse_document!(String.t(), keyword()) :: Markup.Document.t()

Like parse_document/2 but raises on error.

safe_html(html, opts \\ [])

@spec safe_html(String.t(), keyword()) :: String.t()

Sanitizes and/or escapes an HTML string. Plugins use this when they build raw HTML nodes that need to be safe.

Options

  • :sanitize - options (see Markup.Sanitize) merged over the language's default sanitize options, or nil to skip sanitizing. Defaults to [] (the language defaults).
  • :escape - HTML-escape & < > " ' after sanitizing. Defaults to false.
  • :heex - replace { and } with &lbrace; / &rbrace; as the last step, so to_heex/2 doesn't read the HTML as HEEx expressions. From a step, pass Markup.Document.get_private(doc, :heex, false). Defaults to false.
  • :language - the language whose sanitizer and defaults to use. Defaults to the first installed language.

stream(enumerable, opts \\ [])

@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.

to_heex(source, opts \\ [])

(macro)

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.

to_heex!(source, opts \\ [])

(macro)

Like to_heex/2 but raises on error.

to_html(source_or_doc, opts \\ [])

@spec to_html(source(), keyword()) :: {:ok, String.t()} | {:error, Exception.t()}

Renders source or a document to HTML; see convert/3.

to_html!(source_or_doc, opts \\ [])

@spec to_html!(source(), keyword()) :: String.t()

Like to_html/2 but raises on error.

traverse_and_update(doc, fun)

Traverses and updates the tree post-order; returning nil or :pop removes a node.

traverse_and_update(doc, acc, fun)

Like traverse_and_update/2 with an accumulator; the function returns {node, acc}.