-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathsearch.ts
More file actions
53 lines (46 loc) · 1.81 KB
/
Copy pathsearch.ts
File metadata and controls
53 lines (46 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { ICON_SERVICE_URL } from '../../constants';
import { fetchWithCache } from '../../utils';
import { loadImageBase64Resource } from './image';
import { loadRemoteResource } from './remote';
import { loadSVGResource } from './svg';
const queryIcon = async (query: string): Promise<string | null> => {
try {
const params = new URLSearchParams({ text: query, topK: '1' });
const url = `${ICON_SERVICE_URL}?${params.toString()}`;
const response = await fetchWithCache(url);
if (!response.ok) return null;
const result = await response.json();
if (!result?.success || !Array.isArray(result.data)) return null;
return (result.data[0] as string) || null;
} catch (error) {
console.error(`Failed to query icon for "${query}":`, error);
return null;
}
};
function isDataURI(resource: string): boolean {
return resource.startsWith('data:');
}
function looksLikeSVG(resource: string): boolean {
const str = resource.trim();
return str.startsWith('<svg') || str.startsWith('<symbol');
}
export async function loadSearchResource(query: string, format?: string) {
if (!query) return null;
const result = await queryIcon(query);
if (!result) return null;
if (looksLikeSVG(result)) return loadSVGResource(result);
if (isDataURI(result)) {
const mimeType = result.match(/^data:([^;]+)/)?.[1] || '';
const isBase64 = result.includes(';base64,');
if (mimeType === 'image/svg+xml' && !isBase64) {
const commaIndex = result.indexOf(',');
const svgText = commaIndex >= 0 ? result.slice(commaIndex + 1) : result;
return loadSVGResource(svgText);
}
if (mimeType === 'image/svg+xml' && format === 'svg' && isBase64) {
return loadImageBase64Resource(result);
}
return loadImageBase64Resource(result);
}
return loadRemoteResource(result, format);
}