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(content_renderer): render search results
Elian Doran60eaa1ec1636
diff --git a/apps/client/src/services/content_renderer.spec.ts b/apps/client/src/services/content_renderer.spec.tsindex 0b4bd40b96..e10bf49a5a 100644--- a/apps/client/src/services/content_renderer.spec.ts+++ b/apps/client/src/services/content_renderer.spec.ts@@ -71,6 +71,9 @@ vi.mock("../widgets/type_widgets/file/PdfViewer", () => ({ default: pdfViewerComconst webViewComponent = vi.fn(() => null);vi.mock("../widgets/type_widgets/WebView", () => ({ default: webViewComponent }));+const searchNoteListComponent = vi.fn(() => null);+vi.mock("../widgets/collections/NoteList", () => ({ SearchNoteList: searchNoteListComponent }));+// `addHook` is a no-op here: sanitize_content.ts registers a DOMPurify hook at// module load (pulled in transitively), which would otherwise throw against this mock.vi.mock("dompurify", () => ({ default: { sanitize: (s: string) => s, addHook: () => {} } }));@@ -453,6 +456,37 @@ describe("generic FNote fallback / webView", () => {expect($renderedContent.hasClass("no-preview")).toBe(false);});+ it("executes the search and mounts the live results list for an interactive search note", async () => {+ const note = buildNote({ title: "Saved", type: "search" });+ vi.spyOn(note, "getBestNotePathString").mockReturnValue("root/saved");+ const loadSpy = vi.spyOn(froca, "loadSearchNote").mockResolvedValue(undefined);++ const { type, $renderedContent } = await getRenderedContent(note, { interactive: true });++ expect(type).toBe("search");+ expect(loadSpy).toHaveBeenCalledWith(note.noteId);+ expect(searchNoteListComponent).toHaveBeenCalledOnce();+ expect(searchNoteListComponent.mock.calls[0]?.[0]).toMatchObject({ note, media: "screen" });+ expect($renderedContent.find(".rendered-search").length).toBe(1);+ expect($renderedContent.hasClass("no-preview")).toBe(false);++ loadSpy.mockRestore();+ });++ it("keeps the static fallback for a search note when interactive is off", async () => {+ const note = buildNote({ title: "Saved2", type: "search" });+ const loadSpy = vi.spyOn(froca, "loadSearchNote").mockResolvedValue(undefined);++ const { $renderedContent } = await getRenderedContent(note);++ expect(loadSpy).not.toHaveBeenCalled();+ expect(searchNoteListComponent).not.toHaveBeenCalled();+ expect($renderedContent.find(".rendered-search").length).toBe(0);+ expect($renderedContent.hasClass("no-preview")).toBe(true);++ loadSpy.mockRestore();+ });+it("keeps the static fallback in a tooltip or without a src, even when interactive", async () => {// Tooltips never embed the live widget, even with interactive set.const tip = await getRenderedContent(diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.tsindex 85441da1c6..02b077e426 100644--- a/apps/client/src/services/content_renderer.ts+++ b/apps/client/src/services/content_renderer.ts@@ -87,6 +87,8 @@ export async function getRenderedContent(this: {} | { ctx: string }, entity: FNo$renderedContent.append($("<div>").append("<div>This note is protected and to access it you need to enter password.</div>").append("<br/>").append($button));} else if (type === "webView" && options.interactive && !options.tooltip && entity instanceof FNote && entity.hasLabel("webViewSrc")) {await renderWebView(entity, $renderedContent);+ } else if (type === "search" && options.interactive && !options.tooltip && entity instanceof FNote) {+ await renderSearch(entity, $renderedContent);} else if (entity instanceof FNote) {$renderedContent.addClass("no-preview");$renderedContent.append(@@ -358,6 +360,36 @@ async function renderWebView(note: FNote, $renderedContent: JQuery<HTMLElement>)$renderedContent.append($container);}+/**+ * Executes a saved search and mounts the live {@link SearchNoteList} — the same results widget used+ * in the note detail — into the rendered content. Used by interactive contexts (e.g. the dashboard+ * and included notes) that opt in via {@link RenderOptions.interactive}; every other context keeps+ * the static no-preview fallback. Loaded lazily so the collection views (and their dependencies) are+ * only pulled in when a saved search is actually embedded.+ */+async function renderSearch(note: FNote, $renderedContent: JQuery<HTMLElement>) {+ const [ { SearchNoteList }, { default: froca } ] = await Promise.all([+ import("../widgets/collections/NoteList"),+ import("./froca.js")+ ]);++ // A saved search must run server-side before its (virtual) result children exist.+ await froca.loadSearchNote(note.noteId);++ const $container = $('<div class="rendered-search">');+ const container = $container.get(0);+ if (container) {+ render(h(SearchNoteList, {+ note,+ notePath: note.getBestNotePathString(),+ ntxId: undefined,+ media: "screen",+ highlightedTokens: note.highlightedTokens+ }), container);+ }+ $renderedContent.append($container);+}+function getRenderingType(entity: FNote | FAttachment) {let type: string = "";if ("type" in entity) {