Created
September 25, 2025 21:42
-
-
Save mingfang/2f45c792f0668c85e9f8206c145e5b1a to your computer and use it in GitHub Desktop.
Create Mermaid Live link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // based on https://stackblitz.com/edit/stackblitz-starters-5bzieps1?file=index.js | |
| import { deflate } from 'pako'; | |
| function jsStringToByte(data) { | |
| return new TextEncoder().encode(data); | |
| } | |
| function jsBtoa(data) { | |
| let binary = ''; | |
| const bytes = new Uint8Array(data); | |
| for (let i = 0; i < bytes.byteLength; i++) { | |
| binary += String.fromCharCode(bytes[i]); | |
| } | |
| return btoa(binary); | |
| } | |
| export function mermaidLink(graphMarkdown, editMode) { | |
| const jGraph = { | |
| code: graphMarkdown, | |
| mermaid: { theme: 'default' }, | |
| }; | |
| const byteStr = jsStringToByte(JSON.stringify(jGraph)); | |
| const deflated = deflate(byteStr); | |
| const dEncode = jsBtoa(deflated); | |
| const link = | |
| `https://mermaid.live/${editMode ? 'edit' : 'view'}#pako:` + | |
| dEncode.replace('+', '-').replace('/', '_'); | |
| return link; | |
| } | |
| // const example = ` | |
| // sequenceDiagram | |
| // Alice->>+John: Hello John, how are you? | |
| // Alice->>+John: John, can you hear me? | |
| // John-->>-Alice: Hi Alice, I can hear you! | |
| // John-->>-Alice: I feel great!`; | |
| // console.log(mermaidLink(example)); | |
| // console.log(mermaidLink(example, true)); | |
| // example langgraph usage | |
| // console.log(mermaidLink((await graph.getGraphAsync()).drawMermaid())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment