mirror of https://github.com/iconify/api.git
fix: prevent aliases from messing up search results
This commit is contained in:
parent
21d5bdb462
commit
5c31cfb4fd
|
|
@ -255,21 +255,17 @@ export function generateIconSetIconsTree(iconSet: IconifyJSON, commonChunks?: st
|
|||
iconKeywords.add(chunk);
|
||||
(keywords[chunk] || (keywords[chunk] = new Set())).add(icon);
|
||||
});
|
||||
}
|
||||
|
||||
// Check for length
|
||||
let maxLength = name.length;
|
||||
if (commonChunks) {
|
||||
for (let j = 0; j < commonChunks.length; j++) {
|
||||
const chunk = commonChunks[j];
|
||||
if (name.startsWith(chunk + '-') || name.endsWith('-' + chunk)) {
|
||||
maxLength = name.length - chunk.length - 1;
|
||||
break;
|
||||
}
|
||||
// Check for length based on first name
|
||||
if (commonChunks) {
|
||||
for (let j = 0; j < commonChunks.length; j++) {
|
||||
const chunk = commonChunks[j];
|
||||
if (name.startsWith(chunk + '-') || name.endsWith('-' + chunk)) {
|
||||
icon._l = name.length - chunk.length - 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!icon._l || icon._l > maxLength) {
|
||||
icon._l = maxLength;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ export function storeLoadedIconSet(
|
|||
config: SplitIconSetConfig = splitIconSetConfig
|
||||
) {
|
||||
let themes: StorageIconSetThemes | undefined;
|
||||
let themeChunks: string[] | undefined;
|
||||
let themeParts: string[] | undefined;
|
||||
|
||||
if (appConfig.enableIconLists) {
|
||||
// Get themes
|
||||
|
|
@ -52,14 +52,14 @@ export function storeLoadedIconSet(
|
|||
if (appConfig.enableSearchEngine) {
|
||||
const data = findIconSetThemes(iconSet);
|
||||
if (data.length) {
|
||||
themeChunks = data;
|
||||
themeParts = data;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get icons
|
||||
const icons = generateIconSetIconsTree(iconSet, themeChunks);
|
||||
const icons = generateIconSetIconsTree(iconSet, themeParts);
|
||||
removeBadIconSetItems(iconSet, icons);
|
||||
|
||||
// Fix icons counter
|
||||
|
|
@ -113,6 +113,9 @@ export function storeLoadedIconSet(
|
|||
}
|
||||
if (appConfig.enableIconLists) {
|
||||
result.apiV2IconsCache = prepareAPIv2IconsList(iconSet, icons);
|
||||
if (appConfig.enableSearchEngine && themeParts?.length) {
|
||||
result.themeParts = themeParts;
|
||||
}
|
||||
}
|
||||
done(result);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -188,11 +188,28 @@ export function search(
|
|||
}
|
||||
|
||||
// Find icon name that matches all keywords
|
||||
const name = item.find((name) => {
|
||||
let length: number | undefined;
|
||||
const name = item.find((name, index) => {
|
||||
for (let i = 0; i < testMatches.length; i++) {
|
||||
if (name.indexOf(testMatches[i]) === -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get length
|
||||
if (!index) {
|
||||
// First item sets `_l`, unless it didn't match any prefixes/suffixes
|
||||
length = item._l || name.length;
|
||||
} else if (iconSet.themeParts) {
|
||||
// Alias: calculate length
|
||||
const themeParts = iconSet.themeParts;
|
||||
for (let partIndex = 0; partIndex < themeParts.length; partIndex++) {
|
||||
const part = themeParts[partIndex];
|
||||
if (name.startsWith(part + '-') || name.endsWith('-' + part)) {
|
||||
length = name.length - part.length - 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
|
@ -200,8 +217,7 @@ export function search(
|
|||
// Add icon
|
||||
prefixAddedIcons.add(item);
|
||||
|
||||
const length = item._l ?? name.length;
|
||||
const list = getMatchResult(length, !isExact);
|
||||
const list = getMatchResult(length || name.length, !isExact);
|
||||
list.names.push(prefix + ':' + name);
|
||||
allMatchesLength++;
|
||||
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ export interface StoredIconSet {
|
|||
|
||||
// Themes
|
||||
themes?: StorageIconSetThemes;
|
||||
themeParts?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -83,17 +83,17 @@ describe('Searching icons', () => {
|
|||
'mdi-test-prefix:recycle',
|
||||
'mdi-test-prefix:unicycle',
|
||||
'mdi-test-prefix:motorcycle',
|
||||
'emojione-v1:motorcycle',
|
||||
'mdi-test-prefix:bicycle-cargo',
|
||||
'mdi-test-prefix:water-recycle',
|
||||
'emojione-v1:motorcycle',
|
||||
'mdi-test-prefix:bicycle-basket',
|
||||
'mdi-test-prefix:motorcycle-off',
|
||||
'mdi-test-prefix:battery-recycle',
|
||||
'mdi-test-prefix:battery-recycle-outline',
|
||||
'mdi-test-prefix:bicycle-electric',
|
||||
'mdi-test-prefix:motorcycle-off',
|
||||
'mdi-test-prefix:bicycle-penny-farthing',
|
||||
'mdi-test-prefix:recycle-variant',
|
||||
'mdi-test-prefix:bicycle-electric',
|
||||
'mdi-test-prefix:motorcycle-electric',
|
||||
'mdi-test-prefix:bicycle-penny-farthing',
|
||||
],
|
||||
hasMore: false,
|
||||
});
|
||||
|
|
@ -122,13 +122,13 @@ describe('Searching icons', () => {
|
|||
'mdi-test-prefix:bicycle-cargo',
|
||||
'mdi-test-prefix:water-recycle',
|
||||
'mdi-test-prefix:bicycle-basket',
|
||||
'mdi-test-prefix:motorcycle-off',
|
||||
'mdi-test-prefix:battery-recycle',
|
||||
'mdi-test-prefix:battery-recycle-outline',
|
||||
'mdi-test-prefix:bicycle-electric',
|
||||
'mdi-test-prefix:motorcycle-off',
|
||||
'mdi-test-prefix:bicycle-penny-farthing',
|
||||
'mdi-test-prefix:recycle-variant',
|
||||
'mdi-test-prefix:bicycle-electric',
|
||||
'mdi-test-prefix:motorcycle-electric',
|
||||
'mdi-test-prefix:bicycle-penny-farthing',
|
||||
],
|
||||
hasMore: false,
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue