Skip to content

Commit

Permalink
Add flowchart useMaxWidth config parameter (#41)
Browse files Browse the repository at this point in the history
* Add flowchart useMaxWidth config parameter

The useMaxWidth parameter controls whether the diagram will be scaled to fit the available width. The default for this parameter is true, meaning that the diagram will be scaled. For very wide diagrams, this can result in an unreadably small diagram. Setting useMaxWidth to false allows the diagram to remain at a fixed sized. Scrollbars can be added to the area, if desired, with:

.ext-mermaid > div {
	overflow: scroll;
}

* Add flowchart useMaxWidth config parameter

The useMaxWidth parameter controls whether the diagram will be scaled to
fit the available width. The default for this parameter is true, meaning
that the diagram will be scaled. For very wide diagrams, this can result
in an unreadably small diagram. Setting useMaxWidth to false allows the
diagram to remain at a fixed sized. Scrollbars can be added to the area,
if desired, with:

.ext-mermaid > div {
	overflow: scroll;
}
  • Loading branch information
cicalese authored and kghbln committed Nov 9, 2019
1 parent 7963e52 commit 4c91bf5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/MermaidParserFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,26 @@ public function parse( array $params ) {
foreach ( $params as $key => $param ) {

if ( strpos( $param, '=' ) !== false ) {
list( $k, $v ) = explode( '=', $param, 2 );
list( $k, $value ) = array_map( 'trim', explode( '=', $param, 2 ) );

if ( $k === 'config.theme' ) {
$config['theme'] = $v;
$keys = explode( '.', $k );
if ( count( $keys ) == 1 || $keys[0] !== 'config' ) {
continue;
}
array_shift( $keys );

if ( $this->parseParam( $keys, [ 'theme' ], $value, $config ) ) {
unset( $params[$key] );
}

elseif ( $this->parseParam( $keys, [ 'flowchart' , 'curve' ], $value,
$config ) ) {
unset( $params[$key] );
}

if ( $k === 'config.flowchart.curve' ) {
$config['flowchart'] = [ 'curve' => $v ];
elseif ( $this->parseParam( $keys, [ 'flowchart', 'useMaxWidth' ],
filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
$config ) ) {
unset( $params[$key] );
}
}
Expand Down Expand Up @@ -136,4 +147,21 @@ public function parse( array $params ) {
);
}

private function parseParam( $paramKeys, $configKeys, $value, &$config ) {

if ( $paramKeys !== $configKeys ) {
return false;
}

$a = &$config;
foreach ( $configKeys as $key ) {
if ( !isset( $a[$key] ) ) {
$a[$key] = [];
}
$a = &$a[$key];
}
$a = $value;

return true;
}
}
11 changes: 11 additions & 0 deletions tests/phpunit/Unit/MermaidParserFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ public function textProvider() {
[ 'A[Hard edge] -->|Link text| B(Round edge)' ],
'data-mermaid="{"content":"A[Hard edge] -->|Link text| B(Round edge)"'
];

yield [
[ 'graph LR;', 'config.theme=foo', 'config.flowchart.curve=basis' ],
'class="ext-mermaid" data-mermaid="{&quot;content&quot;:&quot;graph LR;&quot;,&quot;config&quot;:{&quot;theme&quot;:&quot;foo&quot;,&quot;flowchart&quot;:{&quot;curve&quot;:&quot;basis&quot;}}}"><div class="mermaid-dots"></div></div>'
];

yield [
[ 'graph LR;', 'config.theme=foo', 'config.flowchart.useMaxWidth=false' ],
'class="ext-mermaid" data-mermaid="{&quot;content&quot;:&quot;graph LR;&quot;,&quot;config&quot;:{&quot;theme&quot;:&quot;foo&quot;,&quot;flowchart&quot;:{&quot;useMaxWidth&quot;:false}}}"><div class="mermaid-dots"></div></div>'
];

}

}

0 comments on commit 4c91bf5

Please sign in to comment.