mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-19 23:39:50 +02:00
fix: Load all extraDiagrams
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
import DOMPurify from 'dompurify';
|
import DOMPurify from 'dompurify';
|
||||||
|
|
||||||
export interface MermaidConfig {
|
export interface MermaidConfig {
|
||||||
extraDiagrams?: any;
|
extraDiagrams?: string[];
|
||||||
theme?: string;
|
theme?: string;
|
||||||
themeVariables?: any;
|
themeVariables?: any;
|
||||||
themeCSS?: string;
|
themeCSS?: string;
|
||||||
|
@@ -1,8 +1,6 @@
|
|||||||
import { MermaidConfig } from '../config.type';
|
import { MermaidConfig } from '../config.type';
|
||||||
|
import { DetectorRecord, DiagramDetector, DiagramLoader } from './types';
|
||||||
|
|
||||||
export type DiagramDetector = (text: string, config?: MermaidConfig) => boolean;
|
|
||||||
export type DiagramLoader = (() => Promise<unknown>) | null;
|
|
||||||
export type DetectorRecord = { detector: DiagramDetector; loader: DiagramLoader };
|
|
||||||
const directive =
|
const directive =
|
||||||
/[%]{2}[{]\s*(?:(?:(\w+)\s*:|(\w+))\s*(?:(?:(\w+))|((?:(?![}][%]{2}).|\r?\n)*))?\s*)(?:[}][%]{2})?/gi;
|
/[%]{2}[{]\s*(?:(?:(\w+)\s*:|(\w+))\s*(?:(?:(\w+))|((?:(?![}][%]{2}).|\r?\n)*))?\s*)(?:[}][%]{2})?/gi;
|
||||||
const anyComment = /\s*%%.*\n/gm;
|
const anyComment = /\s*%%.*\n/gm;
|
||||||
@@ -44,11 +42,10 @@ export const detectType = function (text: string, config?: MermaidConfig): strin
|
|||||||
throw new Error(`No diagram type detected for text: ${text}`);
|
throw new Error(`No diagram type detected for text: ${text}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const addDetector = (
|
export const addDetector = (key: string, detector: DiagramDetector, loader?: DiagramLoader) => {
|
||||||
key: string,
|
if (detectors[key]) {
|
||||||
detector: DiagramDetector,
|
throw new Error(`Detector with key ${key} already exists`);
|
||||||
loader: DiagramLoader | null
|
}
|
||||||
) => {
|
|
||||||
detectors[key] = { detector, loader };
|
detectors[key] = { detector, loader };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -1,9 +1,5 @@
|
|||||||
import {
|
import { registerDiagram, registerDetector } from './diagramAPI';
|
||||||
registerDiagram,
|
import { DiagramDefinition, DiagramDetector } from './types';
|
||||||
registerDetector,
|
|
||||||
DiagramDefinition,
|
|
||||||
DiagramDetector,
|
|
||||||
} from './diagramAPI';
|
|
||||||
|
|
||||||
// // @ts-ignore: TODO Fix ts errors
|
// // @ts-ignore: TODO Fix ts errors
|
||||||
// import mindmapParser from '../diagrams/mindmap/parser/mindmap';
|
// import mindmapParser from '../diagrams/mindmap/parser/mindmap';
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
import { detectType, DiagramDetector } from './detectType';
|
import { detectType } from './detectType';
|
||||||
import { getDiagram, registerDiagram, registerDetector } from './diagramAPI';
|
import { getDiagram, registerDiagram, registerDetector } from './diagramAPI';
|
||||||
import { addDiagrams } from './diagram-orchestration';
|
import { addDiagrams } from './diagram-orchestration';
|
||||||
|
import { DiagramDetector } from './types';
|
||||||
|
|
||||||
addDiagrams();
|
addDiagrams();
|
||||||
|
|
||||||
|
@@ -1,10 +1,10 @@
|
|||||||
import { addDetector, DiagramDetector as _DiagramDetector } from './detectType';
|
import { addDetector } from './detectType';
|
||||||
import { log as _log, setLogLevel as _setLogLevel } from '../logger';
|
import { log as _log, setLogLevel as _setLogLevel } from '../logger';
|
||||||
import { getConfig as _getConfig } from '../config';
|
import { getConfig as _getConfig } from '../config';
|
||||||
import { sanitizeText as _sanitizeText } from '../diagrams/common/common';
|
import { sanitizeText as _sanitizeText } from '../diagrams/common/common';
|
||||||
import { MermaidConfig } from '../config.type';
|
|
||||||
import { setupGraphViewbox as _setupGraphViewbox } from '../setupGraphViewbox';
|
import { setupGraphViewbox as _setupGraphViewbox } from '../setupGraphViewbox';
|
||||||
import { addStylesForDiagram } from '../styles';
|
import { addStylesForDiagram } from '../styles';
|
||||||
|
import { DiagramDefinition, DiagramDetector } from './types';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Packaging and exposing resources for externa diagrams so that they can import
|
Packaging and exposing resources for externa diagrams so that they can import
|
||||||
@@ -13,28 +13,10 @@ import { addStylesForDiagram } from '../styles';
|
|||||||
*/
|
*/
|
||||||
export const log = _log;
|
export const log = _log;
|
||||||
export const setLogLevel = _setLogLevel;
|
export const setLogLevel = _setLogLevel;
|
||||||
export type DiagramDetector = _DiagramDetector;
|
|
||||||
export const getConfig = _getConfig;
|
export const getConfig = _getConfig;
|
||||||
export const sanitizeText = (text: string) => _sanitizeText(text, getConfig());
|
export const sanitizeText = (text: string) => _sanitizeText(text, getConfig());
|
||||||
export const setupGraphViewbox = _setupGraphViewbox;
|
export const setupGraphViewbox = _setupGraphViewbox;
|
||||||
|
|
||||||
export interface InjectUtils {
|
|
||||||
_log: any;
|
|
||||||
_setLogLevel: any;
|
|
||||||
_getConfig: any;
|
|
||||||
_sanitizeText: any;
|
|
||||||
_setupGraphViewbox: any;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface DiagramDefinition {
|
|
||||||
db: any;
|
|
||||||
renderer: any;
|
|
||||||
parser: any;
|
|
||||||
styles: any;
|
|
||||||
init?: (config: MermaidConfig) => void;
|
|
||||||
injectUtils?: (utils: InjectUtils) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
const diagrams: Record<string, DiagramDefinition> = {};
|
const diagrams: Record<string, DiagramDefinition> = {};
|
||||||
const connectCallbacks: Record<string, any> = {}; // TODO fix, eslint-disable-line @typescript-eslint/no-explicit-any
|
const connectCallbacks: Record<string, any> = {}; // TODO fix, eslint-disable-line @typescript-eslint/no-explicit-any
|
||||||
export interface Detectors {
|
export interface Detectors {
|
||||||
|
26
packages/mermaid/src/diagram-api/types.ts
Normal file
26
packages/mermaid/src/diagram-api/types.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import { MermaidConfig } from '../config.type';
|
||||||
|
|
||||||
|
export interface InjectUtils {
|
||||||
|
_log: any;
|
||||||
|
_setLogLevel: any;
|
||||||
|
_getConfig: any;
|
||||||
|
_sanitizeText: any;
|
||||||
|
_setupGraphViewbox: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DiagramDefinition {
|
||||||
|
db: any;
|
||||||
|
renderer: any;
|
||||||
|
parser: any;
|
||||||
|
styles: any;
|
||||||
|
init?: (config: MermaidConfig) => void;
|
||||||
|
injectUtils?: (utils: InjectUtils) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DetectorRecord {
|
||||||
|
detector: DiagramDetector;
|
||||||
|
loader?: DiagramLoader;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type DiagramDetector = (text: string, config?: MermaidConfig) => boolean;
|
||||||
|
export type DiagramLoader = (() => Promise<{ id: string; diagram: DiagramDefinition }>) | null;
|
@@ -1,4 +1,4 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/types';
|
||||||
|
|
||||||
export const c4Detector: DiagramDetector = (txt) => {
|
export const c4Detector: DiagramDetector = (txt) => {
|
||||||
return txt.match(/^\s*C4Context|C4Container|C4Component|C4Dynamic|C4Deployment/) !== null;
|
return txt.match(/^\s*C4Context|C4Container|C4Component|C4Dynamic|C4Deployment/) !== null;
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/types';
|
||||||
|
|
||||||
export const classDetectorV2: DiagramDetector = (txt, config) => {
|
export const classDetectorV2: DiagramDetector = (txt, config) => {
|
||||||
// If we have confgured to use dagre-wrapper then we should return true in this function for classDiagram code thus making it use the new class diagram
|
// If we have confgured to use dagre-wrapper then we should return true in this function for classDiagram code thus making it use the new class diagram
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/types';
|
||||||
|
|
||||||
export const classDetector: DiagramDetector = (txt, config) => {
|
export const classDetector: DiagramDetector = (txt, config) => {
|
||||||
// If we have confgured to use dagre-wrapper then we should never return true in this function
|
// If we have confgured to use dagre-wrapper then we should never return true in this function
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/types';
|
||||||
|
|
||||||
export const erDetector: DiagramDetector = (txt) => {
|
export const erDetector: DiagramDetector = (txt) => {
|
||||||
return txt.match(/^\s*erDiagram/) !== null;
|
return txt.match(/^\s*erDiagram/) !== null;
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/types';
|
||||||
|
|
||||||
export const flowDetectorV2: DiagramDetector = (txt, config) => {
|
export const flowDetectorV2: DiagramDetector = (txt, config) => {
|
||||||
// If we have confgured to use dagre-wrapper then we should return true in this function for graph code thus making it use the new flowchart diagram
|
// If we have confgured to use dagre-wrapper then we should return true in this function for graph code thus making it use the new flowchart diagram
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/types';
|
||||||
|
|
||||||
export const flowDetector: DiagramDetector = (txt, config) => {
|
export const flowDetector: DiagramDetector = (txt, config) => {
|
||||||
// If we have confired to only use new flow charts this function shohuld always return false
|
// If we have confired to only use new flow charts this function shohuld always return false
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/types';
|
||||||
|
|
||||||
export const ganttDetector: DiagramDetector = (txt) => {
|
export const ganttDetector: DiagramDetector = (txt) => {
|
||||||
return txt.match(/^\s*gantt/) !== null;
|
return txt.match(/^\s*gantt/) !== null;
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/types';
|
||||||
|
|
||||||
export const gitGraphDetector: DiagramDetector = (txt) => {
|
export const gitGraphDetector: DiagramDetector = (txt) => {
|
||||||
return txt.match(/^\s*gitGraph/) !== null;
|
return txt.match(/^\s*gitGraph/) !== null;
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/types';
|
||||||
|
|
||||||
export const infoDetector: DiagramDetector = (txt) => {
|
export const infoDetector: DiagramDetector = (txt) => {
|
||||||
return txt.match(/^\s*info/) !== null;
|
return txt.match(/^\s*info/) !== null;
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/types';
|
||||||
|
|
||||||
export const pieDetector: DiagramDetector = (txt) => {
|
export const pieDetector: DiagramDetector = (txt) => {
|
||||||
return txt.match(/^\s*pie/) !== null;
|
return txt.match(/^\s*pie/) !== null;
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/types';
|
||||||
|
|
||||||
export const requirementDetector: DiagramDetector = (txt) => {
|
export const requirementDetector: DiagramDetector = (txt) => {
|
||||||
return txt.match(/^\s*requirement(Diagram)?/) !== null;
|
return txt.match(/^\s*requirement(Diagram)?/) !== null;
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/types';
|
||||||
|
|
||||||
export const sequenceDetector: DiagramDetector = (txt) => {
|
export const sequenceDetector: DiagramDetector = (txt) => {
|
||||||
return txt.match(/^\s*sequenceDiagram/) !== null;
|
return txt.match(/^\s*sequenceDiagram/) !== null;
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/types';
|
||||||
|
|
||||||
export const stateDetectorV2: DiagramDetector = (text, config) => {
|
export const stateDetectorV2: DiagramDetector = (text, config) => {
|
||||||
if (text.match(/^\s*stateDiagram-v2/) !== null) return true;
|
if (text.match(/^\s*stateDiagram-v2/) !== null) return true;
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/types';
|
||||||
|
|
||||||
export const stateDetector: DiagramDetector = (txt, config) => {
|
export const stateDetector: DiagramDetector = (txt, config) => {
|
||||||
// If we have confired to only use new state diagrams this function should always return false
|
// If we have confired to only use new state diagrams this function should always return false
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import type { DiagramDetector } from '../../diagram-api/detectType';
|
import type { DiagramDetector } from '../../diagram-api/types';
|
||||||
|
|
||||||
export const journeyDetector: DiagramDetector = (txt) => {
|
export const journeyDetector: DiagramDetector = (txt) => {
|
||||||
return txt.match(/^\s*journey/) !== null;
|
return txt.match(/^\s*journey/) !== null;
|
||||||
|
@@ -9,13 +9,13 @@ import { mermaidAPI } from './mermaidAPI';
|
|||||||
import { addDetector } from './diagram-api/detectType';
|
import { addDetector } from './diagram-api/detectType';
|
||||||
import {
|
import {
|
||||||
registerDiagram,
|
registerDiagram,
|
||||||
DiagramDefinition,
|
|
||||||
setLogLevel,
|
setLogLevel,
|
||||||
getConfig,
|
getConfig,
|
||||||
setupGraphViewbox,
|
setupGraphViewbox,
|
||||||
sanitizeText,
|
sanitizeText,
|
||||||
} from './diagram-api/diagramAPI';
|
} from './diagram-api/diagramAPI';
|
||||||
import { isDetailedError } from './utils';
|
import { isDetailedError } from './utils';
|
||||||
|
import { DiagramDefinition } from './diagram-api/types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ## init
|
* ## init
|
||||||
@@ -54,12 +54,14 @@ const init = async function (
|
|||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
log.info('Detectors in init', mermaid.detectors); // eslint-disable-line
|
log.info('Detectors in init', mermaid.detectors); // eslint-disable-line
|
||||||
const conf = mermaidAPI.getConfig();
|
const conf = config; // TODO OR mermaidAPI.getConfig(); ?
|
||||||
if (typeof conf.extraDiagrams !== 'undefined' && conf.extraDiagrams.length > 0) {
|
if (conf?.extraDiagrams && conf.extraDiagrams.length > 0) {
|
||||||
// config.extraDiagrams.forEach(async (diagram: string) => {
|
await Promise.allSettled(
|
||||||
const { id, detector, loadDiagram } = await import(conf.extraDiagrams[0]);
|
conf.extraDiagrams.map(async (diagram: string) => {
|
||||||
addDetector(id, detector, loadDiagram);
|
const { id, detector, loadDiagram } = await import(diagram);
|
||||||
// });
|
addDetector(id, detector, loadDiagram);
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
mermaid.detectors.forEach(({ id, detector, path }) => {
|
mermaid.detectors.forEach(({ id, detector, path }) => {
|
||||||
addDetector(id, detector, path);
|
addDetector(id, detector, path);
|
||||||
|
Reference in New Issue
Block a user