Markup.Djot (Markup.Djot v0.1.0)

Copy Markdown View Source

Djot for Markup, backed by the Rust jotdown crate.

Add {:markup_djot, "~> 0.1"} to your deps and use the Markup API with language: :djot (you can leave that off if it's the only language installed).

Examples

iex> Markup.to_html("Hello *world*!", language: :djot)
{:ok, "<p>Hello <strong>world</strong>!</p>\n"}

iex> Markup.to_html!("Hello", language: :djot, render: [renderer: :minified])
"<p>Hello</p>"

iex> {:ok, doc} = Markup.parse_document("Hello *world*", language: :djot)
iex> doc.nodes
[%Markup.Djot.Paragraph{nodes: [%Markup.Djot.Text{literal: "Hello "}, %Markup.Djot.Strong{nodes: [%Markup.Djot.Text{literal: "world"}]}]}]
iex> doc[:text] |> Enum.map(& &1.literal)
["Hello ", "world"]

iex> Markup.safe_html("<script>x</script><b onclick=\"y\">hi</b>", language: :djot)
"<b>hi</b>"

iex> Markup.safe_html("<a>&</a>", sanitize: nil, escape: true, language: :djot)
"&lt;a&gt;&amp;&lt;/a&gt;"

iex> Markup.safe_html("<pre>A{b}</pre>", sanitize: nil, heex: true, language: :djot)
"<pre>A&lbrace;b&rbrace;</pre>"

iex> doc = Markup.parse_document!("Hello *world*", language: :djot)
iex> doc[Markup.Djot.Strong]
[%Markup.Djot.Strong{nodes: [%Markup.Djot.Text{literal: "world"}]}]

iex> import Markup.Sigil
iex> ~MU"*a*"djotHTML
"<p><strong>a</strong></p>\n"

Options

  • :render (keyword/0) - jotdown renderer options. The default value is [].

    • :renderer - Which jotdown HTML renderer to use. The default value is :default.

    • :indent_string (String.t/0) - Indentation per level for the :indented renderer. The default value is "\t".

    • :indent_initial_level (non_neg_integer/0) - Starting indentation level for the :indented renderer. The default value is 0.

  • :syntax_highlight - Code block highlighting, e.g. [engine: :syntect] or [engine: :lumis]. nil disables it. The default value is nil.

Code block decorators

Block attributes on a code block control highlighting and are never rendered:

{theme=github_dark pre_class="a b" highlight_lines="1,3-5" highlight_lines_style="background: #ff0" highlight_lines_class=hl include_highlights=true}
``` elixir
IO.puts("hi")
```

Without syntax_highlight:, pre_class becomes a class on <pre> and the other decorators are ignored.

HEEx

With Markup.to_heex/2 or ~MU"..."djotHEEX, HEEx tags (<.link>, <Mod.fun>, <:slot>), {@expr} expressions and <%= %> blocks are recognized outside code; djot attribute syntax like {.class} is left alone. Explicit `...`{=heex} and ```=heex raw blocks also work.

Streaming

Markup.stream/2 flattens sections so each heading streams as its own block.

iex> ["# Hel", "lo\n\nPara"]
...> |> Markup.stream(language: :djot)
...> |> Enum.map(fn {id, doc} -> {id, Markup.to_html!(doc)} end)
[{0, "<h1 id=\"Hel\">Hel</h1>\n"}, {0, "<h1 id=\"Hello\">Hello</h1>\n"}, {1, "<p>Para</p>\n"}]