| 82 | } |
| 83 | }, |
| 84 | async postTransform(payload: string, { persistentCache }) { |
| 85 | if (mermaidGraphs.length > 0) { |
| 86 | const mermaid = (await import("headless-mermaid")).default; |
| 87 | const promisesArr = []; |
| 88 | const hashArr = []; |
| 89 | for (let graph of mermaidGraphs) { |
| 90 | let keyValue: string | string[] = []; |
| 91 | let options: Record<string, string | object> = { |
| 92 | theme: "forest", |
| 93 | sequence: { |
| 94 | showSequenceNumbers: true, |
| 95 | }, |
| 96 | }; |
| 97 | if (graph.lang.includes(" ")) { |
| 98 | [, ...keyValue] = graph.lang.split(" "); |
| 99 | options = { ...JSON.parse(keyValue.join(" ")) }; |
| 100 | } |
| 101 | const hashObj = hash({ code: graph.code, options }); |
| 102 | if (persistentCache.has(hashObj)) { |
| 103 | promisesArr.push(Promise.resolve(persistentCache.get(hashObj))); |
| 104 | } else { |
| 105 | promisesArr.push(mermaid.execute(graph.code, options)); |
| 106 | } |
| 107 | hashArr.push(hashObj); |
| 108 | } |
| 109 | |
| 110 | let idx = 0; |
| 111 | const strs: string[] = await Promise.all(promisesArr); |
| 112 | |
| 113 | strs.forEach((value, index) => { |
| 114 | if (!persistentCache.has(hashArr[index])) { |
| 115 | persistentCache.set(hashArr[index], value); |
| 116 | } |
| 117 | }); |
| 118 | |
| 119 | return payload.replace(/\<GRAPH\>\<\/GRAPH\>/g, (_) => strs[idx++]); |
| 120 | } |
| 121 | return payload; |
| 122 | }, |
| 123 | }; |
| 124 | } |