Skip to content

Commit e0f7ea5

Browse files
committed
fix: Unknown icon size
1 parent 0ea88df commit e0f7ea5

File tree

1 file changed

+46
-19
lines changed
  • packages/mermaid/src/rendering-util

1 file changed

+46
-19
lines changed
Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { log } from '$root/logger.js';
2-
import type { IconifyJSON } from '@iconify/types';
2+
import type { ExtendedIconifyIcon, IconifyIcon, IconifyJSON } from '@iconify/types';
33
import type { IconifyIconCustomisations } from '@iconify/utils';
44
import { getIconData, iconToHTML, iconToSVG, replaceIDs, stringToIcon } from '@iconify/utils';
55

6+
export const unknownIcon: IconifyIcon = {
7+
body: '<g><rect width="80" height="80" style="fill: #087ebf; stroke-width: 0px;"/><text transform="translate(21.16 64.67)" style="fill: #fff; font-family: ArialMT, Arial; font-size: 67.75px;"><tspan x="0" y="0">?</tspan></text></g>',
8+
height: 80,
9+
width: 80,
10+
};
11+
612
export const iconsStore = new Map<string, IconifyJSON>();
713

814
export const registerIconPacks = (...iconPacks: IconifyJSON[]) => {
@@ -11,26 +17,47 @@ export const registerIconPacks = (...iconPacks: IconifyJSON[]) => {
1117
}
1218
};
1319

14-
export const getIconSVG = (iconName: string, customisations?: IconifyIconCustomisations) => {
20+
const getRegisteredIconData = (iconName: string, fallbackPrefix?: string) => {
21+
const data = stringToIcon(iconName, true, fallbackPrefix !== undefined);
22+
if (!data) {
23+
throw new Error(`Invalid icon name: ${iconName}`);
24+
}
25+
const prefix = data.prefix || fallbackPrefix;
26+
if (!prefix) {
27+
throw new Error(`Icon name must contain a prefix: ${iconName}`);
28+
}
29+
const icons = iconsStore.get(prefix);
30+
if (!icons) {
31+
throw new Error(`Icon set not found: ${data.prefix}`);
32+
}
33+
const iconData = getIconData(icons, data.name);
34+
if (!iconData) {
35+
throw new Error(`Icon not found: ${iconName}`);
36+
}
37+
return iconData;
38+
};
39+
40+
export const isIconAvailable = (iconName: string) => {
41+
try {
42+
getRegisteredIconData(iconName);
43+
return true;
44+
} catch {
45+
return false;
46+
}
47+
};
48+
49+
export const getIconSVG = (
50+
iconName: string,
51+
customisations?: IconifyIconCustomisations & { fallbackPrefix?: string }
52+
) => {
53+
let iconData: ExtendedIconifyIcon;
1554
try {
16-
const data = stringToIcon(iconName, true, true);
17-
if (!data) {
18-
throw new Error(`Invalid icon name: ${iconName}`);
19-
}
20-
const icons = iconsStore.get(data.prefix || 'default');
21-
if (!icons) {
22-
throw new Error(`Icon set not found: ${data.prefix}`);
23-
}
24-
const iconData = getIconData(icons, data.name);
25-
if (!iconData) {
26-
throw new Error(`Icon not found: ${iconName}`);
27-
}
28-
const renderData = iconToSVG(iconData, customisations);
29-
const svg = iconToHTML(replaceIDs(renderData.body), renderData.attributes);
30-
return svg;
55+
iconData = getRegisteredIconData(iconName, customisations?.fallbackPrefix);
3156
} catch (e) {
3257
log.error(e);
33-
// Return unknown icon svg.
34-
return '<g><rect width="80" height="80" style="fill: #087ebf; stroke-width: 0px;"/><text transform="translate(21.16 64.67)" style="fill: #fff; font-family: ArialMT, Arial; font-size: 67.75px;"><tspan x="0" y="0">?</tspan></text></g>';
58+
iconData = unknownIcon;
3559
}
60+
const renderData = iconToSVG(iconData, customisations);
61+
const svg = iconToHTML(replaceIDs(renderData.body), renderData.attributes);
62+
return svg;
3663
};

0 commit comments

Comments
 (0)