Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): exclude regex tokens from quick-search highlightedTokens

perfectra1nb922d02d26f5
diff --git a/packages/trilium-core/src/routes/api/search.spec.ts b/packages/trilium-core/src/routes/api/search.spec.ts
index 884e659d1a..b525816f2d 100644
--- a/packages/trilium-core/src/routes/api/search.spec.ts
+++ b/packages/trilium-core/src/routes/api/search.spec.ts
@@ -41,6 +41,19 @@ describe("Search API (core)", () => {
expect(res.body.highlightedTokens).toContain(UNIQUE_TOKEN.toLowerCase());
});
+ it("excludes regex (`%=`) tokens from quick-search highlightedTokens", async () => {
+ // The client seeds these into the find bar as literal jump-to-match terms; a raw regex
+ // pattern would match nothing there (silent no-op), so the server must drop regex tokens.
+ const pattern = "ZzRegexQwerty";
+ const res = await api.get<{ highlightedTokens: string[]; error: string | null }>(
+ `/api/quick-search/${encodeURIComponent(`note.content %= '${pattern}'`)}`
+ );
+
+ expect(res.status).toBe(200);
+ expect(res.body.error).toBeFalsy();
+ expect(res.body.highlightedTokens).not.toContain(pattern);
+ });
+
it("lists template note ids including a freshly-labelled template", async () => {
const { noteId } = await createTextNote(api, { title: "A template note" });
await api.post(`/api/notes/${noteId}/attributes`, {
diff --git a/packages/trilium-core/src/routes/api/search.ts b/packages/trilium-core/src/routes/api/search.ts
index cf5be61a36..4d85eeca32 100644
--- a/packages/trilium-core/src/routes/api/search.ts
+++ b/packages/trilium-core/src/routes/api/search.ts
@@ -119,7 +119,11 @@ function quickSearch(req: Request<{ searchString: string }>) {
return {
searchResultNoteIds: searchResults.map((result) => result.noteId),
searchResults,
- highlightedTokens: searchContext.highlightedTokens,
+ // Only plain tokens round-trip as literal jump-to-match search terms. Regex (`%=`) tokens
+ // are raw patterns the find bar would treat literally (a silent no-op), so drop them here.
+ // The client uses this field solely to seed jump-to-match; snippet highlighting already
+ // happened server-side on `searchResults`, so filtering the response is safe.
+ highlightedTokens: searchContext.highlightedTokens.filter((token) => !searchContext.regexTokens.has(token)),
error: searchContext.getError()
};
}