Markup Core
The core of markup. Primitives and glue code mostly. This doesn't do any parsing, you want a language package:
(markup_markdown,
markup_djot,
markup_carve)
This package provides:
Markup, the entry points (parse_document/2,to_html/2,convert/3,safe_html/2,stream/2,to_heex/2)Markup.Document, the node tree and plugin pipelineMarkup.Node, node structs and their kindsMarkup.Sanitize, the ammonia option schema every language usesMarkup.Language, the behaviour a language package implements- the
~MUsigil and HEEx rendering
Installation
You install markup next to at least one language:
def deps do
[
{:markup, "~> 0.1"},
{:markup_djot, "~> 0.1"}
]
end
Languages register themselves, so if you only have one installed, you don't need any
config. With several, pass language: at each call, or set a default:
config :markup, default_language: :markdown
Documents and selectors
All languages share Markup.Document. It implements Access and Enumerable (a
pre-order walk), so you can query and edit the tree directly:
doc = Markup.parse_document!("# Title\n\nSome **bold**", language: :markdown)
doc[:strong]
# => [%Markup.Markdown.Strong{...}]
doc
|> Markup.Document.update_nodes(:text, &%{&1 | literal: String.upcase(&1.literal)})
|> Markup.convert!(:markdown)
Nodes are language specific structs, but the ones with a common counterpart declare a
kind (Markup.Node.kinds/0). A bare atom selector that names a kind, like :emphasis
or :strikethrough, matches that kind in any language, so :emphasis finds
Markup.Markdown.Emph and Markup.Djot.Emphasis alike. Language specific things,
like Carve and Djot's custom spans, you reach through that language's own nodes.
Nodes of the same kind use the same field names (nodes, literal, url), so a
plugin can usually be written once and work in every language.
Selectors can also be a node module, a struct pattern (%Markup.Djot.Heading{level: 2}),
a 1-arity function, or an integer index into the walk.
Plugins
A plugin is a module with attach/2 that registers options, puts options and appends
steps:
defmodule ShoutPlugin do
alias Markup.Document
def attach(doc, opts) do
doc
|> Document.register_options([:shout])
|> Document.put_options(opts)
|> Document.append_steps(shout: &shout/1)
end
defp shout(doc) do
if Document.get_option(doc, :shout),
do: Document.update_nodes(doc, :text, &%{&1 | literal: String.upcase(&1.literal)}),
else: doc
end
end
Markup.to_html!("hi", language: :djot, plugins: [{ShoutPlugin, shout: true}])
# => "<p>HI</p>\n"
Steps can read private keys with Markup.Document.get_private/3: :heex (true
while rendering HEEx) and :stream (%{id: id} on documents from Markup.stream/2).
To inject HTML, build it with Markup.safe_html/2 and put it in the language's raw
node (the :raw kind).
Options
Core owns three options, and every language accepts them: :plugins, :assigns and
:sanitize. sanitize: [] turns on ammonia with the language's defaults, any keys you
pass merge over them, and nil turns it off; Markup.Sanitize lists the keys.
Everything else (render:, parse:, syntax_highlight: and so on) belongs to the
language, so check its package docs.
Sigil
use Markup imports ~MU, which is evaluated at compile time. The modifier is an
optional lowercase language id followed by an optional uppercase format:
use Markup
~MU"*a*"djotHTML # HTML string
~MU"# a"markdown # %Markup.Document{}
~MU"/a/"carveMD # Markdown output
The other formats are HEEX, XML, TEXT, ANSI and CARVE, as long as the
language supports them. use Markup, language: :carve lets you omit the id.
HEEx
With {:phoenix_live_view, "~> 1.2"}, the Markup.to_heex/2 macros and ~MU"..."HEEX
render components and {@expr} expressions:
use Markup, language: :djot
import Phoenix.Component
def render(assigns) do
~MU"""
Hello {@name}
<.link href="/x">go</.link>
"""HEEX
end
Streaming
Markup.stream/2 turns chunked input (an LLM response, say) into per-block documents:
chunks
|> Markup.stream(language: :djot)
|> Enum.each(fn {id, doc} -> send(self(), {:block, id, Markup.to_html!(doc)}) end)
Each emission replaces the block with the same id.
Writing a language
A language is a module that implements Markup.Language. You'll need the id, a node
namespace, output formats, an options schema, parse/2, render/3, sanitize defaults
and safe_html/3. A few callbacks are optional (merge_option/3, the
render_source/4 fast path, stream_blocks/2 and heex_prepare/1); the behaviour
docs cover all of them.
Define node structs with use Markup.Node, passing kind: for nodes that have a
common counterpart:
defmodule MyLang.Strong do
use Markup.Node, children: [:nodes], kind: :strong
defstruct nodes: []
end
When you render, sanitize with
Markup.Sanitize.prepare(Markup.Document.get_option(doc, :sanitize)). Some option
combinations make ammonia panic, and prepare/1 sorts those out first.
Then register the module in your package's mix.exs:
def application, do: [env: [markup_languages: [MyLang]]]