Merge pull request #5086 from mermaid-js/sidv/5042_maxEdges

feat #5042: Add `flowchart.maxEdges` config.
This commit is contained in:
Sidharth Vinod
2023-12-10 16:39:57 +00:00
committed by GitHub
3 changed files with 21 additions and 5 deletions

View File

@@ -68,6 +68,11 @@ export interface MermaidConfig {
* The maximum allowed size of the users text diagram * The maximum allowed size of the users text diagram
*/ */
maxTextSize?: number; maxTextSize?: number;
/**
* Defines the maximum number of edges that can be drawn in a graph.
*
*/
maxEdges?: number;
darkMode?: boolean; darkMode?: boolean;
htmlLabels?: boolean; htmlLabels?: boolean;
/** /**

View File

@@ -12,7 +12,6 @@ import {
setDiagramTitle, setDiagramTitle,
getDiagramTitle, getDiagramTitle,
} from '../common/commonDb.js'; } from '../common/commonDb.js';
import errorDiagram from '../error/errorDiagram.js';
const MERMAID_DOM_ID_PREFIX = 'flowchart-'; const MERMAID_DOM_ID_PREFIX = 'flowchart-';
let vertexCounter = 0; let vertexCounter = 0;
@@ -92,7 +91,6 @@ export const addVertex = function (_id, textObj, type, style, classes, dir, prop
if (txt[0] === '"' && txt[txt.length - 1] === '"') { if (txt[0] === '"' && txt[txt.length - 1] === '"') {
txt = txt.substring(1, txt.length - 1); txt = txt.substring(1, txt.length - 1);
} }
vertices[id].text = txt; vertices[id].text = txt;
} else { } else {
if (vertices[id].text === undefined) { if (vertices[id].text === undefined) {
@@ -160,11 +158,17 @@ export const addSingleLink = function (_start, _end, type) {
if (edge?.length > 10) { if (edge?.length > 10) {
edge.length = 10; edge.length = 10;
} }
if (edges.length < 280) { if (edges.length < (config.maxEdges ?? 500)) {
log.info('abc78 pushing edge...'); log.info('abc78 pushing edge...');
edges.push(edge); edges.push(edge);
} else { } else {
throw new Error('Too many edges'); throw new Error(
`Edge limit exceeded. ${edges.length} edges found, but the limit is ${config.maxEdges}.
Initialize mermaid with maxEdges set to a higher number to allow more edges.
You cannot set this config via configuration inside the diagram as it is a secure config.
You have to call mermaid.initialize.`
);
} }
}; };
export const addLink = function (_start, _end, type) { export const addLink = function (_start, _end, type) {
@@ -460,6 +464,7 @@ export const clear = function (ver = 'gen-1') {
tooltips = {}; tooltips = {};
firstGraphFlag = true; firstGraphFlag = true;
version = ver; version = ver;
config = getConfig();
commonClear(); commonClear();
}; };
export const setGen = (ver) => { export const setGen = (ver) => {

View File

@@ -74,6 +74,12 @@ properties:
description: The maximum allowed size of the users text diagram description: The maximum allowed size of the users text diagram
type: number type: number
default: 50000 default: 50000
maxEdges:
description: |
Defines the maximum number of edges that can be drawn in a graph.
type: integer
default: 500
minimum: 0
darkMode: darkMode:
type: boolean type: boolean
default: false default: false
@@ -156,7 +162,7 @@ properties:
in the current `currentConfig`. in the current `currentConfig`.
This prevents malicious graph directives from overriding a site's default security. This prevents malicious graph directives from overriding a site's default security.
default: ['secure', 'securityLevel', 'startOnLoad', 'maxTextSize'] default: ['secure', 'securityLevel', 'startOnLoad', 'maxTextSize', 'maxEdges']
type: array type: array
items: items:
type: string type: string