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(notes): don't let an inherited ~template suppress child:template
Elian Doran48205bc9a0e8
diff --git a/packages/trilium-core/src/services/notes.spec.ts b/packages/trilium-core/src/services/notes.spec.tsindex 273b734d89..3d3fa8cb26 100644--- a/packages/trilium-core/src/services/notes.spec.ts+++ b/packages/trilium-core/src/services/notes.spec.ts@@ -187,6 +187,23 @@ describe("notes service (real DB)", () => {expect(applied).toEqual([chosen.note.noteId]);});+ it("applies child:template even when the parent also has an inheritable ~template", () => {+ const template = createNote("root", { title: "spec-inh-tmpl", content: "<p>tmpl</p>" });+ const parent = createNote("root", {+ title: "spec-inh-tmpl-parent",+ attributes: [+ { type: "relation", name: "child:template", value: template.note.noteId },+ { type: "relation", name: "template", value: template.note.noteId, isInheritable: true }+ ]+ });++ const { note } = createNote(parent.note.noteId, { title: "spec-inh-tmpl-child", content: "" });++ // the relation must be owned, not merely inherited, so the template's content is copied+ expect(note.getOwnedRelations("template").map((r) => r.value)).toEqual([template.note.noteId]);+ expect(note.getContent()).toBe("<p>tmpl</p>");+ });+it("does not inherit the parent's child:template when the new note's type differs (#3015)", () => {const template = createNote("root", { title: "spec-child-tmpl-mismatch", content: "<p>day template</p>" });const parent = createNote("root", {diff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.tsindex 2e352476bb..8afc458e43 100644--- a/packages/trilium-core/src/services/notes.ts+++ b/packages/trilium-core/src/services/notes.ts@@ -108,9 +108,10 @@ function deriveMime(type: string, mime?: string) {}function copyChildAttributes(parentNote: BNote, childNote: BNote, isTypeDefaulted = false) {- // A template already present at this point was chosen by the user explicitly in the menu, and- // suppresses the `child:template` defaults (#3628). Deliberately read once, before the loop.- const hasUserChosenTemplate = childNote.hasRelation("template");+ // A template *owned* at this point was chosen by the user explicitly in the menu, and suppresses+ // the `child:template` defaults (#3628). Deliberately read once, before the loop, and owned-only+ // so an inherited `~template` on the parent doesn't count as a choice.+ const hasUserChosenTemplate = childNote.hasOwnedRelation("template");for (const attr of parentNote.getAttributes()) {if (attr.name.startsWith("child:")) {