Replies: 6 comments 5 replies
-
|
I managed to figure it out with help from stackoverflow and debugging in the browser. Hopefully this helps someone else: |
Beta Was this translation helpful? Give feedback.
-
|
While I'm doing Java now I tried to copy this implementation and it doesn't seem to work. Does mermaid still use Base64? It looks like some other character encoding. |
Beta Was this translation helpful? Give feedback.
-
|
Got it! There were two problems:
A snippet of Java code that works for me: |
Beta Was this translation helpful? Give feedback.
-
|
Hello, Here is the Python script by @dvulanov converted to JavaScript : StackBlitz example : https://stackblitz.com/edit/stackblitz-starters-5bzieps1 |
Beta Was this translation helpful? Give feedback.
-
|
I'm still a big fan of mermaid, and it looks like this thread is still helping people. Note that if you're like me and posting your mermaid links to Azure Devops wiki documents (markdown), these links will break (at least my python implementation does) because they produce pluses and slashes in the URL (which devops sanitizes). You can get around this by replacing + for - and / for _ i.e. link = 'http://mermaid.live/view#pako:' + js_bytes_to_string(dEncode).replace('+', '-').replace('/', '_') (I've updated my original code, and @zeekoe already addressed this issue) |
Beta Was this translation helpful? Give feedback.
-
|
In case anybody needs it, here's an implementation in free form ILE RPG for IBMi : **free
ctl-opt dftactgrp(*no) bnddir('ZLIB/ZLIB');
/include ZLIB/H,ZLIBFREE
dcl-s code varchar(8192) ccsid(*utf8) inz;
dcl-s json char(8192) ccsid(*utf8) inz;
dcl-s jsonHex char(8192) ccsid(*hex) inz;
dcl-s jsonHex2 char(8192) ccsid(*hex) inz;
dcl-s jsonInflatedLen uns(10) inz;
dcl-s jsonDeflatedLen uns(10) inz;
dcl-s zlibStatus int(10) inz(Z_OK);
dcl-ds zstream likeds(z_stream);
code = 'graph TD' + '\n';
code += '\t' + 'A[hello]-->B[world]' + '\n';
code = '{"code": "' + code + '", "mermaid": {"theme": "default"}}';
json = code;
jsonInflatedLen = %len(code);
longsndmsg('JSON chars (' + %char(jsonInflatedLen) + ' bytes): ' + json);
longsndmsg('JSON bytes: ' + bytes_of(%addr(json) : jsonInflatedLen));
jsonDeflatedLen = compressBound(jsonInflatedLen); // max deflated len
jsonHex = json;
bzero(%addr(zstream) : %size(zstream));
zstream.zs_next_in = %addr(jsonHex);
zstream.zs_avail_in = jsonInflatedLen;
zstream.zs_next_out = %addr(jsonHex2);
zstream.zs_avail_out = jsonDeflatedLen;
zlibStatus = deflateInit2(zstream : Z_BEST_COMPRESSION : Z_DEFLATED : 15 : 8 : Z_DEFAULT_STRATEGY : ZLIB_VERSION : %size(z_stream));
if zlibStatus <> Z_OK;
snd-msg 'deflateInit2 error';
snd-msg %str(zError(zlibStatus));
return;
endif;
zlibStatus = deflate(zstream : Z_FINISH);
if zlibStatus <> Z_STREAM_END;
snd-msg 'deflate error !';
snd-msg %str(zError(zlibStatus));
snd-msg 'deflate partial bytes: ' + bytes_of(%addr(jsonHex2) : jsonDeflatedLen);
return;
endif;
if deflateEnd(zstream) <> Z_OK;
snd-msg 'deflateEnd error !';
snd-msg %str(zError(zlibStatus));
return;
endif;
longsndmsg('deflated bytes: ' + bytes_of(%addr(jsonHex2) : zstream.zs_total_out));
json = %subst(jsonHex2 : 1 : zstream.zs_total_out);
jsonDeflatedLen = zstream.zs_total_out;
exec sql values qsys2.base64_encode(substr(:jsonHex2, 1, cast(:jsonDeflatedLen as integer))) into :json;
json = %scanrpl(%char('+' : *utf8) : %char('-' : *utf8) : json : *stdcharsize); // *stdcharsize as base64 uses simple unaccented characters
json = %scanrpl(%char('/' : *utf8) : %char('_' : *utf8) : json : *stdcharsize); // *stdcharsize as base64 uses simple unaccented characters
longsndmsg(%trim(json));
return;
dcl-proc bytes_of;
dcl-pi *n varchar(65536);
strptr pointer value;
n_bytes uns(10) value;
end-pi;
dcl-s i uns(10) inz;
dcl-s byte uns(3) based(strptr);
dcl-s res varchar(65536) inz;
for i = 1 to n_bytes;
if i > 1;
res += ' ';
endif;
res += hexdigit(%div(byte : 16));
res += hexdigit(%rem(byte : 16));
strptr += 1;
endfor;
return res;
end-proc;
dcl-proc hexdigit;
dcl-pi *n varchar(1);
n uns(3) const;
end-pi;
return %subst('0123456789ABCDEF' : n + 1 : 1);
end-proc;
dcl-proc bzero;
dcl-pi *n;
ptr pointer value;
size uns(10) value;
end-pi;
dcl-s byte uns(3) based(ptr);
dou size = 0;
byte = 0;
ptr += 1;
size -= 1;
enddo;
return;
end-proc;
dcl-proc longsndmsg;
dcl-pi *n;
str varchar(65536) const;
end-pi;
dcl-s start uns(20) inz(1);
dcl-s len uns(20) inz;
len = %len(str);
dou start > len;
snd-msg %subst(str : start : %min(512 : len - start) : *natural);
start += 512;
enddo;
end-proc;It's pretty long as I added some debug procedures at the end, and I advise a record length of about 140 for the physical file storing this snippet, as some lines are long too. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I really appreciate the functionality of the mermaid live editor. For me, I'm generating my urls via python. I can encode64 graphs, but would also like to be able to encode larger graphs using pako.
Does anyone know the python implementation of pako (zlib) that's compatible with mermaid live?
Many thanks for any help,
Dan
Beta Was this translation helpful? Give feedback.
All reactions