Skip to content

Commit 2a99d94

Browse files
committed
Fix SEARCH_HIGHLIGHT_LIMIT to apply per snippet instead of per text block
1 parent 3bb6323 commit 2a99d94

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

public/js/app.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,15 @@ document.addEventListener('DOMContentLoaded', () => {
266266
});
267267

268268
if (hasMatch) {
269+
const limit = window.TAKECODE_CONFIG?.SEARCH_HIGHLIGHT_LIMIT ?? 3;
270+
let highlightCount = { value: 0 };
269271
snippet.activeTabIndex = activeTabIndex;
270-
snippet.highlightedContent = highlightedContent;
272+
snippet.highlightedContent = highlightedContent.map(content => ({
273+
...content,
274+
highlightedValue: highlightSearchMatches(content.value, query, highlightCount, limit)
275+
}));
276+
snippet.highlightedName = highlightSearchMatches(snippet.name, query, highlightCount, limit);
277+
snippet.highlightedDescription = snippet.description ? highlightSearchMatches(snippet.description, query, highlightCount, limit) : null;
271278
return true;
272279
}
273280
return false;
@@ -277,9 +284,7 @@ document.addEventListener('DOMContentLoaded', () => {
277284
highlightedContent: snippet.highlightedContent || snippet.content.map(content => ({
278285
...content,
279286
highlightedValue: content.value
280-
})),
281-
highlightedName: highlightSearchMatches(snippet.name, query),
282-
highlightedDescription: snippet.description ? highlightSearchMatches(snippet.description, query) : null
287+
}))
283288
}));
284289

285290
currentSnippets = filteredSnippets;
@@ -433,18 +438,18 @@ document.addEventListener('DOMContentLoaded', () => {
433438
}
434439

435440
// Function to highlight search matches in text
436-
function highlightSearchMatches(text, query) {
441+
function highlightSearchMatches(text, query, highlightCount, limit) {
437442
if (!query.trim()) return text;
438443

439-
const highlightLimit = window.TAKECODE_CONFIG?.SEARCH_HIGHLIGHT_LIMIT ?? 3;
444+
const effectiveLimit = limit !== undefined ? limit : -1;
445+
const effectiveHighlightCount = highlightCount || { value: 0 };
440446

441447
try {
442448
// Try to use the query as a regex pattern
443449
const regex = new RegExp(`(${query})`, 'g'); // case-sensitive, global
444-
let matchCount = 0;
445450
return text.replace(regex, (match, group1) => {
446-
matchCount++;
447-
if (highlightLimit === -1 || matchCount <= highlightLimit) {
451+
if (effectiveLimit === -1 || effectiveHighlightCount.value < effectiveLimit) {
452+
effectiveHighlightCount.value++;
448453
return `<mark class="search-highlight">${group1}</mark>`;
449454
}
450455
return group1; // Return without highlighting for matches beyond limit
@@ -453,10 +458,9 @@ document.addEventListener('DOMContentLoaded', () => {
453458
// If regex is invalid, escape special characters and search as literal string
454459
const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
455460
const regex = new RegExp(`(${escapedQuery})`, 'g');
456-
let matchCount = 0;
457461
return text.replace(regex, (match, group1) => {
458-
matchCount++;
459-
if (highlightLimit === -1 || matchCount <= highlightLimit) {
462+
if (effectiveLimit === -1 || effectiveHighlightCount.value < effectiveLimit) {
463+
effectiveHighlightCount.value++;
460464
return `<mark class="search-highlight">${group1}</mark>`;
461465
}
462466
return group1; // Return without highlighting for matches beyond limit

0 commit comments

Comments
 (0)