Skip to content

Commit

Permalink
fix: use selector loop to manually render all charts on a page (#765)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys committed Jan 9, 2024
1 parent 46a170c commit ff6e9ed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"katex": "0.16.9",
"lodash": "4.17.21",
"mermaid": "10.6.1",
"store2": "2.14.2"
"store2": "2.14.2",
"uuid": "9.0.1"
},
"devDependencies": {
"@babel/eslint-parser": "7.23.3",
Expand Down
29 changes: 19 additions & 10 deletions src/js/mermaid.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const Storage = require("store2")
const { v4: uuidv4 } = require("uuid")
const { COLOR_THEME_DARK, THEME, COLOR_THEME_AUTO } = require("./config.js")

import mermaid from "mermaid"

document.addEventListener("DOMContentLoaded", function (event) {
let lstore = Storage.namespace(THEME)
let currentMode = lstore.get("color-theme") || COLOR_THEME_AUTO
Expand All @@ -16,15 +19,21 @@ document.addEventListener("DOMContentLoaded", function (event) {
theme = "dark"
}

import("mermaid")
.then(({ default: md }) => {
md.initialize({
flowchart: { useMaxWidth: true },
theme: theme,
themeVariables: {
darkMode: darkMode
}
})
mermaid.initialize({
startOnLoad: false,
flowchart: { useMaxWidth: true },
theme: theme,
themeVariables: {
darkMode: darkMode
}
})

document.querySelectorAll(".mermaid").forEach(function (el) {
let id = "graph-" + uuidv4()

mermaid.render(id, el.innerText).then(({ svg, bindFunctions }) => {
el.innerHTML = svg
bindFunctions?.(el)
})
.catch((error) => console.error(error))
})
})

0 comments on commit ff6e9ed

Please sign in to comment.