Skip to content

Build your personal knowledge base with Trilium Notes

fix(becca): drop the searchable title cache when a note is renamed

Elian Doranb818f1bbef4c
diff --git a/packages/trilium-core/src/becca/becca_loader.spec.ts b/packages/trilium-core/src/becca/becca_loader.spec.ts
index 3cf14b52e2..81df92ae7c 100644
--- a/packages/trilium-core/src/becca/becca_loader.spec.ts
+++ b/packages/trilium-core/src/becca/becca_loader.spec.ts
@@ -246,6 +246,24 @@ describe("becca_loader", () => {
});
});
+ describe("note update", () => {
+ it("drops the title-derived search caches so a rename is searchable under its new title", () => {
+ const note = createNote("root");
+ // Populate both caches, as a search would.
+ expect(note.getFlatText()).toContain("becca-loader-spec");
+
+ expect(note.getSearchableTitle().normalized).toBe(note.title.toLowerCase());
+
+ // A local rename assigns the title and saves; `updateFromRow` is not involved.
+ note.title = "Renamed Vienna";
+ getContext().init(() => note.save());
+
+ expect(note.getSearchableTitle().normalized).toBe("renamed vienna");
+ expect(note.getSearchableTitle().words).toEqual([ "renamed", "vienna" ]);
+ expect(note.getFlatText()).toContain("renamed vienna");
+ });
+ });
+
it("exposes load and reload through the default export", () => {
expect(typeof beccaLoader.load).toBe("function");
expect(typeof beccaLoader.reload).toBe("function");
diff --git a/packages/trilium-core/src/becca/becca_loader.ts b/packages/trilium-core/src/becca/becca_loader.ts
index 87f6f91278..8d1c93c6e4 100644
--- a/packages/trilium-core/src/becca/becca_loader.ts
+++ b/packages/trilium-core/src/becca/becca_loader.ts
@@ -207,6 +207,9 @@ function noteUpdated(entityRow: NoteRow) {
// TODO, this wouldn't have worked in the original implementation since the variable was named __flatTextCache.
// type / mime could have been changed, and they are present in flatTextCache
note.__flatTextCache = null;
+ // A local rename assigns `title` and saves without going through `updateFromRow`, so this
+ // is the only point where the caches derived from the title are dropped for it.
+ note.__searchableTitleCache = null;
}
}
diff --git a/packages/trilium-core/src/becca/entities/bnote.ts b/packages/trilium-core/src/becca/entities/bnote.ts
index 3ca0360a61..539f299b0c 100644
--- a/packages/trilium-core/src/becca/entities/bnote.ts
+++ b/packages/trilium-core/src/becca/entities/bnote.ts
@@ -84,10 +84,10 @@ class BNote extends AbstractBeccaEntity<BNote> {
targetRelations!: BAttribute[];
__flatTextCache!: string | null;
- __searchableTitleCache!: SearchableTitle | null;
+ __searchableTitleCache: SearchableTitle | null = null;
private __attributeCache!: BAttribute[] | null;
- private __isArchivedCache!: boolean | null;
+ private __isArchivedCache: boolean | null = null;
private __inheritableAttributeCache!: BAttribute[] | null;
private __ancestorCache!: BNote[] | null;
diff --git a/packages/trilium-core/src/becca/entities/bnote_attributes.spec.ts b/packages/trilium-core/src/becca/entities/bnote_attributes.spec.ts
index e7bb9cee98..f947e4748a 100644
--- a/packages/trilium-core/src/becca/entities/bnote_attributes.spec.ts
+++ b/packages/trilium-core/src/becca/entities/bnote_attributes.spec.ts
@@ -350,4 +350,16 @@ describe("BNote attribute methods (real DB)", () => {
expect(() => getContext().init(() => note.setAttributeValueById("missing-attr-id", "x"))).toThrow();
});
});
+ describe("isArchived", () => {
+ it("reports an archived label and stops reporting it once removed", () => {
+ const { note } = createNote("root");
+ expect(note.isArchived).toBe(false);
+
+ getContext().init(() => note.addLabel("archived"));
+ expect(note.isArchived).toBe(true);
+
+ getContext().init(() => note.removeLabel("archived"));
+ expect(note.isArchived).toBe(false);
+ });
+ });
});