-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: re-render mermaid when theme changed
- Loading branch information
Showing
4 changed files
with
39 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,32 @@ | ||
<script type="module"> | ||
import mermaid from "https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs"; | ||
mermaid.initialize({ startOnLoad: true }); | ||
|
||
const initMermaid = (isDark) => { | ||
mermaid.initialize({ | ||
theme: isDark ? "dark" : "default", | ||
startOnLoad: false, | ||
}); | ||
mermaid.run(); | ||
}; | ||
|
||
// Add data-mermaid-code attribute on all mermaid block. | ||
document.querySelectorAll(".mermaid").forEach((element) => { | ||
element.setAttribute("data-mermaid-code", element.innerHTML); | ||
}); | ||
|
||
// Re-render mermaid when theme changed. | ||
document.body.addEventListener("set-theme", (e) => { | ||
document.querySelectorAll(".mermaid").forEach((element) => { | ||
const mermaidCode = element.getAttribute("data-mermaid-code"); | ||
if (mermaidCode != null) { | ||
element.removeAttribute("data-processed"); | ||
element.innerHTML = mermaidCode; | ||
} | ||
}); | ||
initMermaid(e.detail == "dark"); | ||
}); | ||
|
||
// The es module script will load defer, so the localStorage should already be set by script in header. | ||
// If this script is loaded first, the script in header will dispatch an event to re-render mermaid, it's works too. | ||
initMermaid(localStorage.getItem("dark") === "true"); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<pre class="mermaid bg-white"> | ||
<pre class="mermaid bg-inherit"> | ||
{{ body | trim_start_matches(pat="```mermaid") | trim_start_matches(pat="```") | trim_end_matches(pat="```") }} | ||
</pre> |