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

Use mermaid.render() instead of mermaid.run() API to avoid removing whitespace from diagrams #536

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
2 changes: 1 addition & 1 deletion src-test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ describe("NodeJS API (import ... from '@mermaid-js/mermaid-cli')", () => {
const invalidMMDInput = 'this is not a valid mermaid file'
expect(
parseMMD(browser, invalidMMDInput, 'svg')
).rejects.toThrow('Evaluation failed: Error: No diagram type detected matching given configuration for text: this is not a valid mermaid file')
).rejects.toThrow('Evaluation failed: UnknownDiagramError: No diagram type detected matching given configuration for text: this is not a valid mermaid file')
})

describe.each(workflows)('testing workflow %s', (workflow) => {
Expand Down
41 changes: 4 additions & 37 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,6 @@ async function renderMermaid (browser, definition, outputFormat, { viewport, bac
body.style.background = backgroundColor
}, backgroundColor)
const metadata = await page.$eval('#container', async (container, definition, mermaidConfig, myCSS, backgroundColor) => {
/**
* Checks to see if the given object is one of Mermaid's DetailedErrors.
*
* @param {unknown} error - The error to check
* @returns {error is import("mermaid").DetailedError} Returns `true` is the `error`
* is a `Mermaid.DetailedError`.
* @see https://github.com/mermaid-js/mermaid/blob/v10.0.1/packages/mermaid/src/utils.ts#L927-L930
*/
function isDetailedError (error) {
return typeof error === 'object' && error !== null && 'str' in error
}

container.textContent = definition

/**
* @typedef {Object} GlobalThisWithMermaid
* We've already imported these modules in our `index.html` file, so that they
Expand All @@ -261,29 +247,10 @@ async function renderMermaid (browser, definition, outputFormat, { viewport, bac
*/
const { mermaid } = /** @type {GlobalThisWithMermaid & typeof globalThis} */ (globalThis)

mermaid.initialize(mermaidConfig)
mermaid.initialize({ startOnLoad: false, ...mermaidConfig })
// should throw an error if mmd diagram is invalid
try {
await mermaid.run({
nodes: [
/**
* @type {HTMLElement} We know this is a `HTMLElement`, since we
* control the input HTML file
*/ (container)
],
suppressErrors: false
})
} 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 if (isDetailedError(error)) {
throw new Error(error.message)
} else {
throw new Error(`Unknown mermaid render error: ${error}`)
}
}
const { svg: svgText } = await mermaid.render('my-svg', definition, container)
container.innerHTML = svgText

const svg = container.getElementsByTagName?.('svg')?.[0]
if (svg?.style) {
Expand Down Expand Up @@ -449,7 +416,7 @@ async function run (input, output, { puppeteerConfig = {}, quiet = false, output
if (input && /\.(md|markdown)$/.test(input)) {
const imagePromises = []
for (const mermaidCodeblockMatch of definition.matchAll(mermaidChartsInMarkdownRegexGlobal)) {
const mermaidDefinition = mermaidCodeblockMatch[1]
const mermaidDefinition = mermaidCodeblockMatch[2]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug here was the regex has two match groups, where Group 1 contains a leading newline! Group 2 is the correct group to use, since it doesn't contain the \n character.

Normally, when using HTML, you can put as many newlines as you want and the browser will remove everything (so this bug didn't matter), but the mermaid.render() API is much stricter.

See
image


/** Output can be either a template image file, or a `.md` output file.
* If it is a template image file, use that to created numbered diagrams
Expand Down
9 changes: 9 additions & 0 deletions test-positive/classDiagram-v2.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
# This test is for issue https://github.com/mermaid-js/mermaid-cli/issues/532
title: Empty class diagram v2 structs
---
classDiagram-v2
class Pancake {
}
class Waffle {
}