Mix.install([{:mdex, path: ".."}])
defmodule AlertsExample do
import MDEx.Sigil
def run do
opts = [
extension: [autolink: true],
render: [unsafe_: true]
]
markdown = ~M"""
# 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]
> ### Render this note block as Markdown
> See more at https://github.com/leandrocp/mdex
> [!CAUTION]
>
Render this caution block as HTML
> See more at MDEx repo
"""
note_block = fn content ->
content = MDEx.to_html!(content, opts)
alert = """
"""
%MDEx.HtmlBlock{literal: alert, nodes: []}
end
caution_block = fn content ->
content = MDEx.to_html!(content, opts)
alert = """
"""
%MDEx.HtmlBlock{literal: alert, nodes: []}
end
tailwind_node =
MDEx.parse_fragment!("""
""")
html =
markdown
|> MDEx.traverse_and_update(fn
# inject tailwind
%MDEx.Document{nodes: nodes} = document ->
nodes = [tailwind_node | nodes]
%{document | nodes: nodes}
# inject a html block to render a note alert
%MDEx.BlockQuote{nodes: [%MDEx.Paragraph{nodes: [%MDEx.Text{literal: "[!NOTE]"}]} | content]} ->
note_block.(content)
# inject a html block to render a caution alert
%MDEx.BlockQuote{nodes: [%MDEx.Paragraph{nodes: [%MDEx.Text{literal: "[!CAUTION]"}]} | content]} ->
caution_block.(content)
node ->
node
end)
|> MDEx.to_html!(opts)
File.write!("alerts.html", html)
IO.puts(html)
end
end
AlertsExample.run()