Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/content manager: include the title of parent notes in the filtering
Adorian Doran893aed8f7c5b
diff --git a/apps/client/src/services/content_manager.spec.ts b/apps/client/src/services/content_manager.spec.tsindex 9eb9b56d0e..36aef479d5 100644--- a/apps/client/src/services/content_manager.spec.ts+++ b/apps/client/src/services/content_manager.spec.ts@@ -46,10 +46,20 @@ describe("buildCategoryQuery", () => {// `AndExp` narrows its operands left to right, so the indexed attribute group has to lead;// the bare "#" is what ends the fulltext portion so the following "(" is not swallowed.expect(buildCategoryQuery("#appCss OR #disabled:appCss", "title", "dark")).toBe(- `# (#appCss OR #disabled:appCss) AND note.title *=* "dark" orderBy note.title, note.noteId`+ `# (#appCss OR #disabled:appCss)`+ + ` AND (note.title *=* "dark" OR note.parents.title *=* "dark")`+ + ` orderBy note.title, note.noteId`);});+ it("matches the parent's title too, so an item can be found by where it lives", () => {+ const query = buildCategoryQuery("#appCss", "title", "themes");++ expect(query).toContain(`note.parents.title *=* "themes"`);+ // Both title conditions are grouped, so the OR cannot swallow the attribute filter.+ expect(query).toContain(`AND (note.title *=* "themes" OR note.parents.title *=* "themes")`);+ });+it("leaves the query untouched when the filter is empty or only whitespace", () => {const plain = "#appCss orderBy note.title, note.noteId";@@ -61,6 +71,8 @@ describe("buildCategoryQuery", () => {expect(buildCategoryQuery("#appCss", "title", "my theme")).toContain(`note.title *=* "my theme"`);expect(buildCategoryQuery("#appCss", "title", `a" OR #run`)).toContain(`note.title *=* "a\\" OR #run"`);expect(buildCategoryQuery("#appCss", "title", "back\\slash")).toContain(`note.title *=* "back\\\\slash"`);+ // The parent condition is built from the same escaped value, not a raw second copy.+ expect(buildCategoryQuery("#appCss", "title", `a" OR #run`)).toContain(`note.parents.title *=* "a\\" OR #run"`);});it("keeps the ordering clause at the top level of an OR chain", () => {diff --git a/apps/client/src/services/content_manager.ts b/apps/client/src/services/content_manager.tsindex 4ea0e5b7e8..d810db83e4 100644--- a/apps/client/src/services/content_manager.ts+++ b/apps/client/src/services/content_manager.ts@@ -238,13 +238,23 @@ export function buildCategoryQuery(filter: string, sortOrder: ContentSortOrder,// The attribute group comes first on purpose: `AndExp` threads the narrowing note set through// its operands in order, so the index-backed attribute lookup shrinks the set before the title- // comparison has to walk it. The leading bare "#" is what ends the fulltext portion — without+ // comparisons have to walk it. The leading bare "#" is what ends the fulltext portion — without// it the opening paren is swallowed into that portion and the expression parses unbalanced.- const expression = wanted ? `# (${filter}) AND note.title *=* ${quoteSearchValue(wanted)}` : filter;+ const expression = wanted ? `# (${filter}) AND (${buildTitleFilter(wanted)})` : filter;return `${expression} orderBy ${orderBy}`;}+/**+ * Matches the note's own title or its parent's, so an item can be found by where it lives as well as+ * by what it is called — the location is shown on every row, so it reads as searchable.+ */+function buildTitleFilter(wanted: string) {+ const value = quoteSearchValue(wanted);++ return `note.title *=* ${value} OR note.parents.title *=* ${value}`;+}+/** Quotes a user-typed value so spaces keep it one token and quotes in it can't end it early. */function quoteSearchValue(value: string) {return `"${value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;diff --git a/packages/trilium-core/src/services/search/services/search.spec.ts b/packages/trilium-core/src/services/search/services/search.spec.tsindex bb34c5786e..890bf76e29 100644--- a/packages/trilium-core/src/services/search/services/search.spec.ts+++ b/packages/trilium-core/src/services/search/services/search.spec.ts@@ -713,6 +713,13 @@ describe("Search", () => {`# (#run OR #disabled:run) AND note.title *=* "gamma other" orderBy note.title`, searchContext);expect(searchResults.map((sr) => becca.notes[sr.noteId].title)).toEqual([ "Gamma other" ]);+ // The filter also matches the parent's title, so a second group is AND-ed on as a whole.+ searchResults = searchService.findResultsWithQuery(+ `# (#run OR #disabled:run) AND (note.title *=* nothing OR note.parents.title *=* scripts) orderBy note.title`,+ searchContext);+ expect(searchResults.map((sr) => becca.notes[sr.noteId].title))+ .toEqual([ "Alpha script", "Beta script", "Gamma other" ]);+expect(searchContext.getError()).toBeFalsy();});