Skip to content

Commit b76987a

Browse files
committed
Improve search scoring and fix patterns/examples not being included by mistake
1 parent 035c6d8 commit b76987a

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

src/components/syntaxes/SyntaxDisplayManager.astro

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,19 @@ export interface VersionFilterData {
302302
return Number.MAX_SAFE_INTEGER;
303303
}
304304

305+
const escapedSearch = searchLower.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
306+
const wordBoundaryRegex = new RegExp(`\\b${escapedSearch}\\b`);
307+
305308
// Weigh Name
306309
const name = item.syntaxName.toLowerCase();
307310
if (name === searchLower) { // Exact match in name = highest score
308311
score += 1000;
309312
} else if (id.startsWith(searchLower) || name.startsWith(searchLower)) {
310313
score += 500;
314+
} else if (wordBoundaryRegex.test(id) || wordBoundaryRegex.test(name)) {
315+
score += 300;
316+
// prefer shorter names (more specific matches)
317+
score += Math.max(0, 50 - name.length);
311318
} else if (id.includes(searchLower) || name.includes(searchLower)) {
312319
score += 200;
313320
// prefer shorter names (more specific matches)
@@ -325,20 +332,26 @@ export interface VersionFilterData {
325332
}
326333

327334
// Weigh Patterns
328-
const patterns = getCodeFromSection(element, 'Patterns');
329-
if (patterns.includes(searchLower)) {
330-
score += 100;
335+
const patterns = getCodeFromSection(element, 'Syntax Patterns');
336+
if (wordBoundaryRegex.test(patterns)) {
337+
score += 300;
338+
} else if (patterns.includes(searchLower)) {
339+
score += 150;
331340
}
332341

333342
// Weigh Description
334343
const description = element.querySelector('p')?.textContent?.toLowerCase() || '';
335-
if (description.includes(searchLower)) {
344+
if (wordBoundaryRegex.test(description)) {
345+
score += 100;
346+
} else if (description.includes(searchLower)) {
336347
score += 50;
337348
}
338349

339350
// Weigh Examples
340-
const examples = getCodeFromSection(element, 'Examples');
341-
if (examples.includes(searchLower)) {
351+
const examples = getCodeFromSection(element, 'Usage Examples');
352+
if (wordBoundaryRegex.test(examples)) {
353+
score += 50;
354+
} else if (examples.includes(searchLower)) {
342355
score += 25;
343356
}
344357

0 commit comments

Comments
 (0)