From e17767261884cbcf6145803bdf635c3c1fa98f04 Mon Sep 17 00:00:00 2001 From: Kiril Videlov Date: Fri, 10 Mar 2023 15:36:27 +0100 Subject: [PATCH] fix a bug with highlighting of search terms --- .../search/RenderedSearchResult.svelte | 3 ++- src/lib/components/search/index.ts | 4 +++- src/lib/components/search/process.ts | 19 ++++++++++--------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/lib/components/search/RenderedSearchResult.svelte b/src/lib/components/search/RenderedSearchResult.svelte index dd41c77c8..710014398 100644 --- a/src/lib/components/search/RenderedSearchResult.svelte +++ b/src/lib/components/search/RenderedSearchResult.svelte @@ -31,7 +31,8 @@ : line.operation === 'remove' ? 'bg-[#FF0000]/20' : ''} - ">{@html line.content} + ">{line.contentBeforeHit}{line.contentAtHit}{line.contentAfterHit} {:else} diff --git a/src/lib/components/search/index.ts b/src/lib/components/search/index.ts index cda085802..f5c242c43 100644 --- a/src/lib/components/search/index.ts +++ b/src/lib/components/search/index.ts @@ -4,7 +4,9 @@ import type { SearchResult } from '$lib/search'; export type ProcessedSearchResultLine = { hidden: boolean; - content: string; + contentBeforeHit: string; + contentAtHit: string; + contentAfterHit: string; operation: string; lineNumber: number | undefined; hasKeyword: boolean; diff --git a/src/lib/components/search/process.ts b/src/lib/components/search/process.ts index 2cac62b7f..45d1aa630 100644 --- a/src/lib/components/search/process.ts +++ b/src/lib/components/search/process.ts @@ -84,23 +84,24 @@ const processHunkLines = (lines: string[], newStart: number, query: string) => { for (let i = 0; i < lines.length; i++) { const line = lines[i]; - let content = ''; + let contentBeforeHit = ''; + let querySubstring = ''; + let contentAfterHit = ''; if (!line.includes(query)) { - content = line.slice(1); + contentBeforeHit = line.slice(1); } else { const firstCharIndex = line.indexOf(query); const lastCharIndex = firstCharIndex + query.length - 1; - const beforeQuery = line.slice(1, firstCharIndex); - const querySubstring = line.slice(firstCharIndex, lastCharIndex + 1); - const afterQuery = line.slice(lastCharIndex + 1); - - content = - beforeQuery + `${querySubstring}` + afterQuery; + contentBeforeHit = line.slice(1, firstCharIndex); + querySubstring = line.slice(firstCharIndex, lastCharIndex + 1); + contentAfterHit = line.slice(lastCharIndex + 1); } outLines.push({ hidden: false, - content: content, + contentBeforeHit: contentBeforeHit, + contentAtHit: querySubstring, + contentAfterHit: contentAfterHit, operation: line.startsWith('+') ? 'add' : line.startsWith('-') ? 'remove' : 'unmodified', lineNumber: !line.startsWith('-') ? lineNumber : undefined, hasKeyword: line.includes(query)