Packages
mdex
0.1.8
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
Current section
Files
Jump to
Current section
Files
lib/mdex.ex
defmodule MDEx do
@moduledoc """
A fast 100% CommonMark-compatible GitHub Flavored Markdown parser and formatter.
Use Rust's [comrak crate](https://crates.io/crates/comrak) under the hood.
"""
alias MDEx.Native
@doc """
Convert `markdown` to HTML.
## Examples
iex> MDEx.to_html("# MDEx")
"<h1>MDEx</h1>\\n"
iex> MDEx.to_html("Implemented with:\\n1. Elixir\\n2. Rust")
"<p>Implemented with:</p>\\n<ol>\\n<li>Elixir</li>\\n<li>Rust</li>\\n</ol>\\n"
"""
@spec to_html(String.t()) :: String.t()
def to_html(markdown) when is_binary(markdown) do
Native.to_html(markdown)
end
@doc """
Convert `markdown` to HTML with custom `opts`.
## Options
Accepts all available [Comrak Options](https://docs.rs/comrak/latest/comrak/struct.ComrakOptions.html) as keyword lists.
* `:extension` - https://docs.rs/comrak/latest/comrak/struct.ComrakExtensionOptions.html
* `:parse` - https://docs.rs/comrak/latest/comrak/struct.ComrakParseOptions.html
* `:render` - https://docs.rs/comrak/latest/comrak/struct.ComrakRenderOptions.html
* `:features` - see the available options below
### Features Options
* `:sanitize` (default `false`) - sanitize output using [ammonia](https://crates.io/crates/ammonia).\n Recommended if passing `render: [unsafe_: true]`
* `:syntax_highlight_theme` (default `"onedark"`) - syntax highlight code fences using [autumn](https://github.com/leandrocp/autumn/tree/main/native/inkjet_nif).
## Examples
iex> MDEx.to_html("# MDEx")
"<h1>MDEx</h1>\\n"
iex> MDEx.to_html("Implemented with:\\n1. Elixir\\n2. Rust")
"<p>Implemented with:</p>\\n<ol>\\n<li>Elixir</li>\\n<li>Rust</li>\\n</ol>\\n"
iex> MDEx.to_html("Hello ~world~ there", extension: [strikethrough: true])
"<p>Hello <del>world</del> there</p>\\n"
iex> MDEx.to_html("<marquee>visit https://https://beaconcms.org</marquee>", extension: [autolink: true], render: [unsafe_: true])
"<p><marquee>visit <a href=\\"https://https://beaconcms.org\\">https://https://beaconcms.org</a></marquee></p>\\n"
iex> MDEx.to_html("# Title with <script>console.log('dangerous script')</script>", render: [unsafe_: true], features: [sanitize: true])
"<h1>Title with </h1>\\n"
"""
@spec to_html(String.t(), keyword()) :: String.t()
def to_html(markdown, opts) when is_binary(markdown) do
extension = Keyword.get(opts, :extension, %{})
parse = Keyword.get(opts, :parse, %{})
render = Keyword.get(opts, :render, %{})
features = Keyword.get(opts, :features, %{})
opts = %MDEx.Options{
extension: struct(MDEx.ExtensionOptions, extension),
parse: struct(MDEx.ParseOptions, parse),
render: struct(MDEx.RenderOptions, render),
features: struct(MDEx.FeaturesOptions, features)
}
Native.to_html_with_options(markdown, opts)
end
end