fix: Optional detector

This commit is contained in:
Sidharth Vinod 2022-10-08 13:38:40 +08:00
parent 97b39bca95
commit de5ad8644e
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD
2 changed files with 10 additions and 20 deletions

View File

@ -81,10 +81,8 @@ export class Diagram {
} }
} }
export default Diagram;
// eslint-disable-next-line @typescript-eslint/ban-types // eslint-disable-next-line @typescript-eslint/ban-types
export const getDiagramFromText = async (txt: string, parseError?: Function) => { export const getDiagramFromText = async (txt: string, parseError?: Function): Promise<Diagram> => {
const type = detectType(txt, configApi.getConfig()); const type = detectType(txt, configApi.getConfig());
try { try {
// Trying to find the diagram // Trying to find the diagram
@ -94,24 +92,14 @@ export const getDiagramFromText = async (txt: string, parseError?: Function) =>
if (!loader) { if (!loader) {
throw new Error(`Diagram ${type} not found.`); throw new Error(`Diagram ${type} not found.`);
} }
// Diagram not avaiable, loading it // Diagram not available, loading it
// const path = getPathForDiagram(type); const { diagram } = await loader();
const { diagram } = await loader(); // eslint-disable-line @typescript-eslint/no-explicit-any registerDiagram(type, diagram, undefined, diagram.injectUtils);
registerDiagram(
type,
{
db: diagram.db,
renderer: diagram.renderer,
parser: diagram.parser,
styles: diagram.styles,
},
diagram.injectUtils
);
// await loadDiagram('./packages/mermaid-mindmap/dist/mermaid-mindmap.js');
// await loadDiagram(path + 'mermaid-' + type + '.js');
// new diagram will try getDiagram again and if fails then it is a valid throw // new diagram will try getDiagram again and if fails then it is a valid throw
} }
// If either of the above worked, we have the diagram // If either of the above worked, we have the diagram
// logic and can continue // logic and can continue
return new Diagram(txt, parseError); return new Diagram(txt, parseError);
}; };
export default Diagram;

View File

@ -26,7 +26,7 @@ export interface Detectors {
export const registerDiagram = ( export const registerDiagram = (
id: string, id: string,
diagram: DiagramDefinition, diagram: DiagramDefinition,
detector: DiagramDetector, detector?: DiagramDetector,
callback?: ( callback?: (
_log: any, _log: any,
_setLogLevel: any, _setLogLevel: any,
@ -39,7 +39,9 @@ export const registerDiagram = (
throw new Error(`Diagram ${id} already registered.`); throw new Error(`Diagram ${id} already registered.`);
} }
diagrams[id] = diagram; diagrams[id] = diagram;
addDetector(id, detector); if (detector) {
addDetector(id, detector);
}
addStylesForDiagram(id, diagram.styles); addStylesForDiagram(id, diagram.styles);
if (typeof callback !== 'undefined') { if (typeof callback !== 'undefined') {
callback(log, setLogLevel, getConfig, sanitizeText, setupGraphViewbox); callback(log, setLogLevel, getConfig, sanitizeText, setupGraphViewbox);