Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View MermaidJS Graph passing the code or URL #41

Open
brunobarretofreitas opened this issue Jan 8, 2019 · 6 comments
Open

View MermaidJS Graph passing the code or URL #41

brunobarretofreitas opened this issue Jan 8, 2019 · 6 comments

Comments

@brunobarretofreitas
Copy link

I would like to have a feature where I can pass the mermaidjs code through URL, so It can be displayed on the screen:
Example:
https://mermaidjs.github.io/mermaid-live-editor/#/view/?code=graph TD; A[Christmas] -->|Get money| B(Go shopping);

@jihchi
Copy link

jihchi commented Aug 13, 2019

FYI.

I've made a PR and service to support embed image from code (#52).

You can now directly use base64 encoded mermaid code and get the image from mermaid.ink service:

For example, get base64 encoded mermaid code from mermaid-live-editor:

c2VxdWVuY2VEaWFncmFtCiAgICBwYXJ0aWNpcGFudCBBbGljZQogICAgcGFydGljaXBhbnQgQm9iCiAgICBBbGljZS0-Sm9objogSGVsbG8gSm9obiwgaG93IGFyZSB5b3U_CiAgICBsb29wIEhlYWx0aGNoZWNrCiAgICAgICAgSm9obi0-Sm9objogRmlnaHQgYWdhaW5zdCBoeXBvY2hvbmRyaWEKICAgIGVuZAogICAgTm90ZSByaWdodCBvZiBKb2huOiBSYXRpb25hbCB0aG91Z2h0cyA8YnIvPnByZXZhaWwuLi4KICAgIEpvaG4tLT5BbGljZTogR3JlYXQhCiAgICBKb2huLT5Cb2I6IEhvdyBhYm91dCB5b3U_CiAgICBCb2ItLT5Kb2huOiBKb2xseSBnb29kIQ

append the code after https://mermaid.ink/img/, for example:

https://mermaid.ink/img/c2VxdWVuY2VEaWFncmFtCiAgICBwYXJ0aWNpcGFudCBBbGljZQogICAgcGFydGljaXBhbnQgQm9iCiAgICBBbGljZS0-Sm9objogSGVsbG8gSm9obiwgaG93IGFyZSB5b3U_CiAgICBsb29wIEhlYWx0aGNoZWNrCiAgICAgICAgSm9obi0-Sm9objogRmlnaHQgYWdhaW5zdCBoeXBvY2hvbmRyaWEKICAgIGVuZAogICAgTm90ZSByaWdodCBvZiBKb2huOiBSYXRpb25hbCB0aG91Z2h0cyA8YnIvPnByZXZhaWwuLi4KICAgIEpvaG4tLT5BbGljZTogR3JlYXQhCiAgICBKb2huLT5Cb2I6IEhvdyBhYm91dCB5b3U_CiAgICBCb2ItLT5Kb2huOiBKb2xseSBnb29kIQ

you will get the jpg image:

Mermaid Ink

@theroyakash
Copy link

theroyakash commented Nov 25, 2020

@jihchi How can I generate those base64 encoded strings. I mean what is the algorithm? just put the graph as a text inside base64 algorithms?
Do I do this like

BASE64ENCODE(graph):
    encoded_string <- encode(graph)
    return encoded_string


BASE64ENCODE("""
Graph TD
    A[Christmas] -->|Get money| B(Go shopping);
""")

@theroyakash
Copy link

Oh sorry, I've solved this. Using python and mermaid.ink service like this:

import base64

graph = """
graph LR;
    A-- 2 --->B;
    A-->C;
    B-->D;
    C-- 3 -->D;
"""

graphbytes = graph.encode("ascii") 

base64_bytes = base64.b64encode(graphbytes) 
base64_string = base64_bytes.decode("ascii") 

print(f"Encoded string: {base64_string}")
import requests, io
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open(io.BytesIO(requests.get('https://mermaid.ink/img/' + base64_string).content))
plt.imshow(img)

This reconstructs the graph like:
image

@songz
Copy link

songz commented Jun 27, 2022

FYI for future peeps running into this issue, follow their serialize/de-serialize function

https://github.com/mermaid-js/mermaid-live-editor/blob/b5978e6faf7635e39452855fb4d062d1452ab71b/src/lib/util/serde.ts#L19

@Ziqi-Yang
Copy link

Basically, it is:

import { deflate } from 'pako';
import { fromUint8Array } from 'js-base64';

const formatJSON = (data: unknown): string => JSON.stringify(data, undefined, 2);
const serialize = (state: string): string => {
  const data = new TextEncoder().encode(state);
  const compressed = deflate(data, { level: 9 }); // zlib level 9
  return fromUint8Array(compressed, true); // url safe base64 encoding
}

const defaultState = {
  code: `flowchart TD
    A[Christmas] -->|Get money| B(Go shopping)
    B --> C{Let me think}
    C -->|One| D[Laptop]
    C -->|Two| E[iPhone]
    C -->|Three| F[fa:fa-car Car]
  `,
  mermaid: formatJSON({
    theme: 'default'
  }),
  autoSync: true,
  updateDiagram: true
};

const json = JSON.stringify(defaultState);
console.log(json)
const serialized = serialize(json);
console.log(`pako:${serialized}`);

@sakurawald
Copy link

Oh sorry, I've solved this. Using python and mermaid.ink service like this:

import base64

graph = """
graph LR;
    A-- 2 --->B;
    A-->C;
    B-->D;
    C-- 3 -->D;
"""

graphbytes = graph.encode("ascii") 

base64_bytes = base64.b64encode(graphbytes) 
base64_string = base64_bytes.decode("ascii") 

print(f"Encoded string: {base64_string}")
import requests, io
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open(io.BytesIO(requests.get('https://mermaid.ink/img/' + base64_string).content))
plt.imshow(img)

This reconstructs the graph like: image

This works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants