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
feat(search): match the content of virtual notes
Elian Dorandcae2450f037
diff --git a/apps/server/spec/in_app_help.spec.ts b/apps/server/spec/in_app_help.spec.tsindex ce8e5de5d7..b0ae5741ba 100644--- a/apps/server/spec/in_app_help.spec.ts+++ b/apps/server/spec/in_app_help.spec.ts@@ -4,6 +4,7 @@ import { load } from "@triliumnext/core/src/becca/becca_loader.js";import blobService from "@triliumnext/core/src/services/blob.js";import { getContext } from "@triliumnext/core/src/services/context.js";import { HELP_ASSET_TOKEN, initInAppHelp } from "@triliumnext/core/src/services/in_app_help.js";+import searchService from "@triliumnext/core/src/services/search/services/search.js";import fs from "fs";import path from "path";import { beforeAll, describe, expect, it } from "vitest";@@ -70,6 +71,19 @@ describe("in-app help (shipped artifacts)", () => {expect(missing).toEqual([]);});+ it("is searchable by its content, but only when the search asks for hidden notes", () => {+ // A phrase from the body of a page, not from any title.+ const query = "docker compose";++ const scoped = searchService.searchNotes(query, { includeHiddenNotes: true });+ expect(scoped.map((note) => note.noteId)).toContain("_help_rWX5eY045zbE");++ // The guide lives under _hidden, so an ordinary search still does not surface it — the+ // virtual pass makes the content matchable, not suddenly present in every result list.+ const ordinary = searchService.searchNotes(query, {});+ expect(ordinary.filter((note) => note.noteId.startsWith("_help"))).toEqual([]);+ });+it("makes every page read-only", () => {for (const item of flatten(meta)) {expect(becca.getNoteOrThrow(item.id).isLabelTruthy("readOnly")).toBe(true);diff --git a/packages/trilium-core/src/services/search/expressions/note_content_fulltext.ts b/packages/trilium-core/src/services/search/expressions/note_content_fulltext.tsindex 996c731b2f..54998353ef 100644--- a/packages/trilium-core/src/services/search/expressions/note_content_fulltext.ts+++ b/packages/trilium-core/src/services/search/expressions/note_content_fulltext.ts@@ -1,4 +1,4 @@-import type { NoteRow } from "@triliumnext/commons";+import type { NoteRow, NoteType } from "@triliumnext/commons";import becca from "../../../becca/becca.js";import { getLog } from "../../log.js";@@ -20,6 +20,9 @@ const ALLOWED_OPERATORS = new Set(["=", "!=", "*=*", "*=", "=*", "%=", "~=", "~*// Maximum content size for search processing (2MB)const MAX_SEARCH_CONTENT_SIZE = 2 * 1024 * 1024;+/** Note types whose content is text worth searching. Shared by the persisted and virtual passes. */+const SEARCHABLE_TYPES: NoteType[] = [ "text", "code", "mermaid", "canvas", "mindMap", "spreadsheet", "llmChat" ];+const cachedRegexes: Record<string, RegExp> = {};function getRegex(str: string): RegExp {@@ -83,12 +86,14 @@ class NoteContentFulltextExp extends Expression {for (const row of getSql().iterateRows<SearchRow>(`SELECT noteId, type, mime, content, isProtectedFROM notes JOIN blobs USING (blobId)- WHERE type IN ('text', 'code', 'mermaid', 'canvas', 'mindMap', 'spreadsheet', 'llmChat')+ WHERE type IN (${SEARCHABLE_TYPES.map((type) => `'${type}'`).join(", ")})AND isDeleted = 0AND LENGTH(content) < ${MAX_SEARCH_CONTENT_SIZE}`)) {this.findInText(row, inputNoteSet, resultNoteSet);}+ this.findInVirtualNotes(inputNoteSet, resultNoteSet);+// For exact match with flatText, also search notes WITHOUT content (they may have matching attributes)if (this.flatText && (this.operator === "=" || this.operator === "!=")) {for (const note of inputNoteSet.notes) {@@ -191,6 +196,34 @@ class NoteContentFulltextExp extends Expression {return false;}+ /**+ * Virtual notes (the in-app help, and anything else a virtual note provider injects) hold+ * their content in becca rather than in `blobs`, so the query above cannot see them. They are+ * matched from the cache instead, under the same type and size rules.+ *+ * Whether they can be *returned* is decided elsewhere: they live under `_hidden`, so a search+ * only reaches them when it opts into hidden notes or scopes itself to the subtree — which is+ * why this pass does not change what an ordinary search finds.+ */+ findInVirtualNotes(inputNoteSet: NoteSet, resultNoteSet: NoteSet) {+ for (const noteId in becca.notes) {+ const note = becca.notes[noteId];++ if (!note.isVirtual || !inputNoteSet.hasNoteId(noteId) || !SEARCHABLE_TYPES.includes(note.type)) {+ continue;+ }++ const content = note.getContent();++ if (typeof content !== "string" || content.length >= MAX_SEARCH_CONTENT_SIZE) {+ continue;+ }++ // Virtual notes are never protected — nothing about them is user data.+ this.findInText({ noteId, type: note.type, mime: note.mime, content, isProtected: false }, inputNoteSet, resultNoteSet);+ }+ }+findInText({ noteId, isProtected, content, type, mime }: SearchRow, inputNoteSet: NoteSet, resultNoteSet: NoteSet) {if (!inputNoteSet.hasNoteId(noteId) || !(noteId in becca.notes)) {return;