Skip to content

Build your personal knowledge base with Trilium Notes

fix(templates): tell tree inheritance apart by ancestry, not the flag

Elian Doran8619fdf4a8ed
diff --git a/apps/client/src/services/note_create.spec.ts b/apps/client/src/services/note_create.spec.ts
index e2563e23da..6383c5097b 100644
--- a/apps/client/src/services/note_create.spec.ts
+++ b/apps/client/src/services/note_create.spec.ts
@@ -346,6 +346,7 @@ describe("chooseNoteType", () => {
{ id: "overriding-tpl", title: "Overriding", "#template": "", "~template:newNoteDefaultParent": "people" }
] },
{ id: "derived-tpl", title: "Derived", "#template": "", "~template": "person-tpl" },
+ { id: "derived2-tpl", title: "Derived from folder", "#template": "", "~template": "inheriting-tpl" },
{ id: "plain-tpl", title: "Plain", "#template": "" }
]
});
@@ -369,12 +370,18 @@ describe("chooseNoteType", () => {
.resolves.toMatchObject({ notePath: "root/people", cloneToNoteIds: ["advisors"] });
});
- it("ignores a non-inheritable destination flowing through ~template from the base template", async () => {
+ it("ignores a destination flowing through ~template, whatever its inheritable flag", async () => {
// easy-froca seeds owned attributes into the attribute cache, which would mask template
// inheritance; recomputing resolves it the way the application does.
noteAttributeCache.invalidate();
+
+ // derived-tpl's base owns the relation without the flag; derived2-tpl's base sits in a
+ // folder whose relation is inheritable, so the flag arrives intact and only the
+ // owner-is-an-ancestor check tells the two inheritance paths apart.
await expect(choose({ success: true, noteType: "text", templateNoteId: "derived-tpl" }))
.resolves.toEqual({ success: true, noteType: "text", templateNoteId: "derived-tpl" });
+ await expect(choose({ success: true, noteType: "text", templateNoteId: "derived2-tpl" }))
+ .resolves.toEqual({ success: true, noteType: "text", templateNoteId: "derived2-tpl" });
});
it("uses an inherited default parent, unless the template owns one, which replaces it", async () => {
diff --git a/apps/client/src/services/note_create.ts b/apps/client/src/services/note_create.ts
index 3b277e84a9..f41de24063 100644
--- a/apps/client/src/services/note_create.ts
+++ b/apps/client/src/services/note_create.ts
@@ -171,10 +171,12 @@ async function createNoteWithTypePrompt(parentNotePath: string, options: CreateN
* template overrides an inheritable default on an ancestor instead of adding to it. Undefined when
* no target resolves to a note with a visible path.
*
- * Inherited relations count only when marked inheritable. Attributes also flow through `~template`
- * regardless of that flag, and every note created from a template carries `~template` — without the
- * check, a note created from a template and later marked `#template` itself would silently adopt
- * the original template's destination.
+ * Inherited relations count only when owned by an ancestor of the template, i.e. inherited through
+ * the tree. Attributes also flow through `~template` — with their inheritable flag intact — and
+ * every note created from a template carries `~template`, so neither the flag nor mere presence in
+ * `getRelations()` proves tree inheritance; without the ancestry check, a note created from a
+ * template and later marked `#template` itself would silently adopt the original template's
+ * destination.
*/
async function getTemplateDefaultParents(templateNoteId: string) {
const templateNote = await froca.getNote(templateNoteId);
@@ -183,9 +185,12 @@ async function getTemplateDefaultParents(templateNoteId: string) {
}
const ownedRelations = templateNote.getOwnedRelations("template:newNoteDefaultParent");
+ const ancestorNoteIds = new Set(templateNote.getAllNotePaths().flat());
+ ancestorNoteIds.delete(templateNote.noteId);
const relations = ownedRelations.length
? ownedRelations
- : templateNote.getRelations("template:newNoteDefaultParent").filter((attr) => attr.isInheritable);
+ : templateNote.getRelations("template:newNoteDefaultParent")
+ .filter((attr) => attr.isInheritable && ancestorNoteIds.has(attr.noteId));
const targetNoteIds = [...new Set(relations.map((relation) => relation.value).filter(Boolean))];
const targetNotes = await froca.getNotes(targetNoteIds, true);
const hoistedNoteId = appContext.tabManager.getActiveContext()?.hoistedNoteId;
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Templates.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Templates.html
index b405c2f6cc..6df980e2aa 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Templates.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Templates.html
@@ -68,9 +68,9 @@
as&nbsp;<a class="reference-link" href="#root/_help_IakOLONlIfGI">Cloning Notes</a>.
The relation can also be inherited (e.g. from a folder holding several
templates), in which case a relation owned by the template itself replaces
- the inherited one. Inheritance requires the relation to be marked as inheritable:
- a template created from another template does not adopt that template's
- default parent.</p>
+ the inherited one. The default parent is only inherited from the template's
+ ancestors in the tree: a template created from another template does not
+ adopt that template's default parent.</p>
<p>The default parent is used whenever a note is created from the template
through the <em>Choose note type</em> dialog:</p>
<ul>
diff --git a/docs/User Guide/User Guide/Advanced Usage/Templates.md b/docs/User Guide/User Guide/Advanced Usage/Templates.md
index 1c845a5aed..e5daf0dd4e 100644
--- a/docs/User Guide/User Guide/Advanced Usage/Templates.md
+++ b/docs/User Guide/User Guide/Advanced Usage/Templates.md
@@ -37,7 +37,7 @@ To specify a template for child notes, you can use a `~child:template` relation
A template can gather the notes created from it in one place, instead of having them land wherever they happen to be created. To do so, add a `~template:newNoteDefaultParent` relation to the template note, pointing at the note that should hold the instances, for example a _Person_ template pointing to a _People_ note.
-Adding the relation several times places the new note in every target, as <a class="reference-link" href="../Basic%20Concepts%20and%20Features/Notes/Cloning%20Notes.md">Cloning Notes</a>. The relation can also be inherited (e.g. from a folder holding several templates), in which case a relation owned by the template itself replaces the inherited one. Inheritance requires the relation to be marked as inheritable: a template created from another template does not adopt that template's default parent.
+Adding the relation several times places the new note in every target, as <a class="reference-link" href="../Basic%20Concepts%20and%20Features/Notes/Cloning%20Notes.md">Cloning Notes</a>. The relation can also be inherited (e.g. from a folder holding several templates), in which case a relation owned by the template itself replaces the inherited one. The default parent is only inherited from the template's ancestors in the tree: a template created from another template does not adopt that template's default parent.
The default parent is used whenever a note is created from the template through the _Choose note type_ dialog: