问题
Struggling to get Mermaid - https://github.com/knsv/mermaid to work with Marked - https://github.com/chjj/marked
From what I gather I should be able to write the following in markdown
```
graph TD;A-->B;A-->C;B-->D;C-->D;
```
and have it render
<div class="mermaid">
FLOWCHART / DIAGRAM IS DRAWN HERE
</div>
What am I missing?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="libs/jquery.min.js"></script>
<script src="libs/marked.min.js"></script>
<script src="libs/mermaid.full.js"></script>
</head>
<body>
<div id="content"></div>
<script>
var renderer = new marked.Renderer();
renderer.code = function (code, language) {
if(code.match(/^sequenceDiagram/)||code.match(/^graph/)){
return '<div class="mermaid">'+code+'</div>';
}
};
$(document).ready(function(){
$.get( "test.md", function( data ) {
// console.log(data);
$('#content').html(marked(data));
});
});
console.log(marked('```graph TD;A-->B;A-->C;B-->D;C-->D;```', { renderer: renderer }));
</script>
</body>
</html>
回答1:
I tested your code as far as to get the console.log writing the mermaid div.
There is nothing wrong with your marked instantiation and nothing wrong with your renderer. However... the markdown in the console log was not ok.
By adding new lines before and after the graph definition the expeced div was printed to the console.:
\ngraph TD;A-->B;A-->C;B-->D;C-->D;\n
I hope this helps.
/Knut
回答2:
I had that issue as well. Try this string format, it worked for me:
'graph TB\n'+'A --> B\n'+'A --> C\n'+'B --> D\n'+'C --> D\n'
来源:https://stackoverflow.com/questions/28626329/marked-markdown-mermaid-flow-chart-diagrams