Skip to content

Build your personal knowledge base with Trilium Notes

style(search): keep the added comments to the invariant in place

sxh313ce7b71b2a77c
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 a8a30e2f8a..d2bb941f41 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
@@ -198,12 +198,13 @@ describe("HTML entity decoding", () => {
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&amp;T reported earnings.</p>", type, mime)).toEqual("at&t reported earnings.");
- expect(preprocessContent("<p>if a &lt; b &amp;&amp; b &gt; c, then&nbsp;done</p>", type, mime))
- .toEqual("if a < b && b > c, then done");
+ // `extractContentSnippet()` decodes these entities to render a hit, so the same spelling
+ // is what a query has to match.
+ const entities = "<p>if a &lt; b &amp;&amp; b &gt; c, then&nbsp;done</p>";
+
+ expect(preprocessContent("<p>AT&amp;T reported earnings.</p>", type, mime))
+ .toEqual("at&t reported earnings.");
+ expect(preprocessContent(entities, type, mime)).toEqual("if a < b && b > c, then done");
});
it("decodes an entity-encoded query separator inside an anchor's href", () => {
@@ -213,13 +214,14 @@ describe("HTML entity decoding", () => {
});
it("decodes once, so a body showing an entity is still found by what it shows", () => {
- // "&amp;amp;" displays as "&amp;", 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;amp; to write one</p>", type, mime)).toContain("use &amp; to write one");
+ // This body displays "&amp;"; a second decode would leave a bare "&" and stop matching the
+ // characters the note shows.
+ expect(preprocessContent("<p>use &amp;amp; to write one</p>", type, mime))
+ .toContain("use &amp; 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.
+ // `note.rawContent` is the blob as written, entities and tags included.
expect(preprocessContent("<p>AT&amp;T</p>", type, mime, true)).toEqual("<p>at&amp;t</p>");
});
});
\ No newline at end of file
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 f5a34c0c1e..1632e193f1 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,11 +20,9 @@ export default function preprocessContent(rawContent: string | Uint8Array, type:
// Content size already filtered at DB level, safe to process
content = 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 "&amp;lt;" the note means literally.
+ // The stored body holds entities where the editor shows characters, so decoding is what
+ // makes the searchable text the displayed one. Injected link text arrives already
+ // decoded, so this runs before it is appended and never twice over the same characters.
content = unescapeHtml(content);
if (injectedText) {
diff --git a/packages/trilium-core/src/services/search/services/search.spec.ts b/packages/trilium-core/src/services/search/services/search.spec.ts
index 7f37d0f47c..f4b9200299 100644
--- a/packages/trilium-core/src/services/search/services/search.spec.ts
+++ b/packages/trilium-core/src/services/search/services/search.spec.ts
@@ -1171,13 +1171,14 @@ describe("Search", () => {
}
function finds(query: string, noteId: string) {
- return searchService.findResultsWithQuery(query, new SearchContext()).some((result) => result.noteId === noteId);
+ const results = searchService.findResultsWithQuery(query, new SearchContext());
+
+ return results.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 &nbsp; was unwrapped while &amp;, &lt; and &gt; stayed encoded.
+ // This body reads "AT&T reported R&D spend, where a<b held." on screen, and that is the
+ // wording the result card prints back, so a query in that shape has to reach the body.
const telco = bodyNote("Telco", "<p>AT&amp;T reported R&amp;D spend, where a&lt;b held.</p>");
expect(finds("AT&T", telco.noteId)).toBe(true);
@@ -1186,8 +1187,8 @@ describe("Search", () => {
});
it("decodes once, so a body showing an entity is found by what it shows", () => {
- // This body reads "write &amp;t rather than &lt;" on screen. Decoding it a second time
- // would turn the shown "&lt;" into a bare "<" and let the second query through.
+ // The body displays "&amp;" and "&lt;". A second decode would turn the second one into
+ // a bare "<", which is not what the note says.
const literal = bodyNote("Literal", "<p>write &amp;amp;t rather than &amp;lt;</p>");
expect(finds("&lt;", literal.noteId)).toBe(true);