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

Conversation

aloisklink
Copy link
Member

📑 Summary

Use the mermaid.render() function instead of the mermaid.run() function to parse Mermaid diagram definitions directly, without going through HTML first.

📏 Design Decisions

Currently, mermaid-cli renders mermaid diagrams by:

flowchart LR
    subgraph a ["Removes newlines!!!"]
        div["HTML <div>"]
    end
    MMD --> div["HTML <div>"]
    div -->|"mermaid.run()"| svg["HTML <svg>"]

However, when converting the mmd code to HTML <div>, newlines and whitespace formatting may get removed. For the majority of diagrams, this is no issue, but for some diagrams (e.g. classDiagram), whitespace does matter.

The mermaid API has a mermaid.render() function that we can use instead, which parses in mmd code directly, without having to go through a HTML element first:

flowchart LR
    MMD -->|"mermaid.render()"| svgText["SVG text"] -->svg["HTML &lt;svg&gt;"]

As an additional benefit, we get better error messages from Mermaid too!

📋 Tasks

Make sure you

  • 📖 have read the contribution guidelines
  • 💻 have added unit/e2e tests (if appropriate)
  • 🔖 targeted master branch

The current regex match group we're using to get the mermaid diagrams
from markdown prepends a `\n` character to every mermaid diagram.

This only currently works because the current
mmdc -> HTML -> mermaid.init step removes these newlines automatically.
Currently, `mermaid-cli` renders mermaid diagrams by:

mmd code → HTML `<div>` contexts → mermaid.run() → HTML `<svg>`

However, when converting the mmd code to HTML `<div>`, newlines and
whitespace formatting may get removed. For the majority of diagrams,
this is no issue, but for some diagrams (e.g. classDiagram), whitespace
does matter.

The mermaid API has a `mermaid.render()` function that we can use
instead, which parses in mmd code directly, without having to go
through a HTML element first:

mmd code →  mermaid.render() →  SVG →  HTML `<svg>`

As an additional benefit, we get better error messages from Mermaid
(they're now proper JavaScript `Error`s!)

Fixes: mermaid-js#532
@github-actions github-actions bot added the fix label May 4, 2023
@@ -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

@MindaugasLaganeckas MindaugasLaganeckas merged commit ec8559c into mermaid-js:master May 8, 2023
7 of 8 checks passed
@MindaugasLaganeckas
Copy link
Member

Brilliant work! I appreciate your help very much!
BTW, I have upgraded your collaborator status for this repository to Maintain. Let me know if you would like to get Admin access ❤️

@aloisklink aloisklink deleted the fix/532-fix-class-diagram-error branch May 31, 2023 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to parse class diagram on v10.1.0
2 participants