-
Notifications
You must be signed in to change notification settings - Fork 6
/
mdex.ex
80 lines (58 loc) · 3.05 KB
/
mdex.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
defmodule MDEx do
@external_resource "README.md"
@moduledoc "README.md"
|> File.read!()
|> String.split("<!-- MDOC -->")
|> Enum.fetch!(1)
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.Options.html) as keyword lists.
* `:extension` - https://docs.rs/comrak/latest/comrak/struct.ExtensionOptions.html
* `:parse` - https://docs.rs/comrak/latest/comrak/struct.ParseOptions.html
* `:render` - https://docs.rs/comrak/latest/comrak/struct.RenderOptions.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 themes](https://github.com/leandrocp/autumn/tree/main/priv/themes),
you should pass the filename without special chars and without extension, for example you should pass `syntax_highlight_theme: "adwaita_dark"` to use the [Adwaita Dark](https://github.com/leandrocp/autumn/blob/main/priv/themes/adwaita-dark.toml) theme.
## 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.Types.Options{
extension: struct(MDEx.Types.ExtensionOptions, extension),
parse: struct(MDEx.Types.ParseOptions, parse),
render: struct(MDEx.Types.RenderOptions, render),
features: struct(MDEx.Types.FeaturesOptions, features)
}
Native.to_html_with_options(markdown, opts)
end
end