Either use the npm or bower package managers as per below:
bower install mermaid --save-devnpm install mermaid --save-devOr download javascript files as per the url below, note that #version# should be replaced with version of choice:
https://cdn.rawgit.com/knsv/mermaid/#version#/dist/mermaid.min.jsEx:
There are some bundles to choose from:
Important:
It's best to use a specific tag or commit hash in the URL (not a branch). Files are cached permanently after the first request.
Read more about that at https://rawgit.com/
The easiest way to integrate mermaid on a web page requires two elements:
If these things are in place mermaid listens to the page load event and when fires, when the page has loaded, it will
locate the graphs n the page and transform them to svg files.
<script src="mermaid.min.js"></script>
<script>mermaid.initialize({startOnLoad:true});</script>Further down on your page mermaid will look for tags with class="mermaid". From these tags mermaid will try to
read the chart definiton which will be replaced with the svg chart.
<div class="mermaid">
CHART DEFINITION GOES HERE
</div>Would end up like this:
<div class="mermaid" id="mermaidChart0">
<svg>
Chart ends up here
</svg>
</div>An id is also added to mermaid tags without id.
By default, mermaid.init will be called when the document is ready, finding all elements withclass="mermaid". If you are adding content after mermaid is loaded, or otherwise need
finer-grained control of this behavior, you can call init yourself with:
Example:
mermaid.init({noteMargin: 10}, ".someOtherClass");Or with no config object, and a jQuery selection:
mermaid.init(undefined, $("#someId .yetAnotherClass"));
The reader is assumed to know about CommonJS style of module handling and how to use browserify. If not a good place
to start would be http://browserify.org/ website.
Minimalistic javascript:
mermaid = require('mermaid');
console.log('Test page! mermaid version'+mermaid.version());Bundle the javascript with browserify.
Us the created bundle on a web page as per example below:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="mermaid.css" />
<script src="bundle.js"></script>
</head>
<body>
<div class="mermaid">
graph LR
A-->B
B-->C
C-->A
D-->C
</div>
</body>
</html>The main idea with the API is to be able to call a render function with graph defintion as a string. The render function
will render the graph and call a callback with the resulting svg code. With this approach it is up to the site creator to
fetch the graph definition from the site, perhaps from a textarea, render it and place the graph somewhere in the site.
To do this, include mermaidAPI on your web website instead of mermaid.js. The example below show an outline of how this
could be used. The example just logs the resulting svg to the javascript console.
<script src="mermaidAPI.js"></script>
<script>
mermaidAPI.initialize({
startOnLoad:false
});
// Example of using the API
$(function(){
var graphDefinition = 'graph TB\na-->b';
var graph = mermaidAPI.render(graphDefinition);
$("#graphDiv").html(graph);
});
</script>$ = require('jquery');
mermaidAPI = require('mermaid').mermaidAPI;
mermaidAPI.initialize({
startOnLoad:false
});
$(function(){
var graphDefinition = 'graph TB\na-->b';
var cb = function(html){
console.log(html);
}
mermaidAPI.render('id1',graphDefinition,cb);
});Sometimes the generated graph also has defined interactions like tooltip and click events. When using the API one must
add those events after the graph has been inserted into the DOM.
The example code below is an extract of wheat mermaid does when using the API. The example show how it is possible to
bind events to a svg when using the API for rendering.
var insertSvg = function(svgCode, bindFunctions){
element.innerHTML = svgCode;
if(typeof callback !== 'undefined'){
callback(id);
}
bindFunctions(element);
};
var id = 'theGraph';
mermaidAPI.render(id,txt,insertSvg, element);This is the renderer used for transforming the documentation from markdown to html with mermaid diagrams in the html.
var renderer = new marked.Renderer();
renderer.code = function (code, language) {
if(code.match(/^sequenceDiagram/)||code.match(/^graph/)){
return '<div class="mermaid">'+code+'</div>';
}
else{
return '<pre><code>'+code+'</code></pre>';
}
};Another example in coffeescript that also includes the mermaid script tag into the generated markup.
marked = require 'marked'
module.exports = (options) ->
hasMermaid = false
renderer = new marked.Renderer()
renderer.defaultCode = renderer.code
renderer.code = (code, language) ->
if language is 'mermaid'
html = ''
if not hasMermaid
hasMermaid = true
html += '<script src="'+options.mermaidPath+'"></script>'
html + '<div class="mermaid">'+code+'</div>'
else
@defaultCode(code, language)
rendererError handling
When the parser encounters invalid syntax the mermaid.parseError function is called. It is possible to override this
function in order to handle the error in an application specific way.
Parsing text without rendering
It is also possible to validate the syntax before rendering in order to streamline the user experience. The function
mermaid.parse(txt) takes a text string as an argument and returns true if the text is syntactically correct and
false if it is not. The parseError function will be called when the parse function returns false.
The code-example below in meta code illustrates how this could work:
mermaid.parseError = function(err,hash){
displayErrorInGui(err);
};
var textFieldUpdated = function(){
var textStr = getTextFromFormField('code');
if(mermaid.parse(textStr)){
reRender(textStr)
}
};
bindEventHandler('change', 'code', textFieldUpdated);Mermaid takes a number of options which lets you tweak the rendering of the diagrams. Currently there are three ways of
setting the options in mermaid.
The list above has two ways to many of doing this. Three are deprecated and will eventually be removed. The list of
configuration objects are described in the mermaidAPI documentation.
The future proof way of setting the configuration is by using the initialization call to mermaid or mermaidAPi depending
on what kind of integration you use.
<script src="../dist/mermaid.js"></script>
<script>
var config = {
startOnLoad:true,
flowchart:{
useMaxWidth:false,
htmlLabels:true
}
};
mermaid.initialize(config);
</script>
Is it possible to set some configuration via the mermaid object. The two parameters that are supported using this
approach are:
mermaid.startOnLoad = true;
Is it possible to set some configuration via the mermaid object. The two parameters that are supported using this
approach are:
mermaid_config.startOnLoad = true;
Is it possible to set some configuration via the mermaid object. The two parameters that are supported using this
approach are:
mermaid_config.startOnLoad = true;