Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): name an icon by its own class, not by one it happens to wear

Elian Doran8158c1b4a481
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.ts
index a6988f79db..fa2fce18e9 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
@@ -148,6 +148,17 @@ describe("Inline icon searchability", () => {
expect(words).toEqual([ "bx-star", "star" ]);
});
+ it("names an icon by its pack's class, not by another class the tag carries", () => {
+ // The editor keeps every class an imported icon wears, so a tag can carry a class from
+ // somewhere else entirely. Only a class whose prefix the tag also wears bare names an icon,
+ // and the snippet builder takes the first of them.
+ const html = `<p><span class="tn-icon text-big 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>`;
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 1df4626e7b..67b7c9d85f 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
@@ -150,12 +150,20 @@ 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.
+ * holds, so neither names one. A tag can also wear a class from outside any pack, since the editor
+ * keeps what imported markup carried: only a class the tag declares the prefix of draws a glyph,
+ * so only those are read. Markup declaring no prefix falls back to every hyphenated class, which
+ * is all there is left to go on.
*/
export function readIconClasses(tag: string): string[] {
- return (extractAttribute(tag, "class") ?? "")
+ const classNames = (extractAttribute(tag, "class") ?? "")
.split(/\s+/)
- .filter((className) => className !== ICON_MARKER_CLASS && className.includes("-"));
+ .filter((className) => className && className !== ICON_MARKER_CLASS);
+ const prefixes = new Set(classNames.filter((className) => !className.includes("-")));
+ const packClasses = classNames.filter((className) => className.includes("-"));
+ const named = packClasses.filter((className) => prefixes.has(className.slice(0, className.indexOf("-"))));
+
+ return named.length ? named : packClasses;
}
/** The name inside a pack class: `error-circle` from `bx-error-circle`. */