Skip to content

Build your personal knowledge base with Trilium Notes

feat(search): tell a fuzzy highlight apart from an exact one

Elian Doran4616b0a0798c
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.css
index 88d690c4dc..632ce60463 100644
--- a/apps/client/src/stylesheets/style.css
+++ b/apps/client/src/stylesheets/style.css
@@ -1089,6 +1089,12 @@ table.promoted-attributes-in-tooltip th {
text-decoration: underline;
}
+.aa-dropdown-menu .aa-suggestion .search-result-content b.search-fuzzy-match {
+ color: var(--note-list-view-search-result-fuzzy-highlight-color);
+ font-weight: 400;
+ text-decoration: underline dotted;
+}
+
.aa-dropdown-menu .aa-suggestion p {
padding: 0;
margin: 0;
diff --git a/apps/client/src/stylesheets/theme-next-dark.css b/apps/client/src/stylesheets/theme-next-dark.css
index b874facb27..96cdf8e3df 100644
--- a/apps/client/src/stylesheets/theme-next-dark.css
+++ b/apps/client/src/stylesheets/theme-next-dark.css
@@ -140,6 +140,7 @@
--quick-search-focus-color: white;
--quick-search-result-content-background: #0000004d;
--quick-search-result-highlight-color: #a4d995;
+ --quick-search-result-fuzzy-highlight-color: #efb075;
--left-pane-collapsed-border-color: #0009;
--left-pane-background-color: #1f1f1f;
@@ -191,7 +192,7 @@
--right-pane-heading-color: gray;
--right-pane-peek-background: #303030dc;
-
+
--peek-pane-backdrop-color: #00000032;
--peek-pane-shadow-color: #000000a8;
@@ -343,6 +344,7 @@
--note-list-view-large-icon-color: var(--note-icon-color);
--note-list-view-search-result-highlight-background: transparent;
--note-list-view-search-result-highlight-color: var(--quick-search-result-highlight-color);
+ --note-list-view-search-result-fuzzy-highlight-color: var(--quick-search-result-fuzzy-highlight-color);
--note-list-view-content-background: rgba(0, 0, 0, .2);
--note-list-view-content-search-result-highlight-background: var(--quick-search-result-highlight-color);
--note-list-view-content-search-result-highlight-color: black;
diff --git a/apps/client/src/stylesheets/theme-next-light.css b/apps/client/src/stylesheets/theme-next-light.css
index 0d72f364e2..4f1548af44 100644
--- a/apps/client/src/stylesheets/theme-next-light.css
+++ b/apps/client/src/stylesheets/theme-next-light.css
@@ -133,6 +133,7 @@
--quick-search-focus-color: #000;
--quick-search-result-content-background: #0000000f;
--quick-search-result-highlight-color: #c65050;
+ --quick-search-result-fuzzy-highlight-color: #e47b19;
--left-pane-collapsed-border-color: #0000000d;
--left-pane-background-color: #f2f2f2;
@@ -183,7 +184,7 @@
--right-pane-heading-color: gray;
--right-pane-peek-background: #ffffffea;
-
+
--peek-pane-backdrop-color: #0000000a;
--peek-pane-shadow-color: #0000003a;
@@ -342,6 +343,7 @@
--note-list-view-large-icon-color: var(--note-icon-color);
--note-list-view-search-result-highlight-background: transparent;
--note-list-view-search-result-highlight-color: var(--quick-search-result-highlight-color);
+ --note-list-view-search-result-fuzzy-highlight-color: var(--quick-search-result-fuzzy-highlight-color);
--note-list-view-content-background: #b1b1b133;
--note-list-view-content-search-result-highlight-background: var(--quick-search-result-highlight-color);
--note-list-view-content-search-result-highlight-color: white;
@@ -508,4 +510,4 @@ body.backdrop-effects-disabled :is(.calendar-event-popover, .geo-detail-pane).wi
:where(.ck-content .todo-list li[data-trilium-task-state]), .tn-task-checkbox {
--task-state-custom-hover-background-color: hsl(var(--task-state-hue), 60%, 40%);
-}
\ No newline at end of file
+}
diff --git a/apps/client/src/widgets/collections/search/SearchResultsList.css b/apps/client/src/widgets/collections/search/SearchResultsList.css
index 0e9ed8dae5..2444b3285b 100644
--- a/apps/client/src/widgets/collections/search/SearchResultsList.css
+++ b/apps/client/src/widgets/collections/search/SearchResultsList.css
@@ -131,6 +131,16 @@
font-weight: 600;
text-decoration: underline;
}
+
+ /* A word the search accepted in place of one that was typed: a different hue from the same
+ family, not bold, dotted, so it reads as an approximate match rather than something the
+ query actually contained. */
+ .search-result-card-snippet b.search-fuzzy-match,
+ .search-result-card-badges b.search-fuzzy-match {
+ color: var(--note-list-view-search-result-fuzzy-highlight-color);
+ font-weight: 400;
+ text-decoration: underline dotted;
+ }
}
@keyframes search-result-skeleton {
diff --git a/apps/client/src/widgets/quick_search.ts b/apps/client/src/widgets/quick_search.ts
index 28372abe92..b4f28cd47f 100644
--- a/apps/client/src/widgets/quick_search.ts
+++ b/apps/client/src/widgets/quick_search.ts
@@ -111,6 +111,14 @@ const TPL = /*html*/`
text-decoration: underline;
}
+ .quick-search .search-result-title b.search-fuzzy-match,
+ .quick-search .search-result-content b.search-fuzzy-match,
+ .quick-search .search-result-attributes b.search-fuzzy-match {
+ color: var(--quick-search-result-fuzzy-highlight-color, #e47b19);
+ font-weight: 400;
+ text-decoration: underline dotted;
+ }
+
.quick-search .dropdown-divider {
margin: 0;
}
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html
index b201b2eded..e7ffd6a34b 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html
@@ -13,7 +13,9 @@
look for the dedicated search button.</li>
<li>To limit the search to a note and its children, select <em>Search from subtree</em> from
the&nbsp;<a class="reference-link" href="#root/_help_YtSN43OrfzaA">Note tree contextual menu</a>&nbsp;or
- press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>S</kbd>.</li>
+ press <kbd spellcheck="false">Ctrl</kbd>+<kbd spellcheck="false">Shift</kbd>+
+ <kbd
+ spellcheck="false">S</kbd>.</li>
</ul>
<h2>Interaction</h2>
<p>To search for notes, click on the magnifying glass icon on the toolbar
@@ -118,8 +120,15 @@
</ol>
<h2>Viewing search results</h2>
<p>Results appear below the search pane as a list of <strong>snippet cards</strong>.
- Each card shows the note title and a short excerpt of the text that matched,
- with your search terms highlighted.</p>
+ Each card shows the note title and a short excerpt of the text that matched.</p>
+<p>The search will also highlight the words from the title or content that
+ matched:</p>
+<ul>
+ <li>With a solid underlined green for a direct match;</li>
+ <li>With a dotted underline orange for a partial match (matched by the fuzzy
+ search).</li>
+</ul>
+<p>In addition:&nbsp;</p>
<ul>
<li>The <strong>total number of results</strong> is always shown, so you can
immediately tell how broad a query is.</li>
diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md
index 565eefe7d3..f8031c9981 100644
--- a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md
+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md
@@ -6,7 +6,7 @@ Note search enables you to find notes by searching for text in the title, conten
## Accessing the search
* From the <a class="reference-link" href="../UI%20Elements/Launch%20Bar.md">Launch Bar</a>, look for the dedicated search button.
-* To limit the search to a note and its children, select _Search from subtree_ from the <a class="reference-link" href="../UI%20Elements/Note%20Tree/Note%20tree%20contextual%20menu.md">Note tree contextual menu</a> or press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>S</kbd>.
+* To limit the search to a note and its children, select _Search from subtree_ from the <a class="reference-link" href="../UI%20Elements/Note%20Tree/Note%20tree%20contextual%20menu.md">Note tree contextual menu</a> or press <kbd spellcheck="false">Ctrl</kbd>+<kbd spellcheck="false">Shift</kbd>+<kbd spellcheck="false">S</kbd>.
## Interaction
@@ -57,7 +57,14 @@ The options available are:
## Viewing search results
-Results appear below the search pane as a list of **snippet cards**. Each card shows the note title and a short excerpt of the text that matched, with your search terms highlighted.
+Results appear below the search pane as a list of **snippet cards**. Each card shows the note title and a short excerpt of the text that matched.
+
+The search will also highlight the words from the title or content that matched:
+
+* With a solid underlined green for a direct match;
+* With a dotted underline orange for a partial match (matched by the fuzzy search).
+
+In addition: 
* The **total number of results** is always shown, so you can immediately tell how broad a query is.
* A **page-size selector** lets you choose how many results to display per page. Your choice is remembered and synced across your devices (stored in the `searchResultsPageSize` option), so you do not have to reset it on every device.
diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.ts
index ca12e3ec47..b6a9345003 100644
--- a/packages/commons/src/lib/server_api.ts
+++ b/packages/commons/src/lib/server_api.ts
@@ -341,7 +341,12 @@ export interface SubtreeSizeResponse {
*/
export interface HighlightedTokenInfo {
token: string;
- type: "plain" | "regex";
+ /**
+ * `plain` is matched literally and `regex` (from `%=`) as a regular expression. `fuzzy` is a
+ * word the search accepted in place of one the user typed, matched literally but rendered in a
+ * muted style so an approximate hit does not read as an exact one.
+ */
+ type: "plain" | "regex" | "fuzzy";
}
/** Request body for `POST /api/search-note/:noteId/result-details` (max 100 noteIds). */
diff --git a/packages/trilium-core/src/services/search/services/search.spec.ts b/packages/trilium-core/src/services/search/services/search.spec.ts
index 395adad233..91d3e0817e 100644
--- a/packages/trilium-core/src/services/search/services/search.spec.ts
+++ b/packages/trilium-core/src/services/search/services/search.spec.ts
@@ -1021,7 +1021,7 @@ describe("Search", () => {
const detail = detailsFor("orbiter").find((d) => d.noteId === asimov.noteId);
expect(detail?.contentSnippet).toContain("writer");
- expect(detail?.highlightedContentSnippet).toContain("<b>writer</b>");
+ expect(detail?.highlightedContentSnippet).toContain(`<b class="search-fuzzy-match">writer</b>`);
});
it("keeps one note's fuzzy word out of another note's highlighting", () => {
@@ -1035,8 +1035,21 @@ describe("Search", () => {
// The exact match highlights only what was typed, even though "writer" is the word
// that pulled the other note in and appears here too.
expect(exact?.highlightedContentSnippet).toContain("<b>Orbiter</b>");
- expect(exact?.highlightedContentSnippet).not.toContain("<b>writer</b>");
- expect(fuzzy?.highlightedContentSnippet).toContain("<b>writer</b>");
+ expect(exact?.highlightedContentSnippet).not.toContain("search-fuzzy-match");
+ expect(fuzzy?.highlightedContentSnippet).toContain(`<b class="search-fuzzy-match">writer</b>`);
+ });
+
+ it("marks an exact hit and a fuzzy stand-in apart in the same snippet", () => {
+ // "rocket" is in the note and "orbiter" is not, so the note only survives phase 2 by
+ // accepting "writer" for "orbiter". The two have to render differently, or the word
+ // the user never typed reads exactly like the word they did.
+ const both = contentNote("Both", "<p>The rocket and the writer met</p>");
+
+ const detail = detailsFor("orbiter rocket").find((d) => d.noteId === both.noteId);
+
+ expect(detail?.highlightedContentSnippet).toBe(
+ `The <b>rocket</b> and the <b class="search-fuzzy-match">writer</b> met`
+ );
});
});
diff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.ts
index 81aa4422ad..026c2ada66 100644
--- a/packages/trilium-core/src/services/search/services/search.ts
+++ b/packages/trilium-core/src/services/search/services/search.ts
@@ -23,6 +23,23 @@ import { getSql } from "../../sql/index.js";
/** Cap on marker wraps per snippet field per token, bounding pathological regex patterns. */
const MAX_HIGHLIGHT_WRAPS = 50;
+/** Class on a fuzzy match's tag, so the client can mute it against the exact highlights. */
+export const FUZZY_HIGHLIGHT_CLASS = "search-fuzzy-match";
+
+/**
+ * Markers wrapped around a match before the text is escaped; `renderHighlights()` turns each
+ * pair into its tag. Braces are stripped from the source text and from the tokens beforehand,
+ * so the only ones left are the pairs inserted here and doubling one is unambiguous.
+ */
+const MARKERS = {
+ plain: { open: "{", close: "}" },
+ regex: { open: "{", close: "}" },
+ fuzzy: { open: "{{", close: "}}" }
+} as const;
+
+/** Every marker character, stripped from source text and tokens so neither can forge a tag. */
+const MARKER_CHARS = /[{}]/g;
+
export interface SearchNoteResult {
searchResultNoteIds: string[];
highlightedTokens: string[];
@@ -752,7 +769,7 @@ function buildSearchResultDetails(results: SearchResult[], searchContext: Search
// alone. Without it the card carries no visible reason for being in the results at all.
const matchedWords = searchContext.contentMatches.get(result.noteId)?.matchedWords ?? [];
const noteTokenInfos: HighlightedTokenInfo[] = matchedWords.length
- ? [ ...tokenInfos, ...matchedWords.map((token) => ({ token, type: "plain" as const })) ]
+ ? [ ...tokenInfos, ...matchedWords.map((token) => ({ token, type: "fuzzy" as const })) ]
: tokenInfos;
result.contentSnippet = extractContentSnippet(result.noteId, noteTokenInfos);
@@ -791,16 +808,16 @@ function highlightSearchResults(searchResults: SearchResult[], tokens: Highlight
// like "Issues caused by <div>" would lose its opening bracket.
// The only characters that have to go are the { } markers themselves.
for (const result of searchResults) {
- result.highlightedNotePathTitle = result.notePathTitle.replace(/[{}]/g, "");
+ result.highlightedNotePathTitle = result.notePathTitle.replace(MARKER_CHARS, "");
// Initialize highlighted content snippet, preserving newlines for later conversion to <br>
if (result.contentSnippet) {
- result.highlightedContentSnippet = result.contentSnippet.replace(/[{}]/g, "");
+ result.highlightedContentSnippet = result.contentSnippet.replace(MARKER_CHARS, "");
}
// Initialize highlighted attribute snippet
if (result.attributeSnippet) {
- result.highlightedAttributeSnippet = result.attributeSnippet.replace(/[{}]/g, "");
+ result.highlightedAttributeSnippet = result.attributeSnippet.replace(MARKER_CHARS, "");
}
}
@@ -872,9 +889,9 @@ function normalizeHighlightTokens(tokens: HighlightedTokenInfo[] | string[]): Hi
const result: HighlightedTokenInfo[] = [];
for (const info of toTokenInfos(tokens)) {
- // { } are used as <b>/</b> markers and < can break the surrounding HTML, so we
- // strip them from plain tokens; regex patterns keep them (e.g. `a{2}` quantifiers).
- const token = info.type === "regex" ? info.token : info.token.replace(/[<>{}]/g, "");
+ // Braces are the markers and < can break the surrounding HTML, so strip them from the
+ // literal tokens; regex patterns keep them (e.g. `a{2}` quantifiers).
+ const token = info.type === "regex" ? info.token : info.token.replace(/[<>]/g, "").replace(MARKER_CHARS, "");
if (!token.trim() || seen.has(token)) {
continue;
@@ -905,9 +922,9 @@ function buildHighlightRegex(info: HighlightedTokenInfo): RegExp | null {
}
/**
- * Wraps every match of one token in a field with the `{`/`}` markers that `renderHighlights()`
- * turns into `<b>`/`</b>`. Positions come from a length-preserving normalization, so they align
- * 1:1 with the original field.
+ * Wraps every match of one token in a field with the markers that `renderHighlights()` turns into
+ * tags. Positions come from a length-preserving normalization, so they align 1:1 with the original
+ * field.
*/
function highlightField(field: string | undefined, info: HighlightedTokenInfo): string | undefined {
if (!field) {
@@ -919,6 +936,7 @@ function highlightField(field: string | undefined, info: HighlightedTokenInfo):
return field;
}
+ const { open, close } = MARKERS[info.type];
let match: RegExpExecArray | null;
let wraps = 0;
while ((match = regex.exec(normalizePreservingLength(field))) !== null) {
@@ -928,9 +946,9 @@ function highlightField(field: string | undefined, info: HighlightedTokenInfo):
}
const matchEnd = match.index + matchLength;
- field = `${field.slice(0, match.index)}{${field.slice(match.index, matchEnd)}}${field.slice(matchEnd)}`;
- // Two marker characters were inserted, so advance past them as well.
- regex.lastIndex += 2;
+ field = `${field.slice(0, match.index)}${open}${field.slice(match.index, matchEnd)}${close}${field.slice(matchEnd)}`;
+ // The markers were inserted into the field, so advance past them as well.
+ regex.lastIndex += open.length + close.length;
if (++wraps >= MAX_HIGHLIGHT_WRAPS) {
break;
@@ -940,7 +958,14 @@ function highlightField(field: string | undefined, info: HighlightedTokenInfo):
return field;
}
-/** Escapes the text for display, then turns the { } markers into the <b> tags they stand for. */
+/**
+ * Escapes the text for display, then turns the markers into the tags they stand for. The doubled
+ * fuzzy pair is replaced first, so its braces are consumed before the single-brace pass runs.
+ */
function renderHighlights(text: string) {
- return escapeHtml(text).replace(/{/g, "<b>").replace(/}/g, "</b>");
+ return escapeHtml(text)
+ .replace(/\{\{/g, `<b class="${FUZZY_HIGHLIGHT_CLASS}">`)
+ .replace(/\}\}/g, "</b>")
+ .replace(/{/g, "<b>")
+ .replace(/}/g, "</b>");
}
diff --git a/packages/trilium-core/src/services/search/services/search_highlight.spec.ts b/packages/trilium-core/src/services/search/services/search_highlight.spec.ts
index e90e73e8e9..5a5e2bf065 100644
--- a/packages/trilium-core/src/services/search/services/search_highlight.spec.ts
+++ b/packages/trilium-core/src/services/search/services/search_highlight.spec.ts
@@ -90,6 +90,29 @@ describe("highlightSearchResults", () => {
expect(result.highlightedContentSnippet).toBe("<b>coool</b> result");
});
+ it("renders a fuzzy token in its own tag so it does not read as an exact hit", () => {
+ const result = makeResult("Note", "hello match world");
+ searchService.highlightSearchResults([result], [{ token: "match", type: "fuzzy" }]);
+ expect(result.highlightedContentSnippet).toBe(`hello <b class="search-fuzzy-match">match</b> world`);
+ });
+
+ it("keeps the two marker pairs apart when both appear in one field", () => {
+ const result = makeResult("Note", "exact and fuzzy");
+ searchService.highlightSearchResults([result], [
+ { token: "exact", type: "plain" },
+ { token: "fuzzy", type: "fuzzy" }
+ ]);
+ expect(result.highlightedContentSnippet).toBe(
+ `<b>exact</b> and <b class="search-fuzzy-match">fuzzy</b>`
+ );
+ });
+
+ it("strips braces out of the text so it cannot forge either marker", () => {
+ const result = makeResult("Note", "a {{match}} here");
+ searchService.highlightSearchResults([result], [{ token: "match", type: "plain" }]);
+ expect(result.highlightedContentSnippet).toBe("a <b>match</b> here");
+ });
+
it("skips invalid regex patterns instead of throwing", () => {
const result = makeResult("Note", "plain text");
expect(() =>