mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-08-15 06:19:24 +02:00
Api for validating the syntax without rendering
This commit is contained in:
@@ -22,15 +22,4 @@ describe('when parsing an info graph it',function() {
|
||||
|
||||
ex.parse(str);
|
||||
});
|
||||
|
||||
it('should handle an showMessage statement definition', function () {
|
||||
var str = 'info\nshowInfo';
|
||||
|
||||
try{
|
||||
ex.parse(str);
|
||||
}
|
||||
catch(err){
|
||||
console.log('Caught'+err.message);
|
||||
}
|
||||
});
|
||||
});
|
@@ -188,11 +188,7 @@ exports.getClasses = function (text, isDot) {
|
||||
parser.yy = graph;
|
||||
|
||||
// Parse the graph definition
|
||||
try{
|
||||
parser.parse(text);
|
||||
}
|
||||
catch(err){
|
||||
}
|
||||
parser.parse(text);
|
||||
|
||||
var classDefStylesObj = {};
|
||||
var classDefStyleStr = '';
|
||||
|
@@ -245,13 +245,8 @@ module.exports.setConf = function(cnf){
|
||||
*/
|
||||
module.exports.draw = function (text, id) {
|
||||
sq.yy.clear();
|
||||
//console.log(text);
|
||||
try{
|
||||
sq.parse(text+'\n');
|
||||
}
|
||||
catch(err){
|
||||
sq.parse(text+'\n');
|
||||
|
||||
}
|
||||
exports.bounds.init();
|
||||
var diagram = d3.select('#'+id);
|
||||
|
||||
|
45
src/main.js
45
src/main.js
@@ -5,7 +5,49 @@ var flowRenderer = require('./diagrams/flowchart/flowRenderer');
|
||||
var seq = require('./diagrams/sequenceDiagram/sequenceRenderer');
|
||||
var info = require('./diagrams/example/exampleRenderer');
|
||||
var he = require('he');
|
||||
var infoParser = require('./diagrams/example/parser/example');
|
||||
var flowParser = require('./diagrams/flowchart/parser/flow');
|
||||
var dotParser = require('./diagrams/flowchart/parser/dot');
|
||||
var sequenceParser = require('./diagrams/sequenceDiagram/parser/sequenceDiagram');
|
||||
var sequenceDb = require('./diagrams/sequenceDiagram/sequenceDb');
|
||||
var infoDb = require('./diagrams/example/exampleDb');
|
||||
|
||||
/**
|
||||
* Function that parses a mermaid diagram defintion. If parsing fails the parseError callback is called and an error is
|
||||
* thrown and
|
||||
* @param text
|
||||
*/
|
||||
var parse = function(text){
|
||||
var graphType = utils.detectType(text);
|
||||
var parser;
|
||||
|
||||
switch(graphType){
|
||||
case 'graph':
|
||||
parser = flowParser;
|
||||
parser.parser.yy = graph;
|
||||
break;
|
||||
case 'dotGraph':
|
||||
parser = dotParser;
|
||||
parser.parser.yy = graph;
|
||||
break;
|
||||
case 'sequenceDiagram':
|
||||
parser = sequenceParser;
|
||||
parser.parser.yy = sequenceDb;
|
||||
break;
|
||||
case 'info':
|
||||
parser = infoParser;
|
||||
parser.parser.yy = infoDb;
|
||||
break;
|
||||
}
|
||||
|
||||
try{
|
||||
parser.parse(text);
|
||||
return true;
|
||||
}
|
||||
catch(err){
|
||||
return false;
|
||||
}
|
||||
};
|
||||
/**
|
||||
* Function that goes through the document to find the chart definitions in there and render them.
|
||||
*
|
||||
@@ -113,6 +155,9 @@ global.mermaid = {
|
||||
getParser:function(){
|
||||
return flow.parser;
|
||||
},
|
||||
parse:function(text){
|
||||
return parse(text);
|
||||
},
|
||||
parseError:function(err,hash){
|
||||
console.log('Mermaid Syntax error:');
|
||||
console.log(err);
|
||||
|
@@ -174,4 +174,88 @@ describe('when using main and ',function() {
|
||||
flowRend.addEdges(edges,mockG);
|
||||
});
|
||||
});
|
||||
|
||||
describe('checking validity of input ', function(){
|
||||
it('it should return false for an invalid definiton',function(){
|
||||
spyOn(mermaid,'parseError');
|
||||
var res = mermaid.parse('this is not a mermaid diagram definition');
|
||||
|
||||
expect(res).toBe(false);
|
||||
expect(mermaid.parseError).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('it should return true for a valid flow definition',function(){
|
||||
spyOn(mermaid,'parseError');
|
||||
var res = mermaid.parse('graph TD;A--x|text including URL space|B;');
|
||||
|
||||
expect(res).toBe(true);
|
||||
expect(mermaid.parseError).not.toHaveBeenCalled();
|
||||
});
|
||||
it('it should return false for an invalid flow definition',function(){
|
||||
spyOn(mermaid,'parseError');
|
||||
var res = mermaid.parse('graph TQ;A--x|text including URL space|B;');
|
||||
|
||||
expect(res).toBe(false);
|
||||
expect(mermaid.parseError).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('it should return true for a valid sequenceDiagram definition',function(){
|
||||
spyOn(mermaid,'parseError');
|
||||
var str = 'sequenceDiagram\n' +
|
||||
'Alice->Bob: Hello Bob, how are you?\n\n' +
|
||||
'%% Comment\n' +
|
||||
'Note right of Bob: Bob thinks\n' +
|
||||
'alt isWell\n\n' +
|
||||
'Bob-->Alice: I am good thanks!\n' +
|
||||
'else isSick\n' +
|
||||
'Bob-->Alice: Feel sick...\n' +
|
||||
'end';
|
||||
var res = mermaid.parse(str);
|
||||
|
||||
expect(res).toBe(true);
|
||||
expect(mermaid.parseError).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('it should return false for an invalid sequenceDiagram definition',function(){
|
||||
spyOn(mermaid,'parseError');
|
||||
var str = 'sequenceDiagram\n' +
|
||||
'Alice:->Bob: Hello Bob, how are you?\n\n' +
|
||||
'%% Comment\n' +
|
||||
'Note right of Bob: Bob thinks\n' +
|
||||
'alt isWell\n\n' +
|
||||
'Bob-->Alice: I am good thanks!\n' +
|
||||
'else isSick\n' +
|
||||
'Bob-->Alice: Feel sick...\n' +
|
||||
'end';
|
||||
var res = mermaid.parse(str);
|
||||
|
||||
expect(res).toBe(false);
|
||||
expect(mermaid.parseError).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('it should return true for a valid dot definition',function(){
|
||||
spyOn(mermaid,'parseError');
|
||||
var res = mermaid.parse('digraph\n' +
|
||||
'{\n' +
|
||||
' a -> b -> c -- d -> e;\n' +
|
||||
' a -- e;\n' +
|
||||
'}');
|
||||
|
||||
expect(res).toBe(true);
|
||||
expect(mermaid.parseError).not.toHaveBeenCalled();
|
||||
});
|
||||
it('it should return false for an invalid dot definition',function(){
|
||||
spyOn(mermaid,'parseError');
|
||||
var res = mermaid.parse('digraph\n' +
|
||||
'{\n' +
|
||||
'a -:> b -> c -- d -> e;\n' +
|
||||
'a -- e;\n' +
|
||||
'}');
|
||||
|
||||
expect(res).toBe(false);
|
||||
expect(mermaid.parseError).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
});
|
@@ -18,7 +18,7 @@ module.exports.detectType = function(text,a){
|
||||
}
|
||||
|
||||
if(text.match(/^\s*digraph/)) {
|
||||
console.log('Detected dot syntax');
|
||||
//console.log('Detected dot syntax');
|
||||
return "dotGraph";
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user