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
client/search: decode HTML entities in quick search
Adorian Doran74b57a4f0a0e
diff --git a/packages/trilium-core/src/services/search/services/search.spec.ts b/packages/trilium-core/src/services/search/services/search.spec.tsindex a1741b2b92..b50de9bb58 100644--- a/packages/trilium-core/src/services/search/services/search.spec.ts+++ b/packages/trilium-core/src/services/search/services/search.spec.ts@@ -791,13 +791,25 @@ describe("Search", () => {it("surfaces link-preview url/title/description as separate lines in the quick-search snippet", () => {// The url/title/description live in data attributes that striptags would otherwise drop,- // leaving a blank snippet even though the note matched on the embedded title.+ // leaving a blank snippet even though the note matched on the embedded title. The entity-+ // encoded ampersand in the title must be decoded rather than shown as "&".const noteBuilder = note("Link preview note");- noteBuilder.note.getContent = () => "<section class=\"link-embed\" data-url=\"https://en.wikipedia.org/wiki/The_Terminator\" data-title=\"The Terminator - Wikipedia\" data-description=\"A 1984 science fiction film.\"> </section>";+ noteBuilder.note.getContent = () => "<section class=\"link-embed\" data-url=\"https://example.com/?a=1&b=2\" data-title=\"Tom & Jerry\" data-description=\"A 1984 science fiction film.\"> </section>";rootNote.child(noteBuilder);- const snippet = searchService.extractContentSnippet(noteBuilder.note.noteId, [ "terminator" ]);- expect(snippet.split("\n")).toEqual([ "https://en.wikipedia.org/wiki/The_Terminator", "The Terminator - Wikipedia", "A 1984 science fiction film." ]);+ const snippet = searchService.extractContentSnippet(noteBuilder.note.noteId, [ "jerry" ]);+ expect(snippet.split("\n")).toEqual([ "https://example.com/?a=1&b=2", "Tom & Jerry", "A 1984 science fiction film." ]);+ });++ it("decodes HTML entities in the quick-search snippet instead of showing escape codes", () => {+ // striptags leaves entities (<, >, &, ) untouched, which would surface as+ // literal escape codes in the dropdown.+ const noteBuilder = note("Entity note");+ noteBuilder.note.getContent = () => "<p>if a < b && b > c, then done</p>";+ rootNote.child(noteBuilder);++ const snippet = searchService.extractContentSnippet(noteBuilder.note.noteId, [ "done" ]);+ expect(snippet).toBe("if a < b && b > c, then done");});// FIXME: test what happens when we order without any filter criteriadiff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.tsindex 4b657a669f..0a12b5ff9d 100644--- a/packages/trilium-core/src/services/search/services/search.ts+++ b/packages/trilium-core/src/services/search/services/search.ts@@ -10,7 +10,7 @@ import { getLog } from "../../log.js";import protectedSessionService from "../../protected_session.js";import scriptService from "../../script.js";import { isScriptingEnabled } from "../../scripting_guard.js";-import { escapeHtml, escapeRegExp } from "../../utils/index.js";+import { escapeHtml, escapeRegExp, unescapeHtml } from "../../utils/index.js";import type Expression from "../expressions/expression.js";import SearchContext from "../search_context.js";import SearchResult from "../search_result.js";@@ -513,6 +513,9 @@ function extractContentSnippet(noteId: string, searchTokens: string[], maxLengthreturn `\n${[url, title, description].filter(Boolean).join("\n")}\n`;});content = striptags(content);+ // Decode HTML entities so the snippet shows real characters instead of escape codes+ // (e.g. "<", "&", " ") — attribute-sourced text above is entity-encoded too.+ content = unescapeHtml(content).replace(/ /g, " ");} else if (note.type === "llmChat") {// The note stores the whole conversation as a JSON blob; show the readable prose only.content = extractLlmChatText(content);