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(search): make a note marked with an icon findable
Elian Doran264f1591a3d6
diff --git a/packages/trilium-core/src/services/search/expressions/note_content_fulltext_preprocessor.spec.ts b/packages/trilium-core/src/services/search/expressions/note_content_fulltext_preprocessor.spec.tsindex f552f5cfbe..a6988f79db 100644--- a/packages/trilium-core/src/services/search/expressions/note_content_fulltext_preprocessor.spec.ts+++ b/packages/trilium-core/src/services/search/expressions/note_content_fulltext_preprocessor.spec.ts@@ -124,6 +124,53 @@ describe("Reference-link & link-preview searchability", () => {});});+describe("Inline icon searchability", () => {+ const type: NoteType = "text";+ const mime = "text/html";++ it("makes an icon findable by its class and by the name inside it", () => {+ // dataDowncast markup from inline_icon_editing.ts: an element with nothing inside it, so+ // stripping the markup would otherwise leave no trace of the icon at all.+ const html = `<p>Press <span class="tn-icon bx bx-error-circle"></span> to stop.</p>`;++ const result = preprocessContent(html, type, mime);++ expect(result).toContain("press");+ expect(result).toContain("bx-error-circle");+ expect(result).toContain("error-circle");+ });++ it("leaves out the marker class and the pack's bare prefix, which name no icon", () => {+ const html = `<p><span class="tn-icon bx bx-star"></span></p>`;++ const words = preprocessContent(html, type, mime).split(/\s+/).filter(Boolean);++ expect(words).toEqual([ "bx-star", "star" ]);+ });++ it("names an icon once however often the note carries it", () => {+ const icon = `<span class="tn-icon bx bx-star"></span>`;+ const html = `<p>${icon} one ${icon} two ${icon}</p>`;++ const words = preprocessContent(html, type, mime).split(/\s+/).filter(Boolean);++ expect(words.filter((word) => word === "star")).toHaveLength(1);+ });++ it("reads the icon out of a colour wrapper, and out of single-quoted markup", () => {+ const html = `<p><span style="color:red;">`+ + `<span class='tn-icon bx bx-bulb'></span></span></p>`;++ expect(preprocessContent(html, type, mime)).toContain("bulb");+ });++ it("leaves content carrying no icon as it was", () => {+ const html = "<p>Nothing to see here.</p>";++ expect(preprocessContent(html, type, mime).trim()).toBe("nothing to see here.");+ });+});+describe("Spreadsheet preprocessing", () => {const type: NoteType = "spreadsheet";const mime = "application/json";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.tsindex 5b70d1d220..8e2613a19b 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@@ -15,7 +15,10 @@ export default function preprocessContent(rawContent: string | Uint8Array, type:if (!raw) {// Runs before stripTags(), which discards the data-* metadata and anchor text, and// against the original markup, because normalize() lowercases the noteIds it needs.- const injectedText = extractLinkSearchText(originalContent, resolveNoteTitle);+ const injectedText = [+ extractLinkSearchText(originalContent, resolveNoteTitle),+ extractIconSearchText(originalContent)+ ].filter(Boolean).join(" ");// Content size already filtered at DB level, safe to processcontent = stripTags(content);@@ -137,6 +140,12 @@ const LINK_PREVIEW_TAG_RE = /<(?:section|span)\b[^>]*\bclass=["'][^"']*\blink-(?// Mirrors findInternalLinks() in services/notes.ts, inlined to keep that import out of here.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;++/** The class every icon wears beside its pack's, which says nothing about which icon it is. */+const ICON_MARKER_CLASS = "tn-icon";+/** Collects extra searchable text from a note's link previews and internal-link targets. */function extractLinkSearchText(content: string, resolveNoteTitle?: NoteTitleResolver): string {const parts: string[] = [];@@ -171,6 +180,32 @@ function extractLinkSearchText(content: string, resolveNoteTitle?: NoteTitleResoreturn parts.join(" ");}+/**+ * Collects what the icons in a note's content should be findable by.+ *+ * An icon is an empty element, so stripping the markup leaves nothing of it behind and a note+ * marked with one is unfindable. Each icon's pack class and the name inside it are appended as+ * words instead — `bx bx-error-circle` gives `bx-error-circle error-circle` — so the class serves+ * a search that knows it and the name serves one that does not.+ */+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;+ }++ parts.add(className);+ parts.add(className.slice(className.indexOf("-") + 1));+ }+ }++ return [ ...parts ].join(" ");+}+/** Reads a single/double-quoted HTML attribute value from a tag string, entity-decoded. */function extractAttribute(tag: string, attrName: string): string | null {const re = new RegExp(`\\b${attrName}=(?:"([^"]*)"|'([^']*)')`, "i");