2
0

api.mdx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # API
  2. At the moment the mermaid-to-excalidraw works in two steps. First, you call `parseMermaidToExcalidraw(mermaidSyntax)` on the mermaid diagram definition string, which resolves with elements in a skeleton format — a simplified excalidraw JSON format (docs coming soon). You then pass them to `convertToExcalidrawElements(elements)` to get the fully qualified excalidraw elements you can render in the editor.
  3. The need for these two steps is due to the [@excalidraw/excalidraw](/docs/@excalidraw/excalidraw/installation) being a **UMD** build so we currently cannot import the `convertToExcalidrawElements()` util alone, until we support a tree-shakeable **ESM** build.
  4. ## parseMermaidToExcalidraw
  5. This API receives the mermaid syntax as the input, and resolves to skeleton Excalidraw elements. You will need to call `convertToExcalidraw` API to convert them to fully qualified elements that you can render in Excalidraw.
  6. ** Example **
  7. ```ts
  8. import { parseMermaidToExcalidraw } from "@excalidraw/mermaid-to-excalidraw";
  9. import { convertToExcalidrawElements} from "@excalidraw/excalidraw"
  10. try {
  11. const { elements, files } = await parseMermaid(mermaidSyntax: string, {
  12. fontSize: number,
  13. });
  14. const excalidrawElements = convertToExcalidrawElements(elements);
  15. // Render elements and files on Excalidraw
  16. } catch (e) {
  17. // Parse error, displaying error message to users
  18. }
  19. ```
  20. ## Supported Diagram Types
  21. Currently only [flowcharts](https://mermaid.js.org/syntax/flowchart.html) are supported. Oother diagram types will be rendered as an image in Excalidraw.
  22. ### Flowchart
  23. #### Excalidraw Regular Shapes
  24. **Rectangles**, **Circle**, **Diamond**, and **Arrows** are fully supported in Excalidraw
  25. ```
  26. flowchart TD
  27. A[Christmas] -->|Get money| B(Go shopping)
  28. B --> C{Let me think}
  29. C -->|One| D[Laptop]
  30. C -->|Two| E[iPhone]
  31. C -->|Three| F[Car]
  32. ```
  33. <img src="https://github.com/excalidraw/excalidraw/assets/11256141/c8ea84fc-e9fb-4652-9a12-154136d1a798" width="250" height="200"/>
  34. ```
  35. flowchart LR
  36. id1((Hello from Circle))
  37. ```
  38. <img src="https://github.com/excalidraw/excalidraw/assets/11256141/6202a8b9-8aa7-451e-9478-4d8d75c0f2fa" width="250" height="200"/>
  39. #### Subgraphs
  40. Subgraphs are grouped diagrams hence they are also supported in Excalidraw
  41. ```
  42. flowchart TB
  43. c1-->a2
  44. subgraph one
  45. a1-->a2
  46. end
  47. subgraph two
  48. b1-->b2
  49. end
  50. subgraph three
  51. c1-->c2
  52. end
  53. ```
  54. <img src="https://github.com/excalidraw/excalidraw/assets/11256141/098bce52-8f93-437c-9a06-c6972e27c70a" width="350" height="200"/>
  55. #### Unsupported shapes fallback to Rectangle
  56. **Subroutine**, **Cylindrical**, **Asymmetric**, **Hexagon**, **Parallelogram**, **Trapezoid** are not supported in Excalidraw hence they fallback to the closest shape **Rectangle**
  57. ```
  58. flowchart LR
  59. id1[[Subroutine fallback to Rectangle]]
  60. id2[(Cylindrical fallback to Rectangle)]
  61. id3>Asymmetric fallback to Rectangle]
  62. id4{{Hexagon fallback to Rectangle}}
  63. id5[/Parallelogram fallback to Rectangle /]
  64. id6[/Trapezoid fallback to Rectangle\]
  65. ```
  66. The above shapes are not supported in Excalidraw hence they fallback to Rectangle
  67. <img src="https://github.com/excalidraw/excalidraw/assets/11256141/cb269473-16c5-4c35-bd7a-d631d8cc5b47" width="350" height="200"/>
  68. #### Markdown fallback to Regular text
  69. Since we don't support wysiwyg text editor yet, hence [Markdown Strings](https://mermaid.js.org/syntax/flowchart.html#markdown-strings) will fallback to regular text.
  70. ```
  71. flowchart LR
  72. A("`Hello **World**`") --> B("`Whats **up** ?`")
  73. ```
  74. <img src="https://github.com/excalidraw/excalidraw/assets/11256141/107bd428-9ab9-42d4-ba12-b1e29c8db478" width="250" height="200"/>
  75. #### Basic FontAwesome fallback to text
  76. The [FontAwesome](https://mermaid.js.org/syntax/flowchart.html#basic-support-for-fontawesome) icons aren't support so they won't be rendered in Excalidraw
  77. ```
  78. flowchart TD
  79. B["fab:fa-twitter for peace"]
  80. B-->C[fa:fa-ban forbidden]
  81. B-->E(A fa:fa-camera-retro perhaps?)
  82. ```
  83. <img src="https://github.com/excalidraw/excalidraw/assets/11256141/7a693863-c3f9-42ff-b325-4b3f8303c7af" width="250" height="200"/>
  84. #### Cross Arrow head fallback to Bar Arrow head
  85. ```
  86. flowchart LR
  87. Start x--x Stop
  88. ```
  89. <img src="https://github.com/excalidraw/excalidraw/assets/11256141/217dd1ad-7f4e-4c80-8c1c-03647b42d821" width="250" height="200"/>
  90. ## Unsupported Diagram Types
  91. Currently only [flowcharts](https://mermaid.js.org/syntax/flowchart.html) are supported. All other diagram types will be rendered as an image in Excalidraw.
  92. ```
  93. erDiagram
  94. CUSTOMER ||--o{ ORDER : places
  95. ORDER ||--|{ LINE-ITEM : contains
  96. CUSTOMER }|..|{ DELIVERY-ADDRESS : uses
  97. ```
  98. <img src="https://github.com/excalidraw/excalidraw/assets/11256141/c1d3fdb3-32ef-4bf3-a38a-02ac3d7d2cb9" width="300" height="200"/>
  99. ```
  100. gitGraph
  101. commit
  102. commit
  103. branch develop
  104. checkout develop
  105. commit
  106. commit
  107. checkout main
  108. merge develop
  109. commit
  110. commit
  111. ```
  112. <img src="https://github.com/excalidraw/excalidraw/assets/11256141/e5dcec0b-d570-4eb4-b981-412a996aa96c" width="400" height="300"/>