Skip to content

Commit

Permalink
use mermaid js
Browse files Browse the repository at this point in the history
  • Loading branch information
princemaple authored Oct 22, 2022
1 parent baa645d commit fd2952d
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,34 @@ defmodule DG.MixProject do
canonical: "http://hexdocs.pm/dg",
homepage_url: @source_url,
source_url: @source_url,
source_ref: "v#{@version}"
source_ref: "v#{@version}",
before_closing_body_tag: &before_closing_body_tag/1
]
end

defp before_closing_body_tag(:html) do
"""
<script src="https://unpkg.com/[email protected]/dist/mermaid.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
mermaid.initialize({ startOnLoad: false });
let id = 0;
for (const codeEl of document.querySelectorAll("pre code.mermaid")) {
const preEl = codeEl.parentElement;
const graphDefinition = codeEl.textContent;
const graphEl = document.createElement("div");
const graphId = "mermaid-graph-" + id++;
mermaid.render(graphId, graphDefinition, function (svgSource, bindListeners) {
graphEl.innerHTML = svgSource;
bindListeners && bindListeners(graphEl);
preEl.insertAdjacentElement("afterend", graphEl);
preEl.remove();
});
}
});
</script>
"""
end

defp before_closing_body_tag(_), do: ""
end

0 comments on commit fd2952d

Please sign in to comment.