From 3f5269e4984e2151dfe4fc98d75bde01ab49295e Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Tue, 25 Jul 2023 11:22:39 +0200 Subject: [PATCH] #261 - Fixed supernumerary result --- src/search/omnisearch.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/search/omnisearch.ts b/src/search/omnisearch.ts index f764f62..a5de141 100644 --- a/src/search/omnisearch.ts +++ b/src/search/omnisearch.ts @@ -216,7 +216,7 @@ export class Omnisearch { headings1: settings.weightH1, headings2: settings.weightH2, headings3: settings.weightH3, - unmarkedTags: settings.weightUnmarkedTags + unmarkedTags: settings.weightUnmarkedTags, }, }) @@ -343,7 +343,7 @@ export class Omnisearch { public getMatches(text: string, reg: RegExp, query: Query): SearchMatch[] { const startTime = new Date().getTime() let match: RegExpExecArray | null = null - const matches: SearchMatch[] = [] + let matches: SearchMatch[] = [] let count = 0 while ((match = reg.exec(text)) !== null) { // Avoid infinite loops, stop looking after 100 matches or if we're taking too much time @@ -351,13 +351,14 @@ export class Omnisearch { warnDebug('Stopped getMatches at', count, 'results') break } - const m = match[0] - if (m) matches.push({ match: m, offset: match.index }) + const m = match[2] + if (m) matches.push({ match: m, offset: match.index + 1 }) } // If the query can be found "as is" in the text, put this match first const best = text.toLowerCase().indexOf(query.segmentsToStr()) if (best > -1) { + matches = matches.filter(m => m.offset !== best) matches.unshift({ offset: best, match: query.segmentsToStr(),