Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update mermaid theme on theme change #9307

Merged
merged 7 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions templates/modern/src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { breakWord, meta, loc, options } from './helper'
import AnchorJs from 'anchor-js'
import { html, render } from 'lit-html'
import { getTheme } from './theme'
import { getTheme, onThemeChange } from './theme'

/**
* Initialize markdown rendering.
Expand All @@ -23,6 +23,8 @@ export async function renderMarkdown() {
renderMath(),
renderMermaid()
])

onThemeChange(renderMermaid)
}

async function renderMath() {
Expand All @@ -32,33 +34,41 @@ async function renderMath() {
}
}

let mermaidRenderCount = 0

/**
* Render mermaid diagrams.
*/
async function renderMermaid() {
console.log('asdf')
const diagrams = document.querySelectorAll<HTMLElement>('pre code.lang-mermaid')
if (diagrams.length <= 0) {
const processedDiagrams = document.querySelectorAll<HTMLElement>('pre.mermaid[data-mermaid]')
if (diagrams.length <= 0 && processedDiagrams.length <= 0) {
return
}

const { default: mermaid } = await import('mermaid')
const theme = getTheme() === 'dark' ? 'dark' : 'default'

// Turn off deterministic ids on re-render
const deterministicIds = mermaidRenderCount === 0
const { mermaid: mermaidOptions } = await options()
mermaid.initialize(Object.assign({ startOnLoad: false, deterministicIds, theme }, mermaidOptions))
mermaidRenderCount++
mermaid.initialize(Object.assign({ startOnLoad: false, theme }, mermaidOptions))

const nodes = []
diagrams.forEach(e => {
// Rerender when elements becomes visible due to https://github.com/mermaid-js/mermaid/issues/1846
if (e.offsetParent) {
nodes.push(e.parentElement)
const code = e.innerHTML
e.parentElement.classList.add('mermaid')
e.parentElement.innerHTML = e.innerHTML
e.parentElement.setAttribute('data-mermaid', code)
e.parentElement.innerHTML = code
}
})

processedDiagrams.forEach(e => {
if (e.offsetParent) {
e.removeAttribute('data-processed')
e.innerHTML = e.getAttribute('data-mermaid')
nodes.push(e)
}
})

Expand Down
5 changes: 5 additions & 0 deletions templates/modern/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export async function initTheme() {
setTheme(await getDefaultTheme())
}

export function onThemeChange(callback: (theme: 'light' | 'dark') => void) {
return new MutationObserver(() => callback(getTheme()))
.observe(document.documentElement, { attributes: true, attributeFilter: ['data-bs-theme'] })
}

export function getTheme(): 'light' | 'dark' {
return document.documentElement.getAttribute('data-bs-theme') as 'light' | 'dark'
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading