Skip to content

Build your personal knowledge base with Trilium Notes

fix(templates): don't adopt a default parent leaking through ~template

Elian Dorane075299ca158
diff --git a/apps/client/src/services/note_create.spec.ts b/apps/client/src/services/note_create.spec.ts
index 248ad0c9b6..e2563e23da 100644
--- a/apps/client/src/services/note_create.spec.ts
+++ b/apps/client/src/services/note_create.spec.ts
@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import { buildNote } from "../test/easy-froca";
import froca from "./froca";
+import noteAttributeCache from "./note_attribute_cache.js";
import server from "./server.js";
import ws from "./ws.js";
@@ -344,6 +345,7 @@ describe("chooseNoteType", () => {
{ id: "inheriting-tpl", title: "Inheriting", "#template": "" },
{ id: "overriding-tpl", title: "Overriding", "#template": "", "~template:newNoteDefaultParent": "people" }
] },
+ { id: "derived-tpl", title: "Derived", "#template": "", "~template": "person-tpl" },
{ id: "plain-tpl", title: "Plain", "#template": "" }
]
});
@@ -367,6 +369,14 @@ describe("chooseNoteType", () => {
.resolves.toMatchObject({ notePath: "root/people", cloneToNoteIds: ["advisors"] });
});
+ it("ignores a non-inheritable destination flowing through ~template from the base template", 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();
+ await expect(choose({ success: true, noteType: "text", templateNoteId: "derived-tpl" }))
+ .resolves.toEqual({ success: true, noteType: "text", templateNoteId: "derived-tpl" });
+ });
+
it("uses an inherited default parent, unless the template owns one, which replaces it", async () => {
await expect(choose({ success: true, noteType: "text", templateNoteId: "inheriting-tpl" }))
.resolves.toMatchObject({ notePath: "root/advisors", cloneToNoteIds: [] });
diff --git a/apps/client/src/services/note_create.ts b/apps/client/src/services/note_create.ts
index 7d3dfe3e06..3b277e84a9 100644
--- a/apps/client/src/services/note_create.ts
+++ b/apps/client/src/services/note_create.ts
@@ -170,6 +170,11 @@ async function createNoteWithTypePrompt(parentNotePath: string, options: CreateN
* called, since all branches of a note are equal. Owned relations replace inherited ones, so a
* 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.
*/
async function getTemplateDefaultParents(templateNoteId: string) {
const templateNote = await froca.getNote(templateNoteId);
@@ -180,7 +185,7 @@ async function getTemplateDefaultParents(templateNoteId: string) {
const ownedRelations = templateNote.getOwnedRelations("template:newNoteDefaultParent");
const relations = ownedRelations.length
? ownedRelations
- : templateNote.getRelations("template:newNoteDefaultParent");
+ : templateNote.getRelations("template:newNoteDefaultParent").filter((attr) => attr.isInheritable);
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 b3e463c548..e6e1baf212 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,7 +68,9 @@
as&nbsp;<a class="reference-link" href="#root/_help_IakOLONlIfGI">clones</a>.
The relation can also be inherited — for example from a folder holding several
templates — in which case a relation owned by the template itself replaces
- the inherited one.</p>
+ 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>
<p>The default parent is used whenever a note is created from the template
through the <em>Choose note type</em> dialog — for example via <code spellcheck="false">@</code>-completion
in a&nbsp;<a class="reference-link" href="#root/_help_iPIMuisry3hd">Text</a>&nbsp;note,
diff --git a/docs/User Guide/User Guide/Advanced Usage/Templates.md b/docs/User Guide/User Guide/Advanced Usage/Templates.md
index 517d227094..1bae7d2609 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">clones</a>. The relation can also be inherited — for example from a folder holding several templates — in which case a relation owned by the template itself replaces the inherited one.
+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">clones</a>. The relation can also be inherited — for example 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.
The default parent is used whenever a note is created from the template through the _Choose note type_ dialog — for example via `@`-completion in a <a class="reference-link" href="../Note%20Types/Text.md">Text</a> note, or the _Create and link child note_ option of the note search — and no other location is picked in that dialog. Creating an instance note from the <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Note%20Tree.md">Note Tree</a> context menu (_Insert child note_, _Insert note after_) still creates it at the chosen place.