Skip to content

Build your personal knowledge base with Trilium Notes

feat(search): treat any whitespace as a token separator

Elian Doran13494b9ee878
diff --git a/packages/trilium-core/src/services/search/services/lex.spec.ts b/packages/trilium-core/src/services/search/services/lex.spec.ts
index 92f70fe285..a033299b9e 100644
--- a/packages/trilium-core/src/services/search/services/lex.spec.ts
+++ b/packages/trilium-core/src/services/search/services/lex.spec.ts
@@ -215,6 +215,43 @@ describe("Lexer expression", () => {
});
});
+describe("Lexer whitespace", () => {
+ it("a query can be laid out over several lines", () => {
+ const result = lex(`#book
+ and #author = 'tolkien'
+ orderby note.title`);
+
+ expect(result.expressionTokens.map((t) => t.token)).toEqual(["#book", "and", "#author", "=", "tolkien", "orderby", "note", ".", "title"]);
+
+ // A pasted query carries CRLF.
+ expect(lex("#book\r\nand #author").expressionTokens.map((t) => t.token)).toEqual(["#book", "and", "#author"]);
+ });
+
+ it("tabs separate tokens the way spaces do", () => {
+ expect(lex("#book\tand\t#author").expressionTokens.map((t) => t.token)).toEqual(["#book", "and", "#author"]);
+ });
+
+ it("the fulltext part spans lines too", () => {
+ const result = lex("lord of\nthe rings #book");
+
+ expect(result.fulltextTokens.map((t) => t.token)).toEqual(["lord", "of", "the", "rings"]);
+ expect(result.expressionTokens.map((t) => t.token)).toEqual(["#book"]);
+ // Scoring compares the whole query against titles, so the layout must not reach it.
+ expect(result.fulltextQuery).toBe("lord of the rings");
+ });
+
+ it("whitespace inside quotes is kept verbatim", () => {
+ expect(lex("#note = 'first\nsecond'").expressionTokens.map((t) => t.token)).toEqual(["#note", "=", "first\nsecond"]);
+ });
+
+ it("leading = followed by whitespace is not the exact-match operator", () => {
+ const result = lex("=\nexample");
+
+ expect(result.leadingOperator).toBe("");
+ expect(result.fulltextTokens.map((t) => t.token)).toEqual(["=", "example"]);
+ });
+});
+
describe("Lexer invalid queries and edge cases", () => {
it("concatenated attributes", () => {
expect(lex("#label~relation").expressionTokens.map((t) => t.token)).toEqual(["#label", "~relation"]);
diff --git a/packages/trilium-core/src/services/search/services/lex.ts b/packages/trilium-core/src/services/search/services/lex.ts
index f753caf151..9be267dc79 100644
--- a/packages/trilium-core/src/services/search/services/lex.ts
+++ b/packages/trilium-core/src/services/search/services/lex.ts
@@ -1,5 +1,8 @@
import type { TokenData } from "./types.js";
+/** Separates tokens outside quotes, so a query can be laid out over several lines. */
+const WHITESPACE = /\s/;
+
function lex(str: string) {
str = str.toLowerCase();
@@ -18,7 +21,7 @@ function lex(str: string) {
// Check if the string starts with an exact match operator
// This allows users to use "=searchterm" for exact matching
- if (str.startsWith("=") && str.length > 1 && str[1] !== "=" && str[1] !== " ") {
+ if (str.startsWith("=") && str.length > 1 && str[1] !== "=" && !WHITESPACE.test(str[1])) {
leadingOperator = "=";
str = str.substring(1); // Remove the leading operator from the string
}
@@ -110,7 +113,7 @@ function lex(str: string) {
// ~= and ~* are fuzzy-match operators, not a relation prefix followed by an operator
currentWord += chr;
continue;
- } else if (chr === " ") {
+ } else if (WHITESPACE.test(chr)) {
finishWord(i - 1);
continue;
} else if (fulltextEnded && ["(", ")", "."].includes(chr)) {
@@ -137,7 +140,9 @@ function lex(str: string) {
finishWord(str.length - 1);
- fulltextQuery = fulltextQuery.trim();
+ // Scoring compares the whole fulltext query against note titles, so the layout the query
+ // was typed in must not reach it.
+ fulltextQuery = fulltextQuery.replace(/\s+/g, " ").trim();
return {
fulltextQuery,