Current section
Files
Jump to
Current section
Files
markup_djot
README.md
README.md
# Markup.Djot
<div align=center>
[](https://hex.pm/packages/markup_djot)
[](https://hexdocs.pm/markup_djot)
</div>
[Djot](https://djot.net) for [Markup](https://hex.pm/packages/markup), powered by the
[jotdown](https://crates.io/crates/jotdown) Rust crate. This package adds Djot's
parser, renderer, syntax highlighting and sanitize defaults; the document API,
plugins, HEEx, the sigil and streaming come from `markup` (see its docs).
| markup_djot | jotdown |
| --- | --- |
| 0.1.x | 0.10.0 |
The NIF depends on `jotdown = "0.10.0"` in `native/markup_djot_nif/Cargo.toml`, and
`Cargo.lock` holds the exact build.
## Installation
```elixir
def deps do
[
{:markup, "~> 0.1"},
{:markup_djot, "~> 0.1"}
]
end
```
The language id is `:djot`, and HTML is the only output.
```elixir
Markup.to_html("hello *world*!", language: :djot)
# => {:ok, "<p>hello <strong>world</strong>!</p>\n"}
Markup.to_html!("hello *world*!", language: :djot, render: [renderer: :minified])
# => "<p>hello <strong>world</strong>!</p>"
```
## Options
`render:` takes `renderer` (`:default`, `:minified`, `:indented`), `indent_string`
(default `"\t"`) and `indent_initial_level` (default `0`). See `Markup.Djot` for the
rest.
## Nodes
Nodes are `Markup.Djot.Section`, `Markup.Djot.Paragraph`, `Markup.Djot.Strong` and so
on. Attributes are a keyword list in `attributes`, and headings sit inside a
`Markup.Djot.Section` that owns their id.
```elixir
doc = Markup.parse_document!("# Hi\n\nSome *bold* text", language: :djot)
doc[:strong]
# => [%Markup.Djot.Strong{nodes: [%Markup.Djot.Text{literal: "bold"}], attributes: []}]
doc
|> Markup.Document.update_nodes(:strong, &%Markup.Djot.Emphasis{nodes: &1.nodes})
|> Markup.to_html!()
# => "<section id=\"Hi\">\n<h1>Hi</h1>\n<p>Some <em>bold</em> text</p>\n</section>\n"
```
Plugins inject HTML with `Markup.Djot.Raw` nodes, built with `Markup.safe_html/2`:
```elixir
Document.update_nodes(doc, %Markup.Djot.CodeBlock{language: "diagram"}, fn block ->
body = Markup.safe_html(block.literal, sanitize: nil, escape: true, language: :djot)
%Markup.Djot.Raw{literal: ~s(<pre class="diagram">#{body}</pre>)}
end)
```
## Syntax highlighting
The `:syntect` engine is built in. [Lumis](https://hex.pm/packages/lumis) needs
`{:lumis, "~> 0.10"}` plus a parser package such as `{:lumis_wasm_elixir, "~> 0.26"}`.
```elixir
Markup.to_html!(source, language: :djot, syntax_highlight: [engine: :syntect, opts: [theme: "Dracula"]])
Markup.to_html!(source, language: :djot, syntax_highlight: [engine: :lumis, opts: [formatter: {:html_inline, theme: "onedark"}]])
```
Code blocks take decorators as djot block attributes:
````djot
{theme=Dracula pre_class="a b" highlight_lines="1,3-5" highlight_lines_class=hl}
``` elixir
IO.puts("hi")
```
````
| Decorator | Effect |
| --- | --- |
| `theme` | Overrides the configured theme (syntect, Lumis `:html_inline` / `:terminal`) |
| `pre_class` | Extra classes on `<pre>` |
| `highlight_lines` | Lines to highlight: comma-separated `N` or `N-M`; invalid entries are skipped |
| `highlight_lines_style` | Inline style for highlighted lines |
| `highlight_lines_class` | Extra class for highlighted lines |
| `include_highlights` | `true` adds a `data-highlight` attribute with the scope name (Lumis `:html_inline` / `:html_multi_themes` only) |
Decorators never show up in the output. Without `syntax_highlight:`, `pre_class` still
becomes a class on `<pre>`.
## Sanitization
`sanitize: []` uses `Markup.Djot.default_sanitize_options/0`, which keeps djot's own
section, footnote, task-list and alignment markup.
## HEEx
In HEEx templates, HEEx tags (`<.link>`, `<Mod.fun>`, `<:slot>`), `{@expr}` expressions
and `<%= %>` blocks are recognized outside code. Djot attribute syntax like `{.class}`
is left alone, and explicit `` `...`{=heex} `` and ```` ```=heex ```` raw blocks work too.
## Streaming
Streaming flattens sections, so each heading streams as its own block.