Skip to content

paradox460/djot

Repository files navigation

Djot

Hex.pm

A fast Djot parser and formatter for Elixir.

Djot parsing and transformations powered by the jotdown rust crate.

Usage

Simply call to_html on a string that contains Djot data, and you'll get back a html fragment.

Djot.to_html("hello *world*!")
# => {:ok, "<p>hello <strong>world</strong>!</p>\n"}

Sigil

If you want to write Djot contents inline in an Elixir program, you can use the provided sigil

import Djot.Sigil

~d"""
This is a Djot document. \
It will be compiled to HTML automatically, at compile time!

Here's some display math, thank you djot!

$$`
\frac{-b \pm \sqrt{b^2 - 4 a c}}{2 a}
`
"""
# => "<p>This is a Djot document. <br>\nIt will be compiled to HTML automatically, at compile time!</p>\n<p><span class=\"math display\">\\[\n\\frac{-b \\pm \\sqrt{b^2 - 4 a c}}{2 a}\n\\]</span></p>\n"

This can be useful when paired with tools such as the Tableau SSG

Options

The Djot renderer accepts a few options:

Option Values Description
renderer :default, :minified, :indented Which particular output renderer to use
indent_string String String which is used to indent lines when using the :indented renderer
indent_initial_level Integer How deep the initial indent starts at

These are passed to the Djot.to_html calls as follows:

Djot.to_html(document, renderer: :indented, indent_string: "    ", initial_indent_level: 1)

If you are using the bang variant of to_html/2, substitute it where appropriate

Installation

Add :djot as a dependency:

def deps do
[
  {:djot, "~> 0.1.0"}
]

See Also

If you want something that runs on pure BEAM code, checkout jot, written in Gleam.

This package is inspired by the excellent mdex package.

There is no syntax highlighting out of the box with this package. If you want server-side highlighting, I recommend autumn.