mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-14 06:43:25 +08:00
fix parse() to match spec (return true if valid, false if invalid (and a parseError handler is defined))
This commit is contained in:
parent
8210e3c80a
commit
67b6414693
@ -63,99 +63,120 @@ import getStyles from './styles';
|
|||||||
import theme from './themes';
|
import theme from './themes';
|
||||||
import utils, { directiveSanitizer, assignWithDepth, sanitizeCss } from './utils';
|
import utils, { directiveSanitizer, assignWithDepth, sanitizeCss } from './utils';
|
||||||
import DOMPurify from 'dompurify';
|
import DOMPurify from 'dompurify';
|
||||||
|
import mermaid from './mermaid';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param text
|
* @param text
|
||||||
* @returns {any}
|
* @returns {any}
|
||||||
*/
|
*/
|
||||||
function parse(text) {
|
function parse(text) {
|
||||||
text = text + '\n';
|
var parseEncounteredException = false;
|
||||||
const cnf = configApi.getConfig();
|
try {
|
||||||
const graphInit = utils.detectInit(text, cnf);
|
text = text + '\n';
|
||||||
if (graphInit) {
|
const cnf = configApi.getConfig();
|
||||||
reinitialize(graphInit);
|
const graphInit = utils.detectInit(text, cnf);
|
||||||
log.info('reinit ', graphInit);
|
if (graphInit) {
|
||||||
}
|
reinitialize(graphInit);
|
||||||
const graphType = utils.detectType(text, cnf);
|
log.info('reinit ', graphInit);
|
||||||
let parser;
|
}
|
||||||
|
const graphType = utils.detectType(text, cnf);
|
||||||
|
let parser;
|
||||||
|
|
||||||
log.debug('Type ' + graphType);
|
log.debug('Type ' + graphType);
|
||||||
switch (graphType) {
|
switch (graphType) {
|
||||||
case 'gitGraph':
|
case 'gitGraph':
|
||||||
gitGraphAst.clear();
|
gitGraphAst.clear();
|
||||||
parser = gitGraphParser;
|
parser = gitGraphParser;
|
||||||
parser.parser.yy = gitGraphAst;
|
parser.parser.yy = gitGraphAst;
|
||||||
break;
|
break;
|
||||||
case 'flowchart':
|
case 'flowchart':
|
||||||
flowDb.clear();
|
flowDb.clear();
|
||||||
parser = flowParser;
|
parser = flowParser;
|
||||||
parser.parser.yy = flowDb;
|
parser.parser.yy = flowDb;
|
||||||
break;
|
break;
|
||||||
case 'flowchart-v2':
|
case 'flowchart-v2':
|
||||||
flowDb.clear();
|
flowDb.clear();
|
||||||
parser = flowParser;
|
parser = flowParser;
|
||||||
parser.parser.yy = flowDb;
|
parser.parser.yy = flowDb;
|
||||||
break;
|
break;
|
||||||
case 'sequence':
|
case 'sequence':
|
||||||
sequenceDb.clear();
|
sequenceDb.clear();
|
||||||
parser = sequenceParser;
|
parser = sequenceParser;
|
||||||
parser.parser.yy = sequenceDb;
|
parser.parser.yy = sequenceDb;
|
||||||
break;
|
break;
|
||||||
case 'gantt':
|
case 'gantt':
|
||||||
parser = ganttParser;
|
parser = ganttParser;
|
||||||
parser.parser.yy = ganttDb;
|
parser.parser.yy = ganttDb;
|
||||||
break;
|
break;
|
||||||
case 'class':
|
case 'class':
|
||||||
parser = classParser;
|
parser = classParser;
|
||||||
parser.parser.yy = classDb;
|
parser.parser.yy = classDb;
|
||||||
break;
|
break;
|
||||||
case 'classDiagram':
|
case 'classDiagram':
|
||||||
parser = classParser;
|
parser = classParser;
|
||||||
parser.parser.yy = classDb;
|
parser.parser.yy = classDb;
|
||||||
break;
|
break;
|
||||||
case 'state':
|
case 'state':
|
||||||
parser = stateParser;
|
parser = stateParser;
|
||||||
parser.parser.yy = stateDb;
|
parser.parser.yy = stateDb;
|
||||||
break;
|
break;
|
||||||
case 'stateDiagram':
|
case 'stateDiagram':
|
||||||
parser = stateParser;
|
parser = stateParser;
|
||||||
parser.parser.yy = stateDb;
|
parser.parser.yy = stateDb;
|
||||||
break;
|
break;
|
||||||
case 'info':
|
case 'info':
|
||||||
log.debug('info info info');
|
log.debug('info info info');
|
||||||
parser = infoParser;
|
parser = infoParser;
|
||||||
parser.parser.yy = infoDb;
|
parser.parser.yy = infoDb;
|
||||||
break;
|
break;
|
||||||
case 'pie':
|
case 'pie':
|
||||||
log.debug('pie');
|
log.debug('pie');
|
||||||
parser = pieParser;
|
parser = pieParser;
|
||||||
parser.parser.yy = pieDb;
|
parser.parser.yy = pieDb;
|
||||||
break;
|
break;
|
||||||
case 'er':
|
case 'er':
|
||||||
log.debug('er');
|
log.debug('er');
|
||||||
parser = erParser;
|
parser = erParser;
|
||||||
parser.parser.yy = erDb;
|
parser.parser.yy = erDb;
|
||||||
break;
|
break;
|
||||||
case 'journey':
|
case 'journey':
|
||||||
log.debug('Journey');
|
log.debug('Journey');
|
||||||
parser = journeyParser;
|
parser = journeyParser;
|
||||||
parser.parser.yy = journeyDb;
|
parser.parser.yy = journeyDb;
|
||||||
break;
|
break;
|
||||||
case 'requirement':
|
case 'requirement':
|
||||||
case 'requirementDiagram':
|
case 'requirementDiagram':
|
||||||
log.debug('RequirementDiagram');
|
log.debug('RequirementDiagram');
|
||||||
parser = requirementParser;
|
parser = requirementParser;
|
||||||
parser.parser.yy = requirementDb;
|
parser.parser.yy = requirementDb;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
parser.parser.yy.graphType = graphType;
|
parser.parser.yy.graphType = graphType;
|
||||||
parser.parser.yy.parseError = (str, hash) => {
|
parser.parser.yy.parseError = (str, hash) => {
|
||||||
const error = { str, hash };
|
const error = { str, hash };
|
||||||
throw error;
|
throw error;
|
||||||
};
|
};
|
||||||
|
|
||||||
parser.parse(text);
|
parser.parse(text);
|
||||||
return parser;
|
} catch (error) {
|
||||||
|
parseEncounteredException = true;
|
||||||
|
// Is this the correct way to access mermiad's parseError()
|
||||||
|
// method ? (or global.mermaid.parseError()) ?
|
||||||
|
if (mermaid.parseError) {
|
||||||
|
if (error.str != undefined) {
|
||||||
|
// handle case where error string and hash were
|
||||||
|
// wrapped in object like`const error = { str, hash };`
|
||||||
|
mermaid.parseError(error.str, error.hash);
|
||||||
|
} else {
|
||||||
|
// assume it is just error string and pass it on
|
||||||
|
mermaid.parseError(error);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// No mermaid.parseError() handler defined, so re-throw it
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return !parseEncounteredException;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const encodeEntities = function (text) {
|
export const encodeEntities = function (text) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user