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(spellcheck): don't merge words every time
Elian Doran03136611a1c2
diff --git a/apps/server/src/services/custom_dictionary.spec.ts b/apps/server/src/services/custom_dictionary.spec.tsindex 2f74fd41aa..83803beaee 100644--- a/apps/server/src/services/custom_dictionary.spec.ts+++ b/apps/server/src/services/custom_dictionary.spec.ts@@ -88,7 +88,7 @@ describe("custom_dictionary", () => {expect(session.addWordToSpellCheckerDictionary).toHaveBeenCalledWith("banana");});- it("merges note and local words when both have content", async () => {+ it("only loads note words when both note and local have content", async () => {becca.reset();buildNote({id: "_customDictionary",@@ -100,10 +100,12 @@ describe("custom_dictionary", () => {await customDictionary.loadForSession(session);- expect(session.addWordToSpellCheckerDictionary).toHaveBeenCalledTimes(3);+ expect(session.addWordToSpellCheckerDictionary).toHaveBeenCalledTimes(2);+ expect(session.addWordToSpellCheckerDictionary).toHaveBeenCalledWith("apple");+ expect(session.addWordToSpellCheckerDictionary).toHaveBeenCalledWith("banana");});- it("clears local dictionary after merging", async () => {+ it("clears local dictionary when note has content", async () => {becca.reset();buildNote({id: "_customDictionary",@@ -120,37 +122,41 @@ describe("custom_dictionary", () => {expect(session.removeWordFromSpellCheckerDictionary).toHaveBeenCalledWith("cherry");});- it("does not save when local words are a subset of note words", async () => {+ it("handles note with whitespace and blank lines", async () => {becca.reset();buildNote({id: "_customDictionary",title: "Custom Dictionary",type: "code",- content: "apple\nbanana\ncherry"+ content: " apple \n\n banana \n\n"});- const session = mockSession(["apple", "banana"]);+ const session = mockSession();await customDictionary.loadForSession(session);- expect(session.addWordToSpellCheckerDictionary).toHaveBeenCalledTimes(3);- expect(session.removeWordFromSpellCheckerDictionary).toHaveBeenCalledTimes(2);+ expect(session.addWordToSpellCheckerDictionary).toHaveBeenCalledTimes(2);+ expect(session.addWordToSpellCheckerDictionary).toHaveBeenCalledWith("apple");+ expect(session.addWordToSpellCheckerDictionary).toHaveBeenCalledWith("banana");});- it("handles note with whitespace and blank lines", async () => {+ it("does not re-add words removed from the note but present locally", async () => {becca.reset();buildNote({id: "_customDictionary",title: "Custom Dictionary",type: "code",- content: " apple \n\n banana \n\n"+ content: "apple\nbanana"});- const session = mockSession();+ // "cherry" was previously in the note but user removed it;+ // it still lingers in Electron's local dictionary.+ const session = mockSession(["apple", "banana", "cherry"]);await customDictionary.loadForSession(session);- expect(session.addWordToSpellCheckerDictionary).toHaveBeenCalledTimes(2);+ // Only note words should be loaded, not "cherry".expect(session.addWordToSpellCheckerDictionary).toHaveBeenCalledWith("apple");expect(session.addWordToSpellCheckerDictionary).toHaveBeenCalledWith("banana");+ expect(session.addWordToSpellCheckerDictionary).not.toHaveBeenCalledWith("cherry");});it("handles missing dictionary note gracefully", async () => {diff --git a/apps/server/src/services/custom_dictionary.ts b/apps/server/src/services/custom_dictionary.tsindex 770cefb81e..11919537d6 100644--- a/apps/server/src/services/custom_dictionary.ts+++ b/apps/server/src/services/custom_dictionary.ts@@ -84,17 +84,10 @@ async function loadForSession(session: Session) {log.info(`Importing ${localWords.length} words from local spellchecker dictionary.`);merged = new Set(localWords);saveWords(merged);- clearFromLocalDictionary(session, localWords);- } else if (noteWords.size > 0 && localWords.length > 0) {- // Merge both sources so no words are lost.- const before = noteWords.size;- for (const w of localWords) {- merged.add(w);- }- if (merged.size > before) {- log.info(`Merged ${merged.size - before} new words from local dictionary.`);- saveWords(merged);- }+ }++ // Clear local dictionary so the note remains the single source of truth.+ if (localWords.length > 0) {clearFromLocalDictionary(session, localWords);}