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): stop autocomplete dropping < from note titles
Elian Dorana7d9f7bb7164
diff --git a/packages/trilium-core/src/services/search/services/search.spec.ts b/packages/trilium-core/src/services/search/services/search.spec.tsindex b2bfd9e575..7eb1a774ac 100644--- a/packages/trilium-core/src/services/search/services/search.spec.ts+++ b/packages/trilium-core/src/services/search/services/search.spec.ts@@ -853,6 +853,23 @@ describe("Search", () => {expect(result.highlightedContentSnippet).toBe("Summary Title<br><b>Body</b> text here<br>After the block");});+ it("escapes angle brackets in the note title instead of dropping them", () => {+ // The title is interpolated into the autocomplete dropdown as raw HTML, so a title+ // containing markup-like text must come back escaped. Stripping only "<" would render+ // "Issues caused by <div>" as "Issues caused by div>".+ const result: any = { notePathTitle: "Issues caused by <div>", contentSnippet: "", attributeSnippet: "" };+ searchService.highlightSearchResults([ result ], [ "caused" ]);+ expect(result.highlightedNotePathTitle).toBe("Issues <b>caused</b> by <div>");++ // Escaping happens after highlighting, so a token that looks like part of an entity+ // ("lt" in "<") cannot cut the entity in half and produce "&<b>lt</b>;".+ const entityResult: any = { notePathTitle: "a < b", contentSnippet: "x < y", attributeSnippet: "#lt=1 < 2" };+ searchService.highlightSearchResults([ entityResult ], [ "lt" ]);+ expect(entityResult.highlightedNotePathTitle).toBe("a < b");+ expect(entityResult.highlightedContentSnippet).toBe("x < y");+ expect(entityResult.highlightedAttributeSnippet).toBe("#<b>lt</b>=1 < 2");+ });+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. The entity-diff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.tsindex 2f713294cb..d5290ee1da 100644--- a/packages/trilium-core/src/services/search/services/search.ts+++ b/packages/trilium-core/src/services/search/services/search.ts@@ -734,32 +734,29 @@ function searchNotesForAutocomplete(query: string, fastSearch: boolean = true) {function highlightSearchResults(searchResults: SearchResult[], highlightedTokens: string[], ignoreInternalAttributes = false) {highlightedTokens = Array.from(new Set(highlightedTokens));- // we remove < signs because they can cause trouble in matching and overwriting existing highlighted chunks- // which would make the resulting HTML string invalid.- // { and } are used for marking <b> and </b> tag (to avoid matches on single 'b' character)- // < and > are used for marking <small> and </small>- highlightedTokens = highlightedTokens.map((token) => token.replace(/[<>{}]/g, "")).filter((token) => !!token?.trim());+ // { and } are used for marking <b> and </b> tags (to avoid matches on a single 'b' character),+ // so a token containing either would overwrite the markers already placed in the text.+ highlightedTokens = highlightedTokens.map((token) => token.replace(/[{}]/g, "")).filter((token) => !!token?.trim());// sort by the longest, so we first highlight the longest matcheshighlightedTokens.sort((a, b) => (a.length > b.length ? -1 : 1));+ // Highlighting runs on the text as written, and the result is escaped afterwards. Escaping+ // first would let a token match inside an entity (searching for "lt" would cut < in half),+ // and stripping the offending characters instead would silently mangle the text - a title+ // like "Issues caused by <div>" would lose its opening bracket.+ // The only characters that have to go are the { } markers themselves.for (const result of searchResults) {- result.highlightedNotePathTitle = result.notePathTitle.replace(/[<{}]/g, "");+ result.highlightedNotePathTitle = result.notePathTitle.replace(/[{}]/g, "");- // Initialize highlighted content snippet+ // Initialize highlighted content snippet, preserving newlines for later conversion to <br>if (result.contentSnippet) {- // Escape HTML but preserve newlines for later conversion to <br>- result.highlightedContentSnippet = escapeHtml(result.contentSnippet);- // Remove any stray < { } that might interfere with our highlighting markers- result.highlightedContentSnippet = result.highlightedContentSnippet.replace(/[<{}]/g, "");+ result.highlightedContentSnippet = result.contentSnippet.replace(/[{}]/g, "");}// Initialize highlighted attribute snippetif (result.attributeSnippet) {- // Escape HTML but preserve newlines for later conversion to <br>- result.highlightedAttributeSnippet = escapeHtml(result.attributeSnippet);- // Remove any stray < { } that might interfere with our highlighting markers- result.highlightedAttributeSnippet = result.highlightedAttributeSnippet.replace(/[<{}]/g, "");+ result.highlightedAttributeSnippet = result.attributeSnippet.replace(/[{}]/g, "");}}@@ -767,6 +764,11 @@ function highlightSearchResults(searchResults: SearchResult[], highlightedTokensreturn text.substring(0, start) + prefix + text.substr(start, length) + suffix + text.substring(start + length);}+ /** Escapes the text for display, then turns the { } markers into the <b> tags they stand for. */+ function renderHighlights(text: string) {+ return escapeHtml(text).replace(/{/g, "<b>").replace(/}/g, "</b>");+ }+for (const token of highlightedTokens) {if (!token) {// Avoid empty tokens, which might cause an infinite loop.@@ -812,21 +814,19 @@ function highlightSearchResults(searchResults: SearchResult[], highlightedTokensfor (const result of searchResults) {if (result.highlightedNotePathTitle) {- result.highlightedNotePathTitle = result.highlightedNotePathTitle.replace(/{/g, "<b>").replace(/}/g, "</b>");+ result.highlightedNotePathTitle = renderHighlights(result.highlightedNotePathTitle);}if (result.highlightedContentSnippet) {- // Replace highlighting markers with HTML tags- result.highlightedContentSnippet = result.highlightedContentSnippet.replace(/{/g, "<b>").replace(/}/g, "</b>");- // Convert newlines to <br> tags for HTML display- result.highlightedContentSnippet = result.highlightedContentSnippet.replace(/\n/g, "<br>");+ result.highlightedContentSnippet = renderHighlights(result.highlightedContentSnippet)+ // Convert newlines to <br> tags for HTML display+ .replace(/\n/g, "<br>");}if (result.highlightedAttributeSnippet) {- // Replace highlighting markers with HTML tags- result.highlightedAttributeSnippet = result.highlightedAttributeSnippet.replace(/{/g, "<b>").replace(/}/g, "</b>");- // Convert newlines to <br> tags for HTML display- result.highlightedAttributeSnippet = result.highlightedAttributeSnippet.replace(/\n/g, "<br>");+ result.highlightedAttributeSnippet = renderHighlights(result.highlightedAttributeSnippet)+ // Convert newlines to <br> tags for HTML display+ .replace(/\n/g, "<br>");}}}