mermaid/src/utils.js

68 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-09-10 13:41:34 +02:00
import { logger } from './logger'
/**
* @function detectType
* Detects the type of the graph text.
* ```mermaid
* graph LR
* a-->b
* b-->c
* c-->d
* d-->e
* e-->f
* f-->g
* g-->h
* ```
*
* @param {string} text The text defining the graph
* @returns {string} A graph definition key
*/
2017-09-09 08:46:58 +02:00
export const detectType = function (text) {
2017-04-11 16:14:25 +02:00
text = text.replace(/^\s*%%.*\n/g, '\n')
if (text.match(/^\s*sequenceDiagram/)) {
return 'sequenceDiagram'
}
2017-04-11 16:14:25 +02:00
if (text.match(/^\s*digraph/)) {
return 'dotGraph'
}
2017-04-11 16:14:25 +02:00
if (text.match(/^\s*info/)) {
return 'info'
}
2017-04-11 16:14:25 +02:00
if (text.match(/^\s*gantt/)) {
return 'gantt'
}
2017-04-11 16:14:25 +02:00
if (text.match(/^\s*classDiagram/)) {
2017-09-10 13:41:34 +02:00
logger.debug('Detected classDiagram syntax')
2017-04-11 16:14:25 +02:00
return 'classDiagram'
}
2015-10-30 10:47:25 +01:00
2017-04-11 16:14:25 +02:00
if (text.match(/^\s*gitGraph/)) {
2017-09-10 13:41:34 +02:00
logger.debug('Detected gitGraph syntax')
2017-04-11 16:14:25 +02:00
return 'gitGraph'
}
return 'graph'
}
/**
* @function isSubstringInArray
* Detects whether a substring in present in a given array
* @param {string} str The substring to detect
* @param {array} arr The array to search
* @returns {number} the array index containing the substring or -1 if not present
**/
2017-09-09 08:46:58 +02:00
export const isSubstringInArray = function (str, arr) {
for (let i = 0; i < arr.length; i++) {
2017-04-11 16:14:25 +02:00
if (arr[i].match(str)) return i
}
2017-04-11 16:14:25 +02:00
return -1
}
2017-09-10 13:41:34 +02:00
export default {
detectType,
isSubstringInArray
}