From bc5ef5fb7d1a029c47ca6b0a46295a897a13f7ba Mon Sep 17 00:00:00 2001 From: Knut Sveidqvist Date: Tue, 11 Oct 2022 11:20:34 +0200 Subject: [PATCH] Fix for async issue in parse, adding parseAsync --- .gitignore | 1 + cypress/platform/knsv2.html | 10 ---------- package.json | 2 +- packages/mermaid/src/mermaid.ts | 5 +++++ packages/mermaid/src/mermaidAPI.ts | 14 ++++++++++++++ 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 6e4fe723a..15acfceeb 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ cypress/snapshots/ .eslintcache .tsbuildinfo tsconfig.tsbuildinfo +knsv*.html diff --git a/cypress/platform/knsv2.html b/cypress/platform/knsv2.html index 506ac51ae..de9780185 100644 --- a/cypress/platform/knsv2.html +++ b/cypress/platform/knsv2.html @@ -75,16 +75,6 @@ classDiagram
 mindmap
   root
-    A
-    B
-    C
-    D
-    E
-    A2
-    B2
-    C2
-    D2
-    E2
     child1((Circle))
         grandchild 1
         grandchild 2
diff --git a/package.json b/package.json
index 25d614f95..15adccd64 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
 {
   "name": "mermaid-monorepo",
   "private": true,
-  "version": "9.2.0-rc2",
+  "version": "9.2.0-rc4",
   "description": "Markdownish syntax for generating flowcharts, sequence diagrams, class diagrams, gantt charts and git graphs.",
   "main": "dist/mermaid.core.mjs",
   "module": "dist/mermaid.core.mjs",
diff --git a/packages/mermaid/src/mermaid.ts b/packages/mermaid/src/mermaid.ts
index b83d40a83..6149bb19c 100644
--- a/packages/mermaid/src/mermaid.ts
+++ b/packages/mermaid/src/mermaid.ts
@@ -309,6 +309,9 @@ const setParseErrorHandler = function (newParseErrorHandler: (err: any, hash: an
 const parse = (txt: string) => {
   return mermaidAPI.parse(txt, mermaid.parseError);
 };
+const parseAsync = (txt: string) => {
+  return mermaidAPI.parseAsync(txt, mermaid.parseError);
+};
 
 const mermaid: {
   startOnLoad: boolean;
@@ -317,6 +320,7 @@ const mermaid: {
   parseError?: Function;
   mermaidAPI: typeof mermaidAPI;
   parse: typeof parse;
+  parseAsync: typeof parseAsync;
   render: typeof mermaidAPI.render;
   init: typeof init;
   initThrowsErrors: typeof initThrowsErrors;
@@ -328,6 +332,7 @@ const mermaid: {
   diagrams: {},
   mermaidAPI,
   parse,
+  parseAsync,
   render: mermaidAPI.render,
   init,
   initThrowsErrors,
diff --git a/packages/mermaid/src/mermaidAPI.ts b/packages/mermaid/src/mermaidAPI.ts
index 69425c7b5..7a409edcc 100644
--- a/packages/mermaid/src/mermaidAPI.ts
+++ b/packages/mermaid/src/mermaidAPI.ts
@@ -43,6 +43,18 @@ function parse(text: string, parseError?: Function): boolean {
   const diagram = new Diagram(text, parseError);
   return diagram.parse(text, parseError);
 }
+/* eslint-disable @typescript-eslint/ban-types */
+/**
+ *
+ * @param text
+ * @param parseError
+ */
+async function parseAsync(text: string, parseError?: Function): Promise {
+  addDiagrams();
+  const diagram = await getDiagramFromText(text, parseError);
+  return diagram.parse(text, parseError);
+}
+/* eslint-enable @typescript-eslint/ban-types */
 
 export const encodeEntities = function (text: string): string {
   let txt = text;
@@ -760,6 +772,7 @@ export const mermaidAPI = Object.freeze({
   render,
   renderAsync,
   parse,
+  parseAsync,
   parseDirective,
   initialize,
   getConfig: configApi.getConfig,
@@ -778,6 +791,7 @@ export const mermaidAPI = Object.freeze({
 setLogLevel(configApi.getConfig().logLevel);
 configApi.reset(configApi.getConfig());
 export default mermaidAPI;
+
 /**
  * ## mermaidAPI configuration defaults
  *