Mix.install([
{:mdex, path: ".."}
])
opts = [
extension: [autolink: true],
render: [unsafe_: true],
features: [sanitize: false]
]
markdown = """
# Alerts Example
In this example we'll render blockquotes as alerts.
Ref https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts
> [!NOTE]
> Useful information that users should know, even when skimming content.
> [!CAUTION]
> Advises about risks or negative outcomes of certain actions.
"""
note_html = fn content ->
"""
"""
end
caution_html = fn content ->
"""
"""
end
html =
markdown
|> MDEx.parse_document!(opts)
|> MDEx.traverse_and_update(fn
# inject tailwind
{"document", attrs, children} ->
tailwind =
MDEx.parse_document!("""
""")
{"document", attrs, children ++ tailwind}
# inject a html block to render a note alert
{"block_quote", _attrs, [{"paragraph", [], ["[!NOTE]", _note_attrs, note_content]}]} ->
alert = note_html.(note_content)
{"html_block", [{"literal", alert}], []}
# inject a html block to render a caution alert
{"block_quote", _attrs, [{"paragraph", [], ["[!CAUTION]", _note_attrs, note_content]}]} ->
alert = caution_html.(note_content)
{"html_block", [{"literal", alert}], []}
node ->
node
end)
|> MDEx.to_html!(opts)
File.write!("alerts.html", html)
IO.puts(html)