diff --git a/src/search/query.ts b/src/search/query.ts index 5b5873b..a6d7dba 100644 --- a/src/search/query.ts +++ b/src/search/query.ts @@ -98,4 +98,13 @@ export class Query { ), ] } + + public getBestStringForExcerpt(): string { + // If we have quoted expressions, return the longest one + if (this.#inQuotes.length) { + return this.#inQuotes.sort((a, b) => b.length - a.length)[0] ?? '' + } + // Otherwise, just return the query as is + return this.segmentsToStr() + } } diff --git a/src/tools/text-processing.ts b/src/tools/text-processing.ts index 5cfcbe0..44db0ac 100644 --- a/src/tools/text-processing.ts +++ b/src/tools/text-processing.ts @@ -134,18 +134,18 @@ export function getMatches( .substring(matchStartIndex, matchEndIndex) .trim() if (originalMatch && match.index >= 0) { - matches.push({ match: originalMatch, offset: match.index + 1 }) + matches.push({ match: originalMatch, offset: match.index }) } } // If the query is more than 1 token and can be found "as is" in the text, put this match first - if (query && query.query.text.length > 1) { - const best = text.indexOf(query.segmentsToStr()) + if (query && (query.query.text.length > 1 || query.getExactTerms().length > 0)) { + const best = text.indexOf(query.getBestStringForExcerpt()) if (best > -1 && matches.find(m => m.offset === best)) { matches = matches.filter(m => m.offset !== best) matches.unshift({ offset: best, - match: query.segmentsToStr(), + match: query.getBestStringForExcerpt(), }) } }