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): normalize injected link text for single-token matching
perfectra1n5b57004914cd
diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.mdindex 927aa5ec33..6e0262af12 100644--- a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md@@ -175,6 +175,8 @@ The last point is the least obvious: if a note's only content is a reference lin| Query | Setup | Matches? | Why || --- | --- | --- | --- || `special topic` | a note whose only content is a reference link to a note titled `Special Topic` | Yes — the linking note is found; the linked target ranks first | the target's title is indexed into the linking note's searchable text |+| `special` | the same note (only a reference link to `Special Topic`) | Yes — a single word from the target's title is enough to find the linking note | the indexed title is normalized just like body text, so even one lowercased word matches |+| `zurich` | a note whose only content is a reference link to a note titled `Zürich` | Yes — the linking note is found | the indexed title has its accents normalized, so the plain form matches the accented title |### Diacriticsdiff --git a/packages/trilium-core/src/services/search/docs_examples.spec.ts b/packages/trilium-core/src/services/search/docs_examples.spec.tsindex 471b32df49..68f4dc7924 100644--- a/packages/trilium-core/src/services/search/docs_examples.spec.ts+++ b/packages/trilium-core/src/services/search/docs_examples.spec.ts@@ -250,6 +250,33 @@ describe("Search documentation examples", () => {// the linked target (direct title match) ranks above the linking note (indirect match)expect(targetRank).toBeLessThan(linkerRank);});++ // Docs: Search.md § "What is searchable" — example 9 (single word)+ it("a single word `special` from the target's title still finds the reference-linking note", () => {+ const target = contentNote("Special Topic", "");+ const linker = contentNote(+ "Linker",+ `<p>see <a class="reference-link" href="#root/${target.noteId}"></a></p>`+ );++ const results = search("special");++ expect(rank(results, target.noteId)).toBeGreaterThanOrEqual(0);+ expect(rank(results, linker.noteId)).toBeGreaterThanOrEqual(0);+ });++ // Docs: Search.md § "What is searchable" — example 9 (diacritic-insensitive)+ it("`zurich` finds a note reference-linking to a note titled `Zürich` (diacritics normalized)", () => {+ const target = contentNote("Zürich", "");+ const linker = contentNote(+ "Linker",+ `<p>see <a class="reference-link" href="#root/${target.noteId}"></a></p>`+ );++ const results = search("zurich");++ expect(rank(results, linker.noteId)).toBeGreaterThanOrEqual(0);+ });});describe('Diacritics — normalization strips accents on both sides', () => {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 4e57ee92a0..94b26ec6b6 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@@ -46,34 +46,46 @@ describe("Reference-link & link-preview searchability", () => {it("appends the resolved title of an empty-text reference link", () => {// dataDowncast markup from referencelink.ts: <a href=... class="reference-link">title</a>.// The stored anchor text is stale/empty, so the real title must come from the resolver.+ // The appended title is normalized (lowercased) to match the normalized body.const html = `<p>See <a href="#root/abc123DEF456" class="reference-link"></a> for details.</p>`;const result = preprocessContent(html, type, mime, false, (id) => (id === "abc123DEF456" ? "Special Topic" : null));- expect(result).toContain("Special Topic");+ expect(result).toContain("special topic");+ });++ it("normalizes injected link text so a lowercased single-token query matches", () => {+ // Extraction runs against the case-preserved original (so link-target noteIds resolve),+ // but the APPENDED text must be lowercased and diacritic-stripped: the default single-token+ // content match lowercases the query token and does a raw includes(), so `zurich` must hit+ // a linked "Zürich" (and `special` a linked "Special Topic") in phase 1, not only via fuzzy.+ const html = `<p><a href="#root/city1" class="reference-link"></a></p>`;+ const result = preprocessContent(html, type, mime, false, (id) => (id === "city1" ? "Zürich" : null));+ expect(result).toContain("zurich");+ expect(result).not.toContain("Zürich");});it("also resolves plain internal links (not only reference links)", () => {const html = `<p>Jump to <a href="#root/parent1/target99">whatever</a>.</p>`;const result = preprocessContent(html, type, mime, false, (id) => (id === "target99" ? "Linked Note" : null));- expect(result).toContain("Linked Note");+ expect(result).toContain("linked note");});it("extracts link-embed metadata and entity-decodes attribute values", () => {// dataDowncast markup from link_embed_editing.ts (metadataViewAttributes): class first,- // then META_KEYS-ordered data-* attributes.+ // then META_KEYS-ordered data-* attributes. Injected metadata is normalized (lowercased).const html = `<section class="link-embed" data-url="https://example.com/a" data-embed-type="card" data-title="Great Article" data-description="Things & stuff" data-site-name="Example Site"><div class="link-embed-preview-wrapper"></div></section>`;const result = preprocessContent(html, type, mime, false);expect(result).toContain("https://example.com/a");- expect(result).toContain("Great Article");- expect(result).toContain("Things & stuff");- expect(result).toContain("Example Site");+ expect(result).toContain("great article");+ expect(result).toContain("things & stuff");+ expect(result).toContain("example site");});it("extracts link-mention metadata", () => {const html = `<p>a <span class="link-mention" data-url="https://example.com/x" data-title="Mentioned Thing" data-site-name="Mention Site">x</span> b</p>`;const result = preprocessContent(html, type, mime, false);expect(result).toContain("https://example.com/x");- expect(result).toContain("Mentioned Thing");- expect(result).toContain("Mention Site");+ expect(result).toContain("mentioned thing");+ expect(result).toContain("mention site");});it("appends a repeated link target's title only once", () => {@@ -86,7 +98,7 @@ describe("Reference-link & link-preview searchability", () => {}return null;});- expect(result.match(/Repeated Target/g)?.length).toEqual(1);+ expect(result.match(/repeated target/g)?.length).toEqual(1);expect(calls).toEqual(1);});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.tsindex cf8c3a4906..a25cb29641 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@@ -26,7 +26,14 @@ export default function preprocessContent(rawContent: string | Uint8Array, type:content = stripTags(content);if (injectedText) {- content = `${content} ${injectedText}`;+ // Normalize the appended text the same way the body was normalized above.+ // Extraction ran against the case-preserved original so link-target noteIds+ // still resolved, but the default single-token content match lowercases the+ // query token and does a raw `content.includes()` (see note_content_fulltext+ // matchesContent), so the injected titles/metadata must be lowercased and+ // diacritic-stripped too — otherwise `special` misses a linked "Special Topic"+ // and `zurich` misses a linked "Zürich" in phase 1.+ content = `${content} ${normalize(injectedText)}`;}}diff --git a/packages/trilium-core/src/services/search/services/search.spec.ts b/packages/trilium-core/src/services/search/services/search.spec.tsindex 8278aa7af6..6272c1beb8 100644--- a/packages/trilium-core/src/services/search/services/search.spec.ts+++ b/packages/trilium-core/src/services/search/services/search.spec.ts@@ -885,6 +885,34 @@ describe("Search", () => {expect(targetRank).toBeLessThan(linkerRank);});+ it("finds a note by a SINGLE word from a reference-link's target title, as a phase-1 exact match", () => {+ // The injected title is normalized like the body, so a single lowercased query token+ // hits it via the default *=* content includes() in phase 1 — not only via phase-2+ // fuzzy (where it would rank last, or be dropped entirely once phase 1 is sufficient).+ const target = contentNote("Special Topic", "");+ const linker = contentNote("Linker", `<p>see <a class="reference-link" href="#root/${target.noteId}"></a></p>`);++ const context = new SearchContext();+ const searchResults = searchService.findResultsWithQuery("special", context);++ expect(rank(searchResults, linker.noteId)).toBeGreaterThanOrEqual(0);+ // exact_word (not "fuzzy") proves the injected title matched in phase 1, not the fallback.+ expect(context.contentMatches.get(linker.noteId)?.tier).toBe("exact_word");+ });++ it("finds a note via a reference-link target title with diacritics (zurich -> Zürich), as an exact match", () => {+ // Normalization strips diacritics on the injected title too, so `zurich` matches a+ // linked "Zürich"; without normalizing the injected text this only surfaced via fuzzy.+ const target = contentNote("Zürich", "");+ const linker = contentNote("Linker", `<p>see <a class="reference-link" href="#root/${target.noteId}"></a></p>`);++ const context = new SearchContext();+ const searchResults = searchService.findResultsWithQuery("zurich", context);++ expect(rank(searchResults, linker.noteId)).toBeGreaterThanOrEqual(0);+ expect(context.contentMatches.get(linker.noteId)?.tier).toBe("exact_word");+ });+it("skips content matching in fast search, so content scoring is a natural no-op", () => {const doc = contentNote("Doc", "please sync the database now");