Skip to content

Build your personal knowledge base with Trilium Notes

fix(notes): apply all of a parent's child:template relations, not just the first

Elian Dorane78fda1d2f2e
diff --git a/packages/trilium-core/src/services/notes.spec.ts b/packages/trilium-core/src/services/notes.spec.ts
index 8a76546b01..e66524f22d 100644
--- a/packages/trilium-core/src/services/notes.spec.ts
+++ b/packages/trilium-core/src/services/notes.spec.ts
@@ -141,6 +141,51 @@ describe("notes service (real DB)", () => {
expect(note.getContent()).toBe("<p>day template</p>");
});
+ it("inherits every one of the parent's child:template relations, not just one", () => {
+ const t1 = createNote("root", { title: "spec-multi-tmpl-1", content: "<p>one</p>" });
+ const t2 = createNote("root", { title: "spec-multi-tmpl-2", content: "<p>two</p>" });
+ const parent = createNote("root", {
+ title: "spec-multi-tmpl-parent",
+ attributes: [
+ { type: "relation", name: "child:template", value: t1.note.noteId },
+ { type: "relation", name: "child:template", value: t2.note.noteId }
+ ]
+ });
+
+ const { note } = createNote(parent.note.noteId, {
+ title: "spec-multi-tmpl-child",
+ content: ""
+ });
+
+ const applied = note.getRelations("template").map((r) => r.value);
+ expect(applied).toEqual([t1.note.noteId, t2.note.noteId]);
+ });
+
+ it("lets an explicitly chosen template suppress the child:template defaults", () => {
+ const chosen = createNote("root", {
+ title: "spec-multi-tmpl-chosen",
+ content: "<p>chosen</p>"
+ });
+ const t1 = createNote("root", { title: "spec-mt-def-1", content: "<p>one</p>" });
+ const t2 = createNote("root", { title: "spec-mt-def-2", content: "<p>two</p>" });
+ const parent = createNote("root", {
+ title: "spec-multi-tmpl-parent-chosen",
+ attributes: [
+ { type: "relation", name: "child:template", value: t1.note.noteId },
+ { type: "relation", name: "child:template", value: t2.note.noteId }
+ ]
+ });
+
+ const { note } = createNote(parent.note.noteId, {
+ title: "spec-multi-tmpl-child-chosen",
+ content: "",
+ templateNoteId: chosen.note.noteId
+ });
+
+ const applied = note.getRelations("template").map((r) => r.value);
+ expect(applied).toEqual([chosen.note.noteId]);
+ });
+
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.ts
index b6d55bfc9a..2e352476bb 100644
--- a/packages/trilium-core/src/services/notes.ts
+++ b/packages/trilium-core/src/services/notes.ts
@@ -108,14 +108,16 @@ 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");
+
for (const attr of parentNote.getAttributes()) {
if (attr.name.startsWith("child:")) {
const name = attr.name.substring(6);
if (attr.type === "relation" && name === "template") {
- if (childNote.hasRelation("template")) {
- // if the note already has a template, it means the template was chosen by the user explicitly
- // in the menu. In that case, we should override the default templates defined in the child: attrs
+ if (hasUserChosenTemplate) {
continue;
}