Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): name an icon in the snippet it was found by

Elian Doran6f22b0b75718
diff --git a/packages/trilium-core/src/services/search/expressions/note_content_fulltext_preprocessor.ts b/packages/trilium-core/src/services/search/expressions/note_content_fulltext_preprocessor.ts
index 8e2613a19b..1df4626e7b 100644
--- a/packages/trilium-core/src/services/search/expressions/note_content_fulltext_preprocessor.ts
+++ b/packages/trilium-core/src/services/search/expressions/note_content_fulltext_preprocessor.ts
@@ -141,11 +141,28 @@ const LINK_PREVIEW_TAG_RE = /<(?:section|span)\b[^>]*\bclass=["'][^"']*\blink-(?
const INTERNAL_LINK_RE = /href="[^"]*#root[a-zA-Z0-9_\/]*\/([a-zA-Z0-9_]+)\/?"/g;
// Must match what inline_icon_editing.ts writes in its dataDowncast.
-const ICON_TAG_RE = /<span\b[^>]*\bclass=["'][^"']*\btn-icon\b[^"']*["'][^>]*>/gi;
+export const ICON_TAG_RE = /<span\b[^>]*\bclass=["'][^"']*\btn-icon\b[^"']*["'][^>]*>/gi;
/** The class every icon wears beside its pack's, which says nothing about which icon it is. */
const ICON_MARKER_CLASS = "tn-icon";
+/**
+ * The pack classes an icon tag wears — `bx-error-circle` from `tn-icon bx bx-error-circle`.
+ *
+ * The marker is worn by every icon, and a pack's bare prefix — `bx` — by every icon that pack
+ * holds, so neither names one.
+ */
+export function readIconClasses(tag: string): string[] {
+ return (extractAttribute(tag, "class") ?? "")
+ .split(/\s+/)
+ .filter((className) => className !== ICON_MARKER_CLASS && className.includes("-"));
+}
+
+/** The name inside a pack class: `error-circle` from `bx-error-circle`. */
+export function readIconName(className: string): string {
+ return className.slice(className.indexOf("-") + 1);
+}
+
/** Collects extra searchable text from a note's link previews and internal-link targets. */
function extractLinkSearchText(content: string, resolveNoteTitle?: NoteTitleResolver): string {
const parts: string[] = [];
@@ -192,14 +209,9 @@ function extractIconSearchText(content: string): string {
const parts = new Set<string>();
for (const tag of content.match(ICON_TAG_RE) ?? []) {
- for (const className of (extractAttribute(tag, "class") ?? "").split(/\s+/)) {
- // A pack's bare prefix — `bx` — is worn by every icon it holds, so it names none.
- if (className === ICON_MARKER_CLASS || !className.includes("-")) {
- continue;
- }
-
+ for (const className of readIconClasses(tag)) {
parts.add(className);
- parts.add(className.slice(className.indexOf("-") + 1));
+ parts.add(readIconName(className));
}
}
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 6003fdf313..27d4fdddaf 100644
--- a/packages/trilium-core/src/services/search/services/search.spec.ts
+++ b/packages/trilium-core/src/services/search/services/search.spec.ts
@@ -1031,6 +1031,18 @@ describe("Search", () => {
return searchService.buildSearchResultDetails(results, searchContext);
}
+ it("names the icon a note was found by, and highlights it", () => {
+ // An icon is an empty element. Striptags leaves no trace of it, so a note found by the
+ // icon's name had nothing in its snippet to centre on or to mark.
+ const icons = contentNote("Icons", `<p>${"padding ".repeat(60)}`
+ + `Press <span class="tn-icon bx bx-star"></span> to favourite.</p>`);
+
+ const detail = detailsFor("star").find((d) => d.noteId === icons.noteId);
+
+ expect(detail?.contentSnippet).toContain("[star]");
+ expect(detail?.highlightedContentSnippet).toContain("<b>star</b>");
+ });
+
it("highlights the word a fuzzy body match actually matched", () => {
// "writer" is two edits from "orbiter", which AUTO allows for a 7-character token, so
// this note is a result even though the word the user typed appears nowhere in it.
diff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.ts
index 47d255c837..99355d8bb5 100644
--- a/packages/trilium-core/src/services/search/services/search.ts
+++ b/packages/trilium-core/src/services/search/services/search.ts
@@ -13,6 +13,9 @@ import scriptService from "../../script.js";
import { isScriptingEnabled } from "../../scripting_guard.js";
import { escapeHtml, escapeRegExp, normalizePreservingLength, unescapeHtml } from "../../utils/index.js";
import type Expression from "../expressions/expression.js";
+import {
+ ICON_TAG_RE, readIconClasses, readIconName
+} from "../expressions/note_content_fulltext_preprocessor.js";
import SearchContext from "../search_context.js";
import SearchResult, { precomputeScoringTerms } from "../search_result.js";
import handleParens from "./handle_parens.js";
@@ -607,6 +610,14 @@ function extractContentSnippet(noteId: string, searchTokens: HighlightedTokenInf
const description = element.match(/\bdata-description="([^"]*)"/i)?.[1] ?? "";
return `\n${[url, title, description].filter(Boolean).join("\n")}\n`;
});
+ // An icon is an empty element, so striptags leaves no trace of it and a note found by
+ // the icon's name had nothing in its snippet to centre on or to mark. Name it instead;
+ // the snippet is escaped before it is shown, so the icon itself cannot be drawn there.
+ content = content.replace(ICON_TAG_RE, (tag) => {
+ const [ className ] = readIconClasses(tag);
+
+ return className ? ` [${readIconName(className)}] ` : "";
+ });
content = striptags(content);
// Decode HTML entities so the snippet shows real characters instead of escape codes
// (e.g. "&lt;", "&amp;", "&nbsp;") — attribute-sourced text above is entity-encoded too.