feat: enhance search with include/exclude tokens and improved sorting

- Add token parsing to support include/exclude search terms using "-" prefix
- Implement token-based matching logic for relative path searches
- Improve search result sorting by prioritizing prefix matches and match position
- Add frontend test for multi-token highlighting with exclusion support
This commit is contained in:
Will Miao
2025-11-21 19:48:43 +08:00
parent 9a789f8f08
commit ea1d1a49c9
4 changed files with 167 additions and 12 deletions

View File

@@ -32,6 +32,25 @@ function removeLoraExtension(fileName = '') {
return fileName.replace(/\.(safetensors|ckpt|pt|bin)$/i, '');
}
function parseSearchTokens(term = '') {
const include = [];
const exclude = [];
term.split(/\s+/).forEach((rawTerm) => {
const token = rawTerm.trim();
if (!token) {
return;
}
if (token.startsWith('-') && token.length > 1) {
exclude.push(token.slice(1).toLowerCase());
} else {
include.push(token.toLowerCase());
}
});
return { include, exclude };
}
function createDefaultBehavior(modelType) {
return {
enablePreview: false,
@@ -393,10 +412,20 @@ class AutoComplete {
}
highlightMatch(text, searchTerm) {
if (!searchTerm) return text;
const regex = new RegExp(`(${searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi');
return text.replace(regex, '<span style="background-color: rgba(66, 153, 225, 0.3); color: white; padding: 1px 2px; border-radius: 2px;">$1</span>');
const { include } = parseSearchTokens(searchTerm);
const sanitizedTokens = include
.filter(Boolean)
.map((token) => token.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
if (!sanitizedTokens.length) {
return text;
}
const regex = new RegExp(`(${sanitizedTokens.join('|')})`, 'gi');
return text.replace(
regex,
'<span style="background-color: rgba(66, 153, 225, 0.3); color: white; padding: 1px 2px; border-radius: 2px;">$1</span>',
);
}
showPreviewForItem(relativePath, itemElement) {