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
export const getDiagramFromText = async (txt: string, parseError?: Function) => {
export const getDiagramFromText = async (txt: string, parseError?: Function): Promise<Diagram> => {
const type = detectType(txt, configApi.getConfig());
try {
// Trying to find the diagram
@ -94,24 +92,14 @@ export const getDiagramFromText = async (txt: string, parseError?: Function) =>
if (!loader) {
throw new Error(`Diagram ${type} not found.`);
}
// Diagram not avaiable, loading it
// const path = getPathForDiagram(type);
const { diagram } = await loader(); // eslint-disable-line @typescript-eslint/no-explicit-any
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');
// Diagram not available, loading it
const { diagram } = await loader();
registerDiagram(type, diagram, undefined, diagram.injectUtils);
// 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
// logic and can continue
return new Diagram(txt, parseError);
};
export default Diagram;

View File

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