From 7b278b041c4f09b512fc498a2eb54aef3cab845c Mon Sep 17 00:00:00 2001 From: Benjamin Canac Date: Thu, 22 Aug 2024 16:57:05 +0200 Subject: [PATCH] fix(fuse): prevent indices highlight of a single char --- src/runtime/utils/fuse.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/runtime/utils/fuse.ts b/src/runtime/utils/fuse.ts index d6ca245a..02745817 100644 --- a/src/runtime/utils/fuse.ts +++ b/src/runtime/utils/fuse.ts @@ -33,12 +33,17 @@ function truncateHTMLFromStart(html: string, maxLength: number) { } export function highlight(item: T & { matches?: FuseResult['matches'] }, searchTerm: string, forceKey?: string, omitKeys?: string[]) { - const generateHighlightedText = (value: FuseResultMatch['value'], indices: FuseResultMatch['indices'] = []) => { + function generateHighlightedText(value: FuseResultMatch['value'], indices: FuseResultMatch['indices'] = []) { value = value || '' let content = '' let nextUnhighlightedRegionStartingIndex = 0 indices.forEach((region) => { + // skip if region is a single character + if (region.length === 2 && region[0] === region[1]) { + return + } + const lastIndiceNextIndex = region[1] + 1 const isMatched = (lastIndiceNextIndex - region[0]) >= searchTerm.length