Created
April 3, 2025 15:38
-
-
Save schakko/8ad6ccca7f6c5191d9b115ac62e50d84 to your computer and use it in GitHub Desktop.
Helper functions to create or parse Mermaid API requests to implement your own mermaid rendering API/service
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
<?php | |
/** | |
* Convert the given payload to a pako-compressed string, parsable by mermaid | |
* @var object|array $item | |
* @return string | |
*/ | |
function payload_to_mermaid(object|array $item): string { | |
return pako_compress(json_encode($item)); | |
} | |
/** | |
* Compress the given string with pako | |
* @param string $s | |
* @return string | |
*/ | |
function pako_compress(string $s): string { | |
$converted = mb_convert_encoding($s, "UTF-8"); | |
// level=9 is required for mermaid | |
$inflated = zlib_encode($converted, ZLIB_ENCODING_DEFLATE, 9); | |
$encoded = base64_encode($inflated); | |
// var_dump(dump_bin2hex($encoded)); | |
// replace those base64 characters | |
$encoded = str_replace('/', '_', $encoded); | |
$encoded = str_replace('+', '-', $encoded); | |
return $encoded; | |
} | |
/** | |
* Decompress as mermaid-specific pako payload. | |
* If you want to implement your own mermaid rendering server/API, this is for you. | |
* @param string $s | |
* @return object|array | |
*/ | |
function payload_from_mermaid(string $s): object|array { | |
return json_decode(pako_decompress($s)); | |
} | |
/** | |
* Decompress the pako payload | |
* @param string $s | |
* @return mixed|string | |
*/ | |
function pako_decompress(string $s): mixed { | |
$decoded = str_replace('_', '/', $s); | |
$decoded = str_replace('-', '/', $decoded); | |
// append == for making it base64 decodable | |
if (!str_ends_with($decoded, '==')) { | |
$decoded .= "=="; | |
} | |
$binary = base64_decode($decoded); | |
$deflated = zlib_decode($binary); | |
return $deflated; | |
} | |
/** | |
* Create a new mermaid rendering request | |
* @param string Your diagram-as-a-code | |
* @param null|array additional mermaid rendering configuration | |
* @return string | |
*/ | |
function mermaid_request(string $mermaidDiagram, ?array $configuration = []): string { | |
$configuration = $configuration ?? ['theme' => 'default']; | |
return payload_to_mermaid([ | |
'code' => $mermaidDiagram, | |
'mermaid' => $configuration | |
]); | |
} | |
/** | |
* Build a full rendering link to your mermaid instance | |
* @param string $mermaidDiagram | |
* @param null|array $configuration additional mermaid rendering configuration | |
* @param string $baseUrl Defaults to mermaid.live but feel free to use your own rendering server | |
* @param string $mode Either 'view' or 'edit' | |
* @return string | |
*/ | |
function build_mermaid_render_link(string $mermaidDiagram, ?array $configuration = [], string $baseUrl = 'http://mermaid.live/', $mode = 'view'): string { | |
return $baseUrl | |
. ( $mode == 'view' ? 'view' : 'edit' ) | |
. '#pako:' | |
. mermaid_request($mermaidDiagram, $configuration); | |
} | |
/** | |
* Dump the given parameter as a formatted hex array. | |
* @param string|array stringOrArray | |
* @return string | |
*/ | |
function dump_bin2hex(string|array $stringOrArray): string { | |
$r = []; | |
$size = is_string($stringOrArray) ? strlen($stringOrArray) : sizeof($stringOrArray); | |
for ($i = 0; $i < $size; $i++) { | |
$r[] = str_pad(dechex(ord($stringOrArray[$i])), 2, "0", STR_PAD_LEFT); | |
} | |
return implode(" ", $r); | |
} | |
echo build_mermaid_render_link(<<<PHP | |
C4Context | |
title My second landscape | |
PHP | |
); | |
var_dump(payload_from_mermaid("eNqrVkrOT0lVslJyNnHOzytJrSiJyQPCkpLMkpxUBd9KheLU5Py8FIWcxLyU4uTEglQlHaXc1KLcxMwUJavo2FoA4LIWxg==")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment