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")
"
MDEx
\\n"
iex> MDEx.to_html("Implemented with:\\n1. Elixir\\n2. Rust")
"Implemented with:
\\n\\n- Elixir
\\n- Rust
\\n
\\n"
"""
def to_html(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
## Examples
iex> MDEx.to_html("# MDEx")
"MDEx
\\n"
iex> MDEx.to_html("Implemented with:\\n1. Elixir\\n2. Rust")
"Implemented with:
\\n\\n- Elixir
\\n- Rust
\\n
\\n"
iex> MDEx.to_html("Hello ~world~ there", extension: [strikethrough: true])
"Hello world there
\\n"
iex> MDEx.to_html("", extension: [autolink: true], render: [unsafe_: true])
"\\n"
"""
def to_html(markdown, opts) do
extension = Keyword.get(opts, :extension, %{})
parse = Keyword.get(opts, :parse, %{})
render = Keyword.get(opts, :render, %{})
options = %MDEx.Options{
extension: struct(MDEx.ExtensionOptions, extension),
parse: struct(MDEx.ParseOptions, parse),
render: struct(MDEx.RenderOptions, render)
}
Native.to_html_with_options(markdown, options)
end
end