mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-17 14:29:48 +02:00
@@ -2,6 +2,7 @@ import * as configApi from './config';
|
|||||||
import { log } from './logger';
|
import { log } from './logger';
|
||||||
import { getDiagram } from './diagram-api/diagramAPI';
|
import { getDiagram } from './diagram-api/diagramAPI';
|
||||||
import { detectType } from './diagram-api/detectType';
|
import { detectType } from './diagram-api/detectType';
|
||||||
|
import { isDetailedError } from './utils';
|
||||||
export class Diagram {
|
export class Diagram {
|
||||||
type = 'graph';
|
type = 'graph';
|
||||||
parser;
|
parser;
|
||||||
@@ -42,11 +43,9 @@ export class Diagram {
|
|||||||
// Is this the correct way to access mermiad's parseError()
|
// Is this the correct way to access mermiad's parseError()
|
||||||
// method ? (or global.mermaid.parseError()) ?
|
// method ? (or global.mermaid.parseError()) ?
|
||||||
if (parseError) {
|
if (parseError) {
|
||||||
// @ts-ignore
|
if (isDetailedError(error)) {
|
||||||
if (error.str != undefined) {
|
|
||||||
// handle case where error string and hash were
|
// handle case where error string and hash were
|
||||||
// wrapped in object like`const error = { str, hash };`
|
// wrapped in object like`const error = { str, hash };`
|
||||||
// @ts-ignore
|
|
||||||
parseError(error.str, error.hash);
|
parseError(error.str, error.hash);
|
||||||
} else {
|
} else {
|
||||||
// assume it is just error string and pass it on
|
// assume it is just error string and pass it on
|
||||||
|
@@ -13,11 +13,11 @@ let currentConfig: MermaidConfig = assignWithDepth({}, defaultConfig);
|
|||||||
|
|
||||||
export const updateCurrentConfig = (siteCfg: MermaidConfig, _directives: any[]) => {
|
export const updateCurrentConfig = (siteCfg: MermaidConfig, _directives: any[]) => {
|
||||||
// start with config being the siteConfig
|
// start with config being the siteConfig
|
||||||
let cfg = assignWithDepth({}, siteCfg);
|
let cfg: MermaidConfig = assignWithDepth({}, siteCfg);
|
||||||
// let sCfg = assignWithDepth(defaultConfig, siteConfigDelta);
|
// let sCfg = assignWithDepth(defaultConfig, siteConfigDelta);
|
||||||
|
|
||||||
// Join directives
|
// Join directives
|
||||||
let sumOfDirectives = {};
|
let sumOfDirectives: MermaidConfig = {};
|
||||||
for (let i = 0; i < _directives.length; i++) {
|
for (let i = 0; i < _directives.length; i++) {
|
||||||
const d = _directives[i];
|
const d = _directives[i];
|
||||||
sanitize(d);
|
sanitize(d);
|
||||||
@@ -28,16 +28,15 @@ export const updateCurrentConfig = (siteCfg: MermaidConfig, _directives: any[])
|
|||||||
|
|
||||||
cfg = assignWithDepth(cfg, sumOfDirectives);
|
cfg = assignWithDepth(cfg, sumOfDirectives);
|
||||||
|
|
||||||
// @ts-ignore
|
if (sumOfDirectives.theme && sumOfDirectives.theme in theme) {
|
||||||
if (sumOfDirectives.theme && theme[sumOfDirectives.theme]) {
|
|
||||||
const tmpConfigFromInitialize = assignWithDepth({}, configFromInitialize);
|
const tmpConfigFromInitialize = assignWithDepth({}, configFromInitialize);
|
||||||
const themeVariables = assignWithDepth(
|
const themeVariables = assignWithDepth(
|
||||||
tmpConfigFromInitialize.themeVariables || {},
|
tmpConfigFromInitialize.themeVariables || {},
|
||||||
// @ts-ignore
|
|
||||||
sumOfDirectives.themeVariables
|
sumOfDirectives.themeVariables
|
||||||
);
|
);
|
||||||
// @ts-ignore
|
if (cfg.theme && cfg.theme in theme) {
|
||||||
cfg.themeVariables = theme[cfg.theme].getThemeVariables(themeVariables);
|
cfg.themeVariables = theme[cfg.theme as keyof typeof theme].getThemeVariables(themeVariables);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
currentConfig = cfg;
|
currentConfig = cfg;
|
||||||
|
@@ -147,7 +147,7 @@ export const evaluate = (val: string | boolean): boolean =>
|
|||||||
export const parseGenericTypes = function (text: string): string {
|
export const parseGenericTypes = function (text: string): string {
|
||||||
let cleanedText = text;
|
let cleanedText = text;
|
||||||
|
|
||||||
if (text.indexOf('~') != -1) {
|
if (text.indexOf('~') !== -1) {
|
||||||
cleanedText = cleanedText.replace('~', '<');
|
cleanedText = cleanedText.replace('~', '<');
|
||||||
cleanedText = cleanedText.replace('~', '>');
|
cleanedText = cleanedText.replace('~', '>');
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/detectType';
|
||||||
|
|
||||||
export const gitGraphDetector: DiagramDetector = (txt) => {
|
export const gitGraphDetector: DiagramDetector = (txt) => {
|
||||||
return txt.match(/^\s*gitGraph/) != null;
|
return txt.match(/^\s*gitGraph/) !== null;
|
||||||
};
|
};
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
import { DiagramDetector } from '../../diagram-api/detectType';
|
import { DiagramDetector } from '../../diagram-api/detectType';
|
||||||
|
|
||||||
export const mindmapDetector: DiagramDetector = (txt) => {
|
export const mindmapDetector: DiagramDetector = (txt) => {
|
||||||
return txt.match(/^\s*mindmap/) != null;
|
return txt.match(/^\s*mindmap/) !== null;
|
||||||
};
|
};
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
/** Created by knut on 14-12-11. */
|
/** Created by knut on 14-12-11. */
|
||||||
import { select } from 'd3';
|
import { select } from 'd3';
|
||||||
import { log } from './logger';
|
import { log } from './logger';
|
||||||
|
import { getErrorMessage } from './utils';
|
||||||
|
|
||||||
let conf = {};
|
let conf = {};
|
||||||
|
|
||||||
@@ -89,8 +90,7 @@ export const draw = (id: string, mermaidVersion: string) => {
|
|||||||
svg.attr('viewBox', '768 0 512 512');
|
svg.attr('viewBox', '768 0 512 512');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log.error('Error while rendering info diagram');
|
log.error('Error while rendering info diagram');
|
||||||
// @ts-ignore
|
log.error(getErrorMessage(e));
|
||||||
log.error(e.message);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -6,6 +6,7 @@ import { MermaidConfig } from './config.type';
|
|||||||
import { log } from './logger';
|
import { log } from './logger';
|
||||||
import utils from './utils';
|
import utils from './utils';
|
||||||
import { mermaidAPI } from './mermaidAPI';
|
import { mermaidAPI } from './mermaidAPI';
|
||||||
|
import { isDetailedError } from './utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ## init
|
* ## init
|
||||||
@@ -39,10 +40,10 @@ const init = function (
|
|||||||
initThrowsErrors(config, nodes, callback);
|
initThrowsErrors(config, nodes, callback);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log.warn('Syntax Error rendering');
|
log.warn('Syntax Error rendering');
|
||||||
// @ts-ignore
|
if (isDetailedError(e)) {
|
||||||
log.warn(e.str);
|
log.warn(e.str);
|
||||||
|
}
|
||||||
if (mermaid.parseError) {
|
if (mermaid.parseError) {
|
||||||
// @ts-ignore
|
|
||||||
mermaid.parseError(e);
|
mermaid.parseError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -56,6 +57,7 @@ const initThrowsErrors = function (
|
|||||||
const conf = mermaidAPI.getConfig();
|
const conf = mermaidAPI.getConfig();
|
||||||
// console.log('Starting rendering diagrams (init) - mermaid.init', conf);
|
// console.log('Starting rendering diagrams (init) - mermaid.init', conf);
|
||||||
if (config) {
|
if (config) {
|
||||||
|
// This is a legacy way of setting config. It is not documented and should be removed in the future.
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
mermaid.sequenceConfig = config;
|
mermaid.sequenceConfig = config;
|
||||||
}
|
}
|
||||||
|
14
src/utils.ts
14
src/utils.ts
@@ -933,6 +933,20 @@ export const sanitizeCss = (str) => {
|
|||||||
return str;
|
return str;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export interface DetailedError {
|
||||||
|
str: string;
|
||||||
|
hash: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isDetailedError(error: unknown): error is DetailedError {
|
||||||
|
return 'str' in error;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getErrorMessage(error: unknown): string {
|
||||||
|
if (error instanceof Error) return error.message;
|
||||||
|
return String(error);
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
assignWithDepth,
|
assignWithDepth,
|
||||||
wrapLabel,
|
wrapLabel,
|
||||||
|
Reference in New Issue
Block a user