Skip to content

Build your personal knowledge base with Trilium Notes

feat(client): carry search terms from quick-search results

perfectra1n8edf0c24ba62
diff --git a/apps/client/src/widgets/quick_search.ts b/apps/client/src/widgets/quick_search.ts
index e533a87e3e..2793ade28a 100644
--- a/apps/client/src/widgets/quick_search.ts
+++ b/apps/client/src/widgets/quick_search.ts
@@ -3,7 +3,7 @@ import { Dropdown, Tooltip } from "bootstrap";
import appContext from "../components/app_context.js";
import froca from "../services/froca.js";
import { t } from "../services/i18n.js";
-import linkService from "../services/link.js";
+import linkService, { calculateHash, type ViewScope } from "../services/link.js";
import server from "../services/server.js";
import shortcutService, { isIMEComposing } from "../services/shortcuts.js";
import utils, { handleRightToLeftPlacement } from "../services/utils.js";
@@ -304,7 +304,12 @@ export default class QuickSearchWidget extends BasicWidget {
for (const result of resultsToDisplay) {
if (!result.notePath) continue;
- const $item = $(`<a class="dropdown-item" tabindex="0" href="#${result.notePath}">`);
+ // Carry the highlighted search tokens into the opened note (jump-to-match) via the
+ // view scope. Set the href with .attr() rather than interpolating it into the HTML
+ // string so query separators aren't parsed as HTML entities. calculateHash omits
+ // searchTerms when the token list is empty, so this degrades to a plain `#notePath`.
+ const $item = $(`<a class="dropdown-item" tabindex="0">`);
+ $item.attr("href", calculateHash({ notePath: result.notePath, viewScope: this.resultViewScope() }));
// Build the display HTML with content snippet below the title
let itemHtml = `<div class="quick-search-item">
@@ -349,7 +354,7 @@ export default class QuickSearchWidget extends BasicWidget {
const noteIdsToDisplay = this.allSearchResultNoteIds.slice(startIndex, endIndex);
for (const note of await froca.getNotes(noteIdsToDisplay)) {
- const $link = await linkService.createLink(note.noteId, { showNotePath: true, showNoteIcon: true });
+ const $link = await linkService.createLink(note.noteId, { showNotePath: true, showNoteIcon: true, viewScope: this.resultViewScope() });
$link.addClass("dropdown-item");
$link.attr("tabIndex", "0");
$link.on("click auxclick", (e) => {
@@ -375,6 +380,15 @@ export default class QuickSearchWidget extends BasicWidget {
this.isLoadingMore = false;
}
+ /**
+ * View scope attached to each result link so the opened note jumps to the first match and
+ * pre-fills the find bar. Undefined when the last search highlighted no tokens, so links stay
+ * plain `#notePath`.
+ */
+ private resultViewScope(): ViewScope | undefined {
+ return this.lastHighlightedTokens.length ? { searchTerms: this.lastHighlightedTokens } : undefined;
+ }
+
private handleScroll() {
if (this.isLoadingMore) return;