Packages
mdex
0.9.3
0.13.3
0.13.2
0.13.1
0.13.0
0.12.5
retired
0.12.4
0.12.3
0.12.2
0.12.1
0.12.0
0.11.7
0.11.6
0.11.5
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.0
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.2
0.6.1
0.6.0
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.0
0.1.18
0.1.17
0.1.16
0.1.15
0.1.14
0.1.13
0.1.12
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Fast and extensible Markdown for Elixir
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
README.md
# MDEx<!-- MDOC --><div align="center"> <img src="https://raw.githubusercontent.com/leandrocp/mdex/main/assets/images/mdex_logo.png" width="360" alt="MDEx logo" /> <br> <a href="https://hex.pm/packages/mdex"> <img alt="Hex Version" src="https://img.shields.io/hexpm/v/mdex"> </a> <a href="https://hexdocs.pm/mdex"> <img alt="Hex Docs" src="http://img.shields.io/badge/hex.pm-docs-green.svg?style=flat"> </a> <a href="https://opensource.org/licenses/MIT"> <img alt="MIT" src="https://img.shields.io/hexpm/l/mdex"> </a> <p align="center">Fast and Extensible Markdown for Elixir.</p></div>## Features- Fast- Compliant with the [CommonMark spec](https://commonmark.org)- [Plugins](https://hexdocs.pm/mdex/plugins.html)- Formats: - Markdown (CommonMark) - HTML - JSON - XML - [Quill Delta](https://quilljs.com/docs/delta/)- Floki-like [Document AST](https://hexdocs.pm/mdex/MDEx.Document.html)- Req-like [Document pipeline API](https://hexdocs.pm/mdex/MDEx.Document.html)- [GitHub Flavored Markdown](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax)- Discord and GitLab Flavored-ish Markdown- Wiki-style links- [Streaming](https://hexdocs.pm/mdex/MDEx.Document.html#module-streaming) incomplete fragments- [Emoji](https://www.webfx.com/tools/emoji-cheat-sheet) shortcodes- Built-in [Syntax Highlighting](https://autumnus.dev) for code blocks- [Code Block Decorators](https://hexdocs.pm/mdex/code_block_decorators-2.html)- HTML sanitization- [~MD Sigil](https://hexdocs.pm/mdex/MDEx.Sigil.html) for Markdown, HTML, JSON, XML, and Quill Delta## ExamplesLivebook examples are available at [Pages / Examples](https://hexdocs.pm/mdex/gfm.html)## InstallationAdd `:mdex` dependency:```elixirdef deps do [ {:mdex, "~> 0.8"} ]end```Or use [Igniter](https://hexdocs.pm/igniter):```shmix igniter.install mdex```## Usage```elixiriex> MDEx.to_html!("# Hello :smile:", extension: [shortcodes: true])"<h1>Hello π</h1>"``````elixiriex> MDEx.new(markdown: "# Hello :smile:", extension: [shortcodes: true]) |> MDEx.to_html!()"<h1>Hello π</h1>"``````elixiriex> import MDEx.Sigiliex> ~MD[# Hello :smile:]HTML"<h1>Hello π</h1>"``````elixiriex> import MDEx.Sigiliex> ~MD[# Hello :smile:]#MDEx.Document(3 nodes)<βββ 1 [heading] level: 1, setext: falseβ βββ 2 [text] literal: "Hello "β βββ 3 [short_code] code: "smile", emoji: "π">``````elixiriex> MDEx.new(streaming: true)...> |> MDEx.Document.put_markdown("**Install")...> |> MDEx.to_html!()"<p><strong>Install</strong></p>"```## FoundationThe library is built on top of:- [comrak](https://crates.io/crates/comrak) - a fast Rust port of [GitHub's CommonMark parser](https://github.com/github/cmark-gfm)- [ammonia](https://crates.io/crates/ammonia) for HTML Sanitization- [autumnus](https://crates.io/crates/autumnus) for Syntax Highlighting<!-- MDOC -->## Used By- [BeaconCMS](https://github.com/BeaconCMS/beacon)- [Tableau](https://github.com/elixir-tools/tableau)- [Bonfire](https://github.com/bonfire-networks/bonfire-app)- [00](https://github.com/technomancy-dev/00)- [Plural Console](https://github.com/pluralsh/console)- [Exmeralda](https://github.com/bitcrowd/exmeralda)- [Algora](https://github.com/algora-io/algora)- [Ash AI](https://github.com/ash-project/ash_ai)- [Canada Navigator](https://github.com/canada-ca/navigator)- And [more...](https://github.com/search?q=lang%3Aelixir+%3Amdex&type=code)_Are you using MDEx and want to list your project here? Please send a PR!_## MotivationMDEx 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.- [earmark](https://hex.pm/packages/earmark) is extensible but [can't parse](https://github.com/RobertDober/earmark_parser/issues/126) all kinds of documents and is slow to convert hundreds of markdowns.- [md](https://hex.pm/packages/md) is very extensible but the doc says "If one needs to perfectly parse the common markdown, Md is probably not the correct choice" and CommonMark was a requirement to parse many existing files.- [markdown](https://hex.pm/packages/markdown) is not precompiled and has not received updates in a while.- [cmark](https://hex.pm/packages/cmark) is a fast CommonMark parser but it requires compiling the C library, is hard to extend, and was archived on Apr 2024.## 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 support2. Possible with [earmark_reversal](https://hex.pm/packages/earmark_reversal)3. GitHub Flavored Markdown4. GitLab Flavored Markdown5. Discord Flavored Markdown## BenchmarkA [benchmark](benchmark.exs) 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 msmd 475.87 2.10 ms Β±3.35% 2.09 ms 2.27 msearmark 142.21 7.03 ms Β±2.84% 6.98 ms 7.68 msComparison:mdex 4546.79md 475.87 - 9.55x slower +1.88 msearmark 142.21 - 31.97x slower +6.81 msMemory usage statistics:Comparison:mdex 0.152 MBmd 6.37 MB - 41.86x memory usage +6.21 MBearmark 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- [comrak](https://crates.io/crates/comrak) crate for all the heavy work on parsing Markdown and rendering HTML- [Floki](https://hex.pm/packages/floki) for the AST- [Req](https://hex.pm/packages/req) for the pipeline API- Logo based on [markdown-mark](https://github.com/dcurtis/markdown-mark)