mermaid: make yaml parsing more robust #603 #601

This commit is contained in:
Sören Weber 2023-08-05 14:05:52 +02:00
parent 66c8fcade1
commit c41edc86e1
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D

View file

@ -164,13 +164,13 @@ function initMermaid( update, attrs ) {
var YAML=1;
var INIT=2;
var GRAPH=3;
var d = /^(?:\s*[\n\r])*(-{3}\s*[\n\r](?:.*?)[\n\r]-{3}(?:\s*[\n\r]+)+)?(?:\s*(?:%%\s*\{\s*\w+\s*:([^%]*?)%%\s*[\n\r]?))?(.*)$/s
var d = /^(?:\s*[\n\r])*(?:-{3}(\s*[\n\r](?:.*?)[\n\r])-{3}(?:\s*[\n\r]+)+)?(?:\s*(?:%%\s*\{\s*\w+\s*:([^%]*?)%%\s*[\n\r]?))?(.*)$/s
var m = d.exec( graph );
var yaml = {};
var dir = {};
var content = graph;
if( m && m.length == 4 ){
yaml = m[YAML] ? jsyaml.load(m[YAML].replaceAll("---", "")) : yaml;
yaml = m[YAML] ? jsyaml.load( m[YAML] ) : yaml;
dir = m[INIT] ? JSON.parse( '{ "init": ' + m[INIT] ).init : dir;
content = m[GRAPH] ? m[GRAPH] : content;
}
@ -179,14 +179,14 @@ function initMermaid( update, attrs ) {
};
var serializeGraph = function( graph ){
yamlPart = '';
dirPart = '';
if (JSON.stringify(graph.dir) !== '{}') {
dirPart = '%%{init: ' + JSON.stringify(graph.dir) + '}%%\n';
}
if (JSON.stringify(graph.yaml) !== '{}') {
var yamlPart = '';
if( Object.keys( graph.yaml ).length ){
yamlPart = '---\n' + jsyaml.dump( graph.yaml ) + '---\n';
}
var dirPart = '';
if( Object.keys( graph.dir ).length ){
dirPart = '%%{init: ' + JSON.stringify( graph.dir ) + '}%%\n';
}
return yamlPart + dirPart + graph.content;
};