From 4d2f13c0d7a6e0220bb6c980e57b8f2ec7d9c2fd Mon Sep 17 00:00:00 2001
From: Cotes Chung <11371340+cotes2020@users.noreply.github.com>
Date: Thu, 10 Dec 2020 02:42:46 +0800
Subject: [PATCH] Integrate with mermaid-js
---
_includes/mermaid.html | 28 ++++++++++++++++++++++++++++
_includes/mode-toggle.html | 37 +++++++++++++++++++++++++++++++++++--
_layouts/default.html | 4 ++++
3 files changed, 67 insertions(+), 2 deletions(-)
create mode 100644 _includes/mermaid.html
diff --git a/_includes/mermaid.html b/_includes/mermaid.html
new file mode 100644
index 0000000..7c63656
--- /dev/null
+++ b/_includes/mermaid.html
@@ -0,0 +1,28 @@
+
+
+
diff --git a/_includes/mode-toggle.html b/_includes/mode-toggle.html
index e9f908c..1ae94ec 100644
--- a/_includes/mode-toggle.html
+++ b/_includes/mode-toggle.html
@@ -33,8 +33,8 @@
/* always follow the system prefers */
this.sysDarkPrefers.addListener(function() {
- if (self.mode != null) {
- if (self.mode == ModeToggle.DARK_MODE) {
+ if (self.hasMode) {
+ if (self.isDarkMode) {
if (!self.isSysDarkPrefer) {
self.setDark();
}
@@ -47,6 +47,8 @@
self.clearMode();
}
+
+ self.updateMermaid();
});
} /* constructor() */
@@ -79,6 +81,33 @@
get mode() { return sessionStorage.getItem(ModeToggle.MODE_KEY); }
+ /* get the current mode on screen */
+ get modeStatus() {
+ if (this.isDarkMode
+ || (!this.hasMode && this.isSysDarkPrefer) ) {
+ return ModeToggle.DARK_MODE;
+ } else {
+ return ModeToggle.LIGHT_MODE;
+ }
+ }
+
+ updateMermaid() {
+ if (mermaid !== undefined) {
+ let expectedTheme = (this.modeStatus === ModeToggle.DARK_MODE? "dark" : "default");
+ let config = { theme: expectedTheme };
+
+ /* re-render the SVG › */
+ $(".mermaid").each(function() {
+ let svgCode = $(this).prev().children().html();
+ $(this).removeAttr("data-processed");
+ $(this).html(svgCode);
+ });
+
+ mermaid.initialize(config);
+ mermaid.init(undefined, ".mermaid");
+ }
+ }
+
flipMode() {
if (this.hasMode) {
if (this.isSysDarkPrefer) {
@@ -104,6 +133,8 @@
}
}
+ this.updateMermaid();
+
} /* flipMode() */
} /* ModeToggle */
@@ -111,7 +142,9 @@
let toggle = new ModeToggle();
$(".mode-toggle").click(function() {
+
toggle.flipMode();
+
});
diff --git a/_layouts/default.html b/_layouts/default.html
index dd4cab3..ad98149 100644
--- a/_layouts/default.html
+++ b/_layouts/default.html
@@ -40,6 +40,10 @@ layout: compress
+ {% if page.mermaid %}
+ {% include mermaid.html %}
+ {% endif %}
+