mirror of
https://github.com/nunocoracao/blowfish.git
synced 2026-01-30 16:31:52 +01:00
feat(mermaid): support dark mode
This commit is contained in:
@@ -22,6 +22,34 @@ if (document.documentElement.getAttribute("data-auto-appearance") === "true") {
|
||||
});
|
||||
}
|
||||
|
||||
// Mermaid dark mode support
|
||||
var updateMermaidTheme = () => {
|
||||
if (typeof mermaid !== 'undefined') {
|
||||
const isDark = document.documentElement.classList.contains("dark");
|
||||
|
||||
const mermaids = document.querySelectorAll('pre.mermaid');
|
||||
mermaids.forEach(e => {
|
||||
if (e.getAttribute('data-processed')) {
|
||||
// Already rendered, clean the processed attributes
|
||||
e.removeAttribute('data-processed');
|
||||
// Replace the rendered HTML with the stored text
|
||||
e.innerHTML = e.getAttribute('data-graph');
|
||||
} else {
|
||||
// First time, store the text
|
||||
e.setAttribute('data-graph', e.textContent);
|
||||
}
|
||||
});
|
||||
|
||||
if (isDark) {
|
||||
initMermaidDark();
|
||||
mermaid.run();
|
||||
} else {
|
||||
initMermaidLight();
|
||||
mermaid.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener("DOMContentLoaded", (event) => {
|
||||
const switcher = document.getElementById("appearance-switcher");
|
||||
const switcherMobile = document.getElementById("appearance-switcher-mobile");
|
||||
@@ -29,6 +57,9 @@ window.addEventListener("DOMContentLoaded", (event) => {
|
||||
updateMeta();
|
||||
this.updateLogo?.(getTargetAppearance());
|
||||
|
||||
// Initialize mermaid theme on page load
|
||||
updateMermaidTheme();
|
||||
|
||||
if (switcher) {
|
||||
switcher.addEventListener("click", () => {
|
||||
document.documentElement.classList.toggle("dark");
|
||||
@@ -38,6 +69,7 @@ window.addEventListener("DOMContentLoaded", (event) => {
|
||||
targetAppearance
|
||||
);
|
||||
updateMeta();
|
||||
updateMermaidTheme();
|
||||
this.updateLogo?.(targetAppearance);
|
||||
});
|
||||
switcher.addEventListener("contextmenu", (event) => {
|
||||
@@ -54,6 +86,7 @@ window.addEventListener("DOMContentLoaded", (event) => {
|
||||
targetAppearance
|
||||
);
|
||||
updateMeta();
|
||||
updateMermaidTheme();
|
||||
this.updateLogo?.(targetAppearance);
|
||||
});
|
||||
switcherMobile.addEventListener("contextmenu", (event) => {
|
||||
|
||||
@@ -2,30 +2,32 @@ function css(name) {
|
||||
return "rgb(" + getComputedStyle(document.documentElement).getPropertyValue(name) + ")";
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const mermaidDivs = document.querySelectorAll("div.mermaid");
|
||||
function initMermaidLight() {
|
||||
mermaid.initialize({
|
||||
theme: "base",
|
||||
themeVariables: {
|
||||
background: css("--color-neutral"),
|
||||
primaryColor: css("--color-primary-200"),
|
||||
secondaryColor: css("--color-secondary-200"),
|
||||
tertiaryColor: css("--color-neutral-100"),
|
||||
primaryBorderColor: css("--color-primary-400"),
|
||||
secondaryBorderColor: css("--color-secondary-400"),
|
||||
tertiaryBorderColor: css("--color-neutral-400"),
|
||||
lineColor: css("--color-neutral-600"),
|
||||
fontFamily:
|
||||
"ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,segoe ui,Roboto,helvetica neue,Arial,noto sans,sans-serif",
|
||||
fontSize: "16px",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
for (const div of mermaidDivs) {
|
||||
const preElement = div.querySelector("pre");
|
||||
if (preElement) {
|
||||
div.textContent = preElement.textContent;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
mermaid.initialize({
|
||||
theme: "base",
|
||||
themeVariables: {
|
||||
background: css("--color-neutral"),
|
||||
primaryColor: css("--color-primary-200"),
|
||||
secondaryColor: css("--color-secondary-200"),
|
||||
tertiaryColor: css("--color-neutral-100"),
|
||||
primaryBorderColor: css("--color-primary-400"),
|
||||
secondaryBorderColor: css("--color-secondary-400"),
|
||||
tertiaryBorderColor: css("--color-neutral-400"),
|
||||
lineColor: css("--color-neutral-600"),
|
||||
fontFamily:
|
||||
"ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,segoe ui,Roboto,helvetica neue,Arial,noto sans,sans-serif",
|
||||
fontSize: "16px",
|
||||
},
|
||||
});
|
||||
function initMermaidDark() {
|
||||
mermaid.initialize({
|
||||
theme: "dark",
|
||||
themeVariables: {
|
||||
fontFamily:
|
||||
"ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,segoe ui,Roboto,helvetica neue,Arial,noto sans,sans-serif",
|
||||
fontSize: "16px",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user