2024-11-25 21:58:56 +02:00
|
|
|
let elkLoaded = false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines whether the ELK extension of Mermaid.js needs to be loaded (which is a relatively large library), based on the
|
|
|
|
|
* front-matter of the diagram and loads the library if needed.
|
2025-01-09 18:07:02 +02:00
|
|
|
*
|
2024-11-25 21:58:56 +02:00
|
|
|
* <p>
|
|
|
|
|
* If the library has already been loaded or the diagram does not require it, the method will exit immediately.
|
2025-01-09 18:07:02 +02:00
|
|
|
*
|
2024-11-25 21:58:56 +02:00
|
|
|
* @param mermaidContent the plain text of the mermaid diagram, potentially including a frontmatter.
|
|
|
|
|
*/
|
2024-12-21 22:39:28 +02:00
|
|
|
export async function loadElkIfNeeded(mermaidContent: string) {
|
2024-11-25 21:58:56 +02:00
|
|
|
if (elkLoaded) {
|
|
|
|
|
// Exit immediately since the ELK library is already loaded.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parsedContent = await mermaid.parse(mermaidContent, {
|
2025-01-09 18:07:02 +02:00
|
|
|
suppressErrors: true
|
2024-11-25 21:58:56 +02:00
|
|
|
});
|
|
|
|
|
if (parsedContent?.config?.layout === "elk") {
|
|
|
|
|
elkLoaded = true;
|
2025-01-17 19:29:20 +02:00
|
|
|
mermaid.registerLayoutLoaders((await import("@mermaid-js/layout-elk")).default);
|
2025-01-09 18:07:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-03-06 21:18:36 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Processes the output of a Mermaid SVG render before it should be delivered to the user.
|
|
|
|
|
*
|
|
|
|
|
* <p>
|
|
|
|
|
* Currently this fixes <br> to <br/> which would otherwise cause an invalid XML.
|
|
|
|
|
*
|
|
|
|
|
* @param svg the Mermaid SVG to process.
|
|
|
|
|
* @returns the processed SVG.
|
|
|
|
|
*/
|
|
|
|
|
export function postprocessMermaidSvg(svg: string) {
|
|
|
|
|
return svg.replaceAll(/<br\s*>/ig, "<br/>");
|
|
|
|
|
}
|