diff --git a/packages/mermaid/src/rendering-util/render.ts b/packages/mermaid/src/rendering-util/render.ts index 442780c75..4c08b5d27 100644 --- a/packages/mermaid/src/rendering-util/render.ts +++ b/packages/mermaid/src/rendering-util/render.ts @@ -1,3 +1,5 @@ +import { log } from '$root/logger.js'; + export interface LayoutAlgorithm { render(data4Layout: any, svg: any, element: any, algorithm?: string): any; } @@ -24,10 +26,6 @@ const registerDefaultLayoutLoaders = () => { name: 'dagre', loader: async () => await import('./layout-algorithms/dagre/index.js'), }, - // { - // name: 'elk', - // loader: async () => await import('../../../mermaid-layout-elk/src/render.js'), - // }, ]); }; @@ -42,3 +40,17 @@ export const render = async (data4Layout: any, svg: any, element: any) => { const layoutRenderer = await layoutDefinition.loader(); return layoutRenderer.render(data4Layout, svg, element, layoutDefinition.algorithm); }; + +/** + * Get the registered layout algorithm. If the algorithm is not registered, use the fallback algorithm. + */ +export const getRegisteredLayoutAlgorithm = (algorithm = '', { fallback = 'dagre' } = {}) => { + if (algorithm in layoutAlgorithms) { + return algorithm; + } + if (fallback in layoutAlgorithms) { + log.warn(`Layout algorithm ${algorithm} is not registered. Using ${fallback} as fallback.`); + return fallback; + } + throw new Error(`Both layout algorithms ${algorithm} and ${fallback} are not registered.`); +};