mermaid-js / mermaid-cli

Command line tool for the Mermaid library
MIT License
2.35k stars 227 forks source link

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

Closed aloisklink closed 1 year ago

aloisklink commented 1 year ago

:bookmark_tabs: Summary

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

:straight_ruler: 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!

:clipboard: Tasks

Make sure you

MindaugasLaganeckas commented 1 year ago

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 ❤️