Skip to content

Commit

Permalink
fix: add mermaid-mindmap support
Browse files Browse the repository at this point in the history
Adds and tests basic support for mermaid-mindmaps.

Issues
======

- mermaid-mindmap is currently loaded from a CDN, which isn't good.
  This means that mindmaps won't work if there is no internet
  connection (or if the network blocks CDNs).
- mermaid won't load mermaid-mindmap using mermaid.initThrowsErrors
  until PR mermaid-js/mermaid#3702
  is merged and released
  • Loading branch information
aloisklink committed Oct 20, 2022
1 parent fbd86cb commit af6ded8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,25 @@ async function renderMermaid (browser, definition, outputFormat, { viewport, bac
await page.$eval('body', (body, backgroundColor) => {
body.style.background = backgroundColor
}, backgroundColor)
const metadata = await page.$eval('#container', (container, definition, mermaidConfig, myCSS, backgroundColor) => {
const metadata = await page.$eval('#container', async (container, definition, mermaidConfig, myCSS, backgroundColor) => {
container.textContent = definition
window.mermaid.initialize(mermaidConfig)
await window.mermaid.initialize({
lazyLoadedDiagrams: [
// TODO: bundle mermaid-mindmap instead of downloading from CDN
'https://cdn.jsdelivr.net/npm/@mermaid-js/mermaid-mindmap@9.2.0-rc5/dist/mermaid-mindmap-detector.esm.mjs'
],
...mermaidConfig
})
// should throw an error if mmd diagram is invalid
try {
window.mermaid.initThrowsErrors(undefined, container)
await window.mermaid.initThrowsErrors(undefined, container)
} catch (error) {
if (error instanceof Error) {
// mermaid-js doesn't currently throws JS Errors, but let's leave this
// here in case it does in the future
throw error
} else {
throw new Error(error?.message ?? 'Unknown mermaid render error')
throw new Error(error?.message ?? `Unknown mermaid render error`)
}
}

Expand Down
14 changes: 14 additions & 0 deletions test-positive/mind-map.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
mindmap
root
child1((Circle))
grandchild 1
grandchild 2
child2(Round rectangle)
grandchild 3
grandchild 4
child3[Square]
grandchild 5
::icon(mdi mdi-fire)
gc6((grand<br/>child 6))
::icon(mdi mdi-fire)
gc7((grand<br/>grand<br/>child 8))

0 comments on commit af6ded8

Please sign in to comment.