2021-12-11 20:36:36 +00:00
|
|
|
import type { IconifyJSON } from '@iconify/types';
|
2022-01-10 11:17:05 +00:00
|
|
|
import type { FullIconifyIcon } from '../icon';
|
2022-01-10 11:21:34 +00:00
|
|
|
import { iconToSVG } from '../svg/build';
|
|
|
|
import { getIconData } from '../icon-set/get-icon';
|
2022-02-26 14:12:13 +00:00
|
|
|
import { mergeIconProps } from './utils';
|
2021-12-11 20:36:36 +00:00
|
|
|
import createDebugger from 'debug';
|
2022-01-10 11:17:05 +00:00
|
|
|
import { defaults as DefaultIconCustomizations } from '../customisations';
|
2022-02-26 14:12:13 +00:00
|
|
|
import type { IconifyLoaderOptions } from './types';
|
2021-12-09 21:12:00 +00:00
|
|
|
|
2022-01-10 11:17:05 +00:00
|
|
|
const debug = createDebugger('@iconify-loader:icon');
|
2021-12-09 21:12:00 +00:00
|
|
|
|
2022-01-10 12:43:35 +00:00
|
|
|
export async function searchForIcon(
|
2021-12-12 10:08:44 +00:00
|
|
|
iconSet: IconifyJSON,
|
|
|
|
collection: string,
|
|
|
|
ids: string[],
|
2022-03-04 16:37:21 +00:00
|
|
|
options?: IconifyLoaderOptions
|
2022-01-10 12:43:35 +00:00
|
|
|
): Promise<string | undefined> {
|
2021-12-11 20:36:36 +00:00
|
|
|
let iconData: FullIconifyIcon | null;
|
2022-02-26 17:31:45 +00:00
|
|
|
const { customize } = options?.customizations ?? {};
|
2021-12-09 21:12:00 +00:00
|
|
|
for (const id of ids) {
|
2021-12-11 20:36:36 +00:00
|
|
|
iconData = getIconData(iconSet, id, true);
|
2021-12-09 21:12:00 +00:00
|
|
|
if (iconData) {
|
2021-12-11 20:36:36 +00:00
|
|
|
debug(`${collection}:${id}`);
|
2022-01-10 16:08:40 +00:00
|
|
|
const defaultCustomizations = { ...DefaultIconCustomizations };
|
2021-12-12 10:08:44 +00:00
|
|
|
const { attributes, body } = iconToSVG(
|
|
|
|
iconData,
|
2022-01-10 16:08:40 +00:00
|
|
|
typeof customize === 'function'
|
|
|
|
? customize(defaultCustomizations)
|
|
|
|
: defaultCustomizations
|
2021-12-12 10:08:44 +00:00
|
|
|
);
|
2022-01-10 12:43:35 +00:00
|
|
|
return await mergeIconProps(
|
2022-02-26 16:02:00 +00:00
|
|
|
// DON'T remove space on <svg >
|
|
|
|
`<svg >${body}</svg>`,
|
2022-01-10 12:43:35 +00:00
|
|
|
collection,
|
|
|
|
id,
|
2022-02-26 17:31:45 +00:00
|
|
|
options,
|
2022-03-04 16:37:21 +00:00
|
|
|
() => attributes
|
2022-01-10 16:08:40 +00:00
|
|
|
);
|
2021-12-09 21:12:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|