fix pnpm lock and use promise.all to speed up icon replacement

This commit is contained in:
saurabhg772244
2025-02-21 23:45:33 +05:30
parent 83a6e696c6
commit 2fb7bc2806
2 changed files with 21 additions and 19 deletions

View File

@@ -2,7 +2,7 @@
// @ts-nocheck TODO: Fix types // @ts-nocheck TODO: Fix types
import { select } from 'd3'; import { select } from 'd3';
import type { MermaidConfig } from '../config.type.js'; import type { MermaidConfig } from '../config.type.js';
import { getConfig } from '../diagram-api/diagramAPI.js'; import { getConfig, sanitizeText } from '../diagram-api/diagramAPI.js';
import type { SVGGroup } from '../diagram-api/types.js'; import type { SVGGroup } from '../diagram-api/types.js';
import common, { hasKatex, renderKatex } from '../diagrams/common/common.js'; import common, { hasKatex, renderKatex } from '../diagrams/common/common.js';
import type { D3TSpanElement, D3TextElement } from '../diagrams/common/commonTypes.js'; import type { D3TSpanElement, D3TextElement } from '../diagrams/common/commonTypes.js';
@@ -192,24 +192,26 @@ export async function replaceIconSubstring(text: string) {
return text; return text;
} }
let newText = text; const replacements = await Promise.all(
matches.map(async ([fullMatch, prefix, iconName]) => {
for (const match of matches) {
const [fullMatch, prefix, iconName] = match;
const registeredIconName = `${prefix}:${iconName}`; const registeredIconName = `${prefix}:${iconName}`;
try { try {
const isIconAvail = await isIconAvailable(registeredIconName); const isIconRegistered = await isIconAvailable(registeredIconName);
if (isIconAvail) { const replacement = isIconRegistered
const faIcon = await getIconSVG(registeredIconName, undefined, { class: 'label-icon' }); ? await getIconSVG(registeredIconName, undefined, { class: 'label-icon' })
newText = newText.replace(fullMatch, faIcon); : `<i class='${sanitizeText(fullMatch).replace(':', ' ')}'></i>`;
} else { return { fullMatch, replacement };
newText = newText.replace(fullMatch, `<i class='${fullMatch.replace(':', ' ')}'></i>`);
}
} catch (error) { } catch (error) {
log.error(`Error processing ${registeredIconName}:`, error); log.error(`Error processing ${registeredIconName}:`, error);
return { fullMatch, replacement: fullMatch };
} }
} })
return newText; );
return replacements.reduce(
(text, { fullMatch, replacement }) => text.replace(fullMatch, replacement),
text
);
} }
// Note when using from flowcharts converting the API isNode means classes should be set accordingly. When using htmlLabels => to sett classes to'nodeLabel' when isNode=true otherwise 'edgeLabel' // Note when using from flowcharts converting the API isNode means classes should be set accordingly. When using htmlLabels => to sett classes to'nodeLabel' when isNode=true otherwise 'edgeLabel'