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
fix(search): match a note body as the editor displays it
sxh3131169f6ba1681
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..a8a30e2f8a 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@@ -191,4 +191,35 @@ describe("LLM chat preprocessing", () => {});expect(preprocessContent(chat, type, mime)).toEqual("what is a branch? a parent-child link.");});+});++describe("HTML entity decoding", () => {+ const type: NoteType = "text";+ const mime = "text/html";++ it("makes the text the editor displays searchable", () => {+ // The editor shows "if a < b && b > c, then done" for this body, and extractContentSnippet()+ // decodes these same entities when it renders the hit — so a query in that shape has to match+ // the body it was copied from, rather than only the entity spelling stored in the blob.+ expect(preprocessContent("<p>AT&T reported earnings.</p>", type, mime)).toEqual("at&t reported earnings.");+ expect(preprocessContent("<p>if a < b && b > c, then done</p>", type, mime))+ .toEqual("if a < b && b > c, then done");+ });++ it("decodes an entity-encoded query separator inside an anchor's href", () => {+ const html = `<p>see <a href="https://example.com/?a=1&b=2">docs</a></p>`;++ expect(preprocessContent(html, type, mime)).toContain("https://example.com/?a=1&b=2");+ });++ it("decodes once, so a body showing an entity is still found by what it shows", () => {+ // "&amp;" displays as "&", and searching for that literal string must hit it — which+ // it would not if the decode ran twice and left a bare "&".+ expect(preprocessContent("<p>use &amp; to write one</p>", type, mime)).toContain("use & to write one");+ });++ it("keeps a raw content search against the stored markup", () => {+ // note.rawContent is the blob as written, entities and tags included, so it stays encoded.+ expect(preprocessContent("<p>AT&T</p>", type, mime, true)).toEqual("<p>at&t</p>");+ });});\ No newline at end of filediff --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..f5a34c0c1e 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@@ -20,6 +20,13 @@ export default function preprocessContent(rawContent: string | Uint8Array, type:// Content size already filtered at DB level, safe to processcontent = stripTags(content);+ // What striptags leaves behind is the body as the editor writes it, so entities stand in+ // for the characters on screen. Decoding them here is what makes a query copied off the+ // screen match; extractContentSnippet() already decodes the same set to render the hit.+ // Runs before the injected text is appended, since that one is decoded already and a+ // second pass would eat an "&lt;" the note means literally.+ content = unescapeHtml(content);+if (injectedText) {// The body above was normalized, and matchesContent() compares a lowercased query// token against the raw string, so the injected text must be normalized too.diff --git a/packages/trilium-core/src/services/search/services/search.spec.ts b/packages/trilium-core/src/services/search/services/search.spec.tsindex 6003fdf313..7f37d0f47c 100644--- a/packages/trilium-core/src/services/search/services/search.spec.ts+++ b/packages/trilium-core/src/services/search/services/search.spec.ts@@ -1160,6 +1160,41 @@ describe("Search", () => {expect(searchService.extractContentSnippet(noteBuilder.note.noteId, [ "secret" ])).toBe("");});+ describe("body text held in HTML entities", () => {+ function bodyNote(title: string, content: string) {+ return getContext().init(() => noteService.createNewNote({+ parentNoteId: "root",+ title,+ content,+ type: "text"+ }).note);+ }++ function finds(query: string, noteId: string) {+ return searchService.findResultsWithQuery(query, new SearchContext()).some((result) => result.noteId === noteId);+ }++ it("finds a body by the text the editor shows for it", () => {+ // The editor renders this body as "AT&T reported R&D spend, where a<b held.", and that is+ // the wording the quick-search snippet prints back. Searching for the words on screen found+ // nothing, because only was unwrapped while &, < and > stayed encoded.+ const telco = bodyNote("Telco", "<p>AT&T reported R&D spend, where a<b held.</p>");++ expect(finds("AT&T", telco.noteId)).toBe(true);+ expect(finds("R&D", telco.noteId)).toBe(true);+ expect(finds("a<b", telco.noteId)).toBe(true);+ });++ it("decodes once, so a body showing an entity is found by what it shows", () => {+ // This body reads "write &t rather than <" on screen. Decoding it a second time+ // would turn the shown "<" into a bare "<" and let the second query through.+ const literal = bodyNote("Literal", "<p>write &amp;t rather than &lt;</p>");++ expect(finds("<", literal.noteId)).toBe(true);+ expect(finds("at<t", literal.noteId)).toBe(false);+ });+ });+// FIXME: test what happens when we order without any filter criteria// it("comparison between labels", () => {