Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): highlight an accented query token the way the search matched it

Elian Doran2732011bf0dd
diff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.ts
index 42a0e389a4..df26df0711 100644
--- a/packages/trilium-core/src/services/search/services/search.ts
+++ b/packages/trilium-core/src/services/search/services/search.ts
@@ -27,9 +27,8 @@ export interface SearchNoteResult {
searchResultNoteIds: string[];
highlightedTokens: string[];
/**
- * Structured highlight tokens (additive). Empty for script-based searches, which
- * have no lexed tokens. {@link highlightedTokens} is left untouched — it is part
- * of the public scripting API.
+ * Structured highlight tokens (additive). Empty for script-based searches, which have no
+ * lexed tokens. {@link highlightedTokens} is left untouched, being part of the scripting API.
*/
highlightedTokenInfos: HighlightedTokenInfo[];
error: string | null;
@@ -316,8 +315,8 @@ function performSearch(expression: Expression, searchContext: SearchContext, ena
const originalFuzzyMatching = searchContext.enableFuzzyMatching;
searchContext.enableFuzzyMatching = enableFuzzyMatching;
- // Each progressive phase re-executes the expression tree, so clear content-match
- // records first — otherwise phase-2 (fuzzy) records would mix with phase-1 ones.
+ // Each progressive phase re-executes the expression tree, so clear the content-match records
+ // first to keep phase-2 (fuzzy) records from mixing with phase-1 ones.
searchContext.contentMatches.clear();
const noteSet = expression.execute(allNoteSet, executionContext, searchContext);
@@ -894,7 +893,9 @@ function buildHighlightRegex(info: HighlightedTokenInfo): RegExp | null {
}
}
- return new RegExp(escapeRegExp(info.token), "gi");
+ // The field is normalized before matching, so the token must be too, or an accented query
+ // highlights nothing in a note the diacritic-insensitive search just matched.
+ return new RegExp(escapeRegExp(normalizePreservingLength(info.token)), "gi");
}
/**
diff --git a/packages/trilium-core/src/services/search/services/search_highlight.spec.ts b/packages/trilium-core/src/services/search/services/search_highlight.spec.ts
index a577933cbe..e90e73e8e9 100644
--- a/packages/trilium-core/src/services/search/services/search_highlight.spec.ts
+++ b/packages/trilium-core/src/services/search/services/search_highlight.spec.ts
@@ -11,7 +11,7 @@ import searchService from "./search.js";
let rootNote: NoteBuilder;
-// "cafe" + combining acute accent (U+0301) — an NFD-decomposed "café".
+// "cafe" + combining acute accent (U+0301), an NFD-decomposed "café".
const DECOMPOSED_CAFE = "café";
function makeResult(title: string, snippet: string): SearchResult {
@@ -72,6 +72,18 @@ describe("highlightSearchResults", () => {
expect(result.highlightedContentSnippet).toBe(`${DECOMPOSED_CAFE} <b>match</b>`);
});
+ it("highlights in both diacritic directions, matching what the search itself matched", () => {
+ // Matching runs on a normalized field, so the token has to be normalized the same way.
+ // An accented query finds the note; without this the match it found went unmarked.
+ const accentedToken = makeResult("Note", "slovo ktory znamena");
+ searchService.highlightSearchResults([accentedToken], ["ktorý"]);
+ expect(accentedToken.highlightedContentSnippet).toBe("slovo <b>ktory</b> znamena");
+
+ const accentedContent = makeResult("Note", "slovo ktorý znamena");
+ searchService.highlightSearchResults([accentedContent], ["ktory"]);
+ expect(accentedContent.highlightedContentSnippet).toBe("slovo <b>ktorý</b> znamena");
+ });
+
it("highlights matches of a regex token via HighlightedTokenInfo", () => {
const result = makeResult("Note", "coool result");
searchService.highlightSearchResults([result], [{ token: "co+l", type: "regex" }]);