Skip to content

Commit

Permalink
⬆️ Fix: bump Mermaid from 9.x to 10.x and fix error (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lruihao committed Mar 7, 2024
1 parent 063d8fa commit bf3ee63
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 1,616 deletions.
4 changes: 3 additions & 1 deletion assets/css/_shortcodes/_mermaid.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.mermaid {
text-align: center;
&:has(svg) {
text-align: center;
}

svg {
max-width: 100%;
Expand Down
3 changes: 0 additions & 3 deletions assets/data/cdn/jsdelivr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ libFiles:
# TODO update to 3.x
mapboxGLCSS: [email protected]/dist/mapbox-gl.css
mapboxGLJS: [email protected]/dist/mapbox-gl.js
# [email protected] https://github.com/mermaid-js/mermaid
# TODO bump Mermaid from 9.x to 10.x
mermaidJS: [email protected]/dist/mermaid.min.js
# [email protected] https://github.com/metowolf/MetingJS
metingJS: [email protected]/dist/Meting.min.js
# [email protected] https://github.com/fregante/object-fit-images
Expand Down
3 changes: 0 additions & 3 deletions assets/data/cdn/unpkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ libFiles:
# TODO update to 3.x
mapboxGLCSS: [email protected]/dist/mapbox-gl.css
mapboxGLJS: [email protected]/dist/mapbox-gl.js
# [email protected] https://github.com/mermaid-js/mermaid
# TODO bump Mermaid from 9.x to 10.x
mermaidJS: [email protected]/dist/mermaid.min.js
# [email protected] https://github.com/metowolf/MetingJS
metingJS: [email protected]/dist/Meting.min.js
# [email protected] https://github.com/fregante/object-fit-images
Expand Down
34 changes: 16 additions & 18 deletions assets/js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,24 +550,23 @@ class FixIt {
}
}

switchMermaidTheme(theme) {
const $mermaidElements = document.getElementsByClassName('mermaid');
if ($mermaidElements.length) {
// TODO perf
const themes = this.config.mermaid.themes ?? ['default', 'dark', 'neutral'];
mermaid.initialize({ startOnLoad: false, theme: theme ?? (this.isDark ? themes[1] : themes[0]), securityLevel: 'loose' });
this.util.forEach($mermaidElements, $mermaid => {
mermaid.render('svg-' + $mermaid.id, this.data[$mermaid.id], svgCode => {
$mermaid.innerHTML = svgCode;
}, $mermaid);
});
}
};

initMermaid() {
this.switchMermaidTheme();
this.switchThemeEventSet.add(() => { this.switchMermaidTheme(); });
this.beforeprintEventSet.add(() => { this.switchMermaidTheme('neutral'); });
if (!window.mermaid) {
return;
}
const themes = window.mermaid.themes ?? ['default', 'dark'];
window.mermaid.initialize({
securityLevel: 'loose',
startOnLoad: true,
theme: this.isDark ? themes[1] : themes[0],
});
this.switchThemeEventSet.add(() => {
// Mermaid does not provide a method for switching yet. When switching themes, refresh the page.
window.location.reload();
});
this.beforeprintEventSet.add(() => {
// Set the theme to neutral when printing.
});
}

initEcharts() {
Expand Down Expand Up @@ -1101,7 +1100,6 @@ class FixIt {
event();
}
this.initToc();
this.switchMermaidTheme();
this.initSearch();

const isMobile = this.util.isMobile()
Expand Down
1 change: 0 additions & 1 deletion assets/lib/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ [email protected] https://github.com/instantpage/instant.page
[email protected] https://github.com/KaTeX/KaTeX
[email protected] https://github.com/sachinchoolur/lightgallery
[email protected] https://github.com/mapbox/mapbox-gl-js
[email protected] https://github.com/mermaid-js/mermaid
[email protected] https://github.com/metowolf/MetingJS
[email protected] https://github.com/necolas/normalize.css
[email protected] https://github.com/fregante/object-fit-images [archived]
Expand Down
1,580 changes: 0 additions & 1,580 deletions assets/lib/mermaid/mermaid.min.js

This file was deleted.

7 changes: 4 additions & 3 deletions layouts/_default/_markup/render-codeblock-mermaid.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{- $id := dict "Content" (trim (partial "function/dos2unix.html" .Inner) "\n") "Scratch" .Page.Scratch | partial "function/id.html" -}}
<div class="mermaid" id="{{ $id }}"></div>
{{- .Page.Scratch.SetInMap "this" "mermaid" true -}}
<pre class="mermaid">
{{- .Inner | safeHTML }}
</pre>
{{- .Page.Store.Set "hasMermaid" true -}}
11 changes: 6 additions & 5 deletions layouts/partials/assets.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@
{{- end -}}

{{- /* mermaid */ -}}
{{- if (.HasShortcode "mermaid") | or (.Scratch.Get "this").mermaid -}}
{{- $source := $cdn.mermaidJS | default "lib/mermaid/mermaid.min.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- $mermaid := .Site.Params.mermaid -}}
{{- $config = dict "themes" $mermaid.themes | dict "mermaid" | merge $config -}}
{{- if .Store.Get "hasMermaid" -}}
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
window.mermaid = mermaid;
window.mermaid.themes = {{ .Site.Params.mermaid.themes }};
</script>
{{- end -}}

{{- /* ECharts */ -}}
Expand Down
6 changes: 4 additions & 2 deletions layouts/shortcodes/mermaid.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{{- $id := dict "Content" (trim (partial "function/dos2unix.html" .Inner) "\n") "Scratch" .Page.Scratch | partial "function/id.html" -}}
<div class="mermaid" id="{{ $id }}"></div>
<pre class="mermaid">
{{- .Inner | safeHTML }}
</pre>
{{- .Page.Store.Set "hasMermaid" true -}}
{{- /* EOF */ -}}

0 comments on commit bf3ee63

Please sign in to comment.