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(quick_search): also try to show the "matched" text in quick search results and not just note titles
perf3ct51a8937c64f0
diff --git a/apps/client/src/widgets/quick_search.ts b/apps/client/src/widgets/quick_search.tsindex 2d06baafee..3c23eaea57 100644--- a/apps/client/src/widgets/quick_search.ts+++ b/apps/client/src/widgets/quick_search.ts@@ -45,6 +45,13 @@ const MAX_DISPLAYED_NOTES = 15;// TODO: Deduplicate with server.interface QuickSearchResponse {searchResultNoteIds: string[];+ searchResults?: Array<{+ notePath: string;+ noteTitle: string;+ notePathTitle: string;+ highlightedNotePathTitle: string;+ icon: string;+ }>;error: string;}@@ -115,7 +122,7 @@ export default class QuickSearchWidget extends BasicWidget {this.$dropdownMenu.empty();this.$dropdownMenu.append(`<span class="dropdown-item disabled"><span class="bx bx-loader bx-spin"></span>${t("quick-search.searching")}</span>`);- const { searchResultNoteIds, error } = await server.get<QuickSearchResponse>(`quick-search/${encodeURIComponent(searchString)}`);+ const { searchResultNoteIds, searchResults, error } = await server.get<QuickSearchResponse>(`quick-search/${encodeURIComponent(searchString)}`);if (error) {let tooltip = new Tooltip(this.$searchString[0], {@@ -129,44 +136,88 @@ export default class QuickSearchWidget extends BasicWidget {setTimeout(() => tooltip.dispose(), 4000);}- const displayedNoteIds = searchResultNoteIds.slice(0, Math.min(MAX_DISPLAYED_NOTES, searchResultNoteIds.length));-this.$dropdownMenu.empty();- if (displayedNoteIds.length === 0) {- this.$dropdownMenu.append(`<span class="dropdown-item disabled">${t("quick-search.no-results")}</span>`);- }+ // Use highlighted search results if available, otherwise fall back to basic display+ if (searchResults && searchResults.length > 0) {+ const displayedResults = searchResults.slice(0, Math.min(MAX_DISPLAYED_NOTES, searchResults.length));++ if (displayedResults.length === 0) {+ this.$dropdownMenu.append(`<span class="dropdown-item disabled">${t("quick-search.no-results")}</span>`);+ }- for (const note of await froca.getNotes(displayedNoteIds)) {- const $link = await linkService.createLink(note.noteId, { showNotePath: true, showNoteIcon: true });- $link.addClass("dropdown-item");- $link.attr("tabIndex", "0");- $link.on("click", (e) => {- this.dropdown.hide();+ for (const result of displayedResults) {+ const noteId = result.notePath.split("/").pop();+ if (!noteId) continue;- if (!e.target || e.target.nodeName !== "A") {- // click on the link is handled by link handling, but we want the whole item clickable+ const $item = $('<a class="dropdown-item" tabindex="0" href="javascript:">');+ $item.html(`<span class="${result.icon}"></span> ${result.highlightedNotePathTitle}`);++ $item.on("click", (e) => {+ this.dropdown.hide();+ e.preventDefault();+const activeContext = appContext.tabManager.getActiveContext();if (activeContext) {- activeContext.setNote(note.noteId);+ activeContext.setNote(noteId);}- }- });- shortcutService.bindElShortcut($link, "return", () => {- this.dropdown.hide();+ });++ shortcutService.bindElShortcut($item, "return", () => {+ this.dropdown.hide();- const activeContext = appContext.tabManager.getActiveContext();- if (activeContext) {- activeContext.setNote(note.noteId);- }- });+ const activeContext = appContext.tabManager.getActiveContext();+ if (activeContext) {+ activeContext.setNote(noteId);+ }+ });- this.$dropdownMenu.append($link);- }+ this.$dropdownMenu.append($item);+ }- if (searchResultNoteIds.length > MAX_DISPLAYED_NOTES) {- const numRemainingResults = searchResultNoteIds.length - MAX_DISPLAYED_NOTES;- this.$dropdownMenu.append(`<span class="dropdown-item disabled">${t("quick-search.more-results", { number: numRemainingResults })}</span>`);+ if (searchResults.length > MAX_DISPLAYED_NOTES) {+ const numRemainingResults = searchResults.length - MAX_DISPLAYED_NOTES;+ this.$dropdownMenu.append(`<span class="dropdown-item disabled">${t("quick-search.more-results", { number: numRemainingResults })}</span>`);+ }+ } else {+ // Fallback to original behavior if no highlighted results+ const displayedNoteIds = searchResultNoteIds.slice(0, Math.min(MAX_DISPLAYED_NOTES, searchResultNoteIds.length));++ if (displayedNoteIds.length === 0) {+ this.$dropdownMenu.append(`<span class="dropdown-item disabled">${t("quick-search.no-results")}</span>`);+ }++ for (const note of await froca.getNotes(displayedNoteIds)) {+ const $link = await linkService.createLink(note.noteId, { showNotePath: true, showNoteIcon: true });+ $link.addClass("dropdown-item");+ $link.attr("tabIndex", "0");+ $link.on("click", (e) => {+ this.dropdown.hide();++ if (!e.target || e.target.nodeName !== "A") {+ // click on the link is handled by link handling, but we want the whole item clickable+ const activeContext = appContext.tabManager.getActiveContext();+ if (activeContext) {+ activeContext.setNote(note.noteId);+ }+ }+ });+ shortcutService.bindElShortcut($link, "return", () => {+ this.dropdown.hide();++ const activeContext = appContext.tabManager.getActiveContext();+ if (activeContext) {+ activeContext.setNote(note.noteId);+ }+ });++ this.$dropdownMenu.append($link);+ }++ if (searchResultNoteIds.length > MAX_DISPLAYED_NOTES) {+ const numRemainingResults = searchResultNoteIds.length - MAX_DISPLAYED_NOTES;+ this.$dropdownMenu.append(`<span class="dropdown-item disabled">${t("quick-search.more-results", { number: numRemainingResults })}</span>`);+ }}const $showInFullButton = $('<a class="dropdown-item" tabindex="0">').text(t("quick-search.show-in-full-search"));diff --git a/apps/server/src/routes/api/search.ts b/apps/server/src/routes/api/search.tsindex 0e9304a07d..29d75c6dca 100644--- a/apps/server/src/routes/api/search.ts+++ b/apps/server/src/routes/api/search.ts@@ -52,10 +52,15 @@ function quickSearch(req: Request) {fuzzyAttributeSearch: false});- const resultNoteIds = searchService.findResultsWithQuery(searchString, searchContext).map((sr) => sr.noteId);+ // Use the same highlighting logic as autocomplete for consistency+ const searchResults = searchService.searchNotesForAutocomplete(searchString, false);++ // Extract note IDs for backward compatibility+ const resultNoteIds = searchResults.map((result) => result.notePath.split("/").pop()).filter(Boolean) as string[];return {searchResultNoteIds: resultNoteIds,+ searchResults: searchResults,error: searchContext.getError()};}