MDEx

MDEx logo
Hex VersionHex DocsMIT

Fast and Extensible Markdown for Elixir.

Features

Examples

Livebook examples are available at Pages / Examples

Installation

Add :mdex dependency:

def deps do
  [
    {:mdex, "~> 0.8"}
  ]
end

Or use Igniter:

mix igniter.install mdex

Usage

iex> MDEx.to_html!("# Hello :smile:", extension: [shortcodes: true])
"<h1>Hello πŸ˜„</h1>"
iex> MDEx.new(markdown: "# Hello :smile:", extension: [shortcodes: true]) |> MDEx.to_html!()
"<h1>Hello πŸ˜„</h1>"
iex> import MDEx.Sigil
iex> ~MD[# Hello :smile:]HTML
"<h1>Hello πŸ˜„</h1>"
iex> import MDEx.Sigil
iex> ~MD[# Hello :smile:]
#MDEx.Document(3 nodes)<
β”œβ”€β”€ 1 [heading] level: 1, setext: false
β”‚   β”œβ”€β”€ 2 [text] literal: "Hello "
β”‚   └── 3 [short_code] code: "smile", emoji: "πŸ˜„"
>
iex> MDEx.new(streaming: true)
...> |> MDEx.Document.put_markdown("**Install")
...> |> MDEx.to_html!()
"<p><strong>Install</strong></p>"

Foundation

The library is built on top of:

Used By

Are you using MDEx and want to list your project here? Please send a PR!

Motivation

MDEx was born out of the necessity of parsing CommonMark files, to parse hundreds of files quickly, and to be easily extensible by consumers of the library.

Comparison

Feature MDEx Earmark md cmark
Active βœ… βœ… βœ… ❌
Pure Elixir ❌ βœ… βœ… ❌
Extensible βœ… βœ… βœ… ❌
Syntax Highlighting βœ… ❌ ❌ ❌
Code Block Decorators βœ… ❌ ❌ ❌
Streaming (fragments) βœ… ❌ ❌ ❌
AST βœ… βœ… βœ… ❌
AST to Markdown βœ… ⚠️² ❌ ❌
To HTML βœ… βœ… βœ… βœ…
To JSON βœ… ❌ ❌ ❌
To XML βœ… ❌ ❌ βœ…
To Manpage ❌ ❌ ❌ βœ…
To LaTeX ❌ ❌ ❌ βœ…
To Quill Delta βœ… ❌ ❌ ❌
Emoji βœ… ❌ ❌ ❌
GFMΒ³ βœ… βœ… ❌ ❌
GitLab⁴ ⚠️¹ ❌ ❌ ❌
Discord⁡ ⚠️¹ ❌ ❌ ❌
  1. Partial support
  2. Possible with earmark_reversal
  3. GitHub Flavored Markdown
  4. GitLab Flavored Markdown
  5. Discord Flavored Markdown

Benchmark

A benchmark is available to compare existing libs:

Name              ips        average  deviation         median         99th %
mdex          4546.79        0.22 ms     Β±6.19%        0.22 ms        0.27 ms
md             475.87        2.10 ms     Β±3.35%        2.09 ms        2.27 ms
earmark        142.21        7.03 ms     Β±2.84%        6.98 ms        7.68 ms

Comparison:
mdex          4546.79
md             475.87 - 9.55x slower +1.88 ms
earmark        142.21 - 31.97x slower +6.81 ms

Memory usage statistics:

Comparison:
mdex           0.152 MB
md              6.37 MB - 41.86x memory usage +6.21 MB
earmark         4.34 MB - 28.51x memory usage +4.18 MB

The most performance gain is using the ~MD sigil to compile the Markdown instead of parsing it at runtime, prefer using it when possible.

To finish, a friendly reminder that all libs have their own strengths and trade-offs so use the one that better suits your needs.

Acknowledgements