Skip to content

Build your personal knowledge base with Trilium Notes

fix(notes): don't let child:template override an explicitly chosen note type (closes #3015)

Elian Doran1666c5ebdf98
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 62c78cbb58..2cbee3650a 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
@@ -57,16 +57,37 @@
will not automatically apply the new template to the grandchild notes.
Only the structure present at the time of note creation is considered.</p>
</aside>
+<h2>Regarding note types</h2>
+<p>By default, newly created notes are&nbsp;<a class="reference-link" href="#root/pOsGYCXsbNQG/KSZ04uQ2D1St/_help_iPIMuisry3hd">Text</a>&nbsp;notes.
+ If a parent note defines a <code spellcheck="false">child:template</code> pointing
+ to a template of a different type (e.g. a Code note), the behavior depends
+ on how the new note is created:</p>
+<ul>
+ <li>If no note type is explicitly chosen (e.g. the + button in the&nbsp;
+ <a
+ class="reference-link" href="#root/pOsGYCXsbNQG/gh7bpGYxajRS/Vc8PjrjAGuOp/_help_oPVyFC7WL2Lp">Note Tree</a>), the template is applied and the new note takes the type
+ and content of the template.</li>
+ <li>If a note type is explicitly selected (e.g.&nbsp;<a class="reference-link"
+ href="#root/pOsGYCXsbNQG/gh7bpGYxajRS/Vc8PjrjAGuOp/_help_oPVyFC7WL2Lp">Note Tree</a>&nbsp;→ <em>Insert note after</em> / <em>Insert child note</em>):
+ <ul>
+ <li>If the selected type matches the template's type, the template is applied.</li>
+ <li>If the selected type differs, the template is disregarded entirely — the
+ new note is an empty note of the selected type.</li>
+ </ul>
+ </li>
+</ul>
+<p>Selecting a specific template from the creation menu always takes precedence
+ over <code spellcheck="false">child:template</code>.</p>
<h2>Additional Notes</h2>
<p>From a visual perspective, templates can define <code spellcheck="false">#iconClass</code> and
<code
spellcheck="false">#cssClass</code>attributes, allowing all instance notes (e.g., books)
to display a specific icon and CSS style.</p>
-<p>Explore the concept further in the <a href="#root/_help_wX4HbRucYSDD">demo notes</a>,
- including examples like the <a href="#root/_help_iRwzGnHPzonm">Relation Map</a>,
+<p>Explore the concept further in the&nbsp;<a class="reference-link" href="#root/pOsGYCXsbNQG/tC7s2alapj8V/wX4HbRucYSDD/_help_6tZeKvSHEUiB">Demo Notes</a>,
+ including examples like the&nbsp;<a class="reference-link" href="#root/pOsGYCXsbNQG/KSZ04uQ2D1St/_help_iRwzGnHPzonm">Relation Map</a>,&nbsp;
<a
- href="#root/_help_xYjQUYhpbUEW">Task Manager</a>, and <a href="#root/_help_l0tKav7yLHGF">Day Notes</a>.</p>
-<p>Additionally, see <a href="#root/_help_47ZrP6FNuoG8">default note title</a> for
+ class="reference-link" href="#root/pOsGYCXsbNQG/tC7s2alapj8V/5668rwcirq1t/_help_xYjQUYhpbUEW">Task Manager</a>, and&nbsp;<a class="reference-link" href="#root/pOsGYCXsbNQG/tC7s2alapj8V/5668rwcirq1t/_help_l0tKav7yLHGF">Day Notes</a>.</p>
+<p>Additionally, see&nbsp;<a class="reference-link" href="#root/pOsGYCXsbNQG/tC7s2alapj8V/_help_47ZrP6FNuoG8">Default Note Title</a>&nbsp;for
creating title templates. Note templates and title templates can be combined
by creating a <code spellcheck="false">#titleTemplate</code> for a template
note.</p>
\ No newline at end of file
diff --git a/docs/User Guide/!!!meta.json b/docs/User Guide/!!!meta.json
index dce6a2e547..f408286ab2 100644
--- a/docs/User Guide/!!!meta.json
+++ b/docs/User Guide/!!!meta.json
@@ -15741,54 +15741,68 @@
"isInheritable": false,
"position": 30
},
+ {
+ "type": "label",
+ "name": "shareAlias",
+ "value": "template",
+ "isInheritable": false,
+ "position": 30
+ },
+ {
+ "type": "label",
+ "name": "iconClass",
+ "value": "bx bx-copy",
+ "isInheritable": false,
+ "position": 110
+ },
{
"type": "relation",
"name": "internalLink",
- "value": "wX4HbRucYSDD",
+ "value": "47ZrP6FNuoG8",
"isInheritable": false,
- "position": 40
+ "position": 120
},
{
"type": "relation",
"name": "internalLink",
- "value": "iRwzGnHPzonm",
+ "value": "6tZeKvSHEUiB",
"isInheritable": false,
- "position": 50
+ "position": 130
},
{
"type": "relation",
"name": "internalLink",
- "value": "xYjQUYhpbUEW",
+ "value": "iRwzGnHPzonm",
"isInheritable": false,
- "position": 60
+ "position": 140
},
{
"type": "relation",
"name": "internalLink",
- "value": "l0tKav7yLHGF",
+ "value": "xYjQUYhpbUEW",
"isInheritable": false,
- "position": 70
+ "position": 150
},
{
"type": "relation",
"name": "internalLink",
- "value": "47ZrP6FNuoG8",
+ "value": "l0tKav7yLHGF",
"isInheritable": false,
- "position": 80
+ "position": 160
},
{
- "type": "label",
- "name": "shareAlias",
- "value": "template",
+ "type": "relation",
+ "name": "internalLink",
+ "value": "iPIMuisry3hd",
"isInheritable": false,
- "position": 30
+ "position": 170
},
{
- "type": "label",
- "name": "iconClass",
- "value": "bx bx-copy",
+ "type": "relation",
+ "name": "internalLink",
+ "value": "oPVyFC7WL2Lp",
"isInheritable": false,
- "position": 110
+ "position": 180
}
],
"format": "markdown",
diff --git a/docs/User Guide/User Guide/Advanced Usage/Templates.md b/docs/User Guide/User Guide/Advanced Usage/Templates.md
index ced19c67ad..a34dccfabe 100644
--- a/docs/User Guide/User Guide/Advanced Usage/Templates.md
+++ b/docs/User Guide/User Guide/Advanced Usage/Templates.md
@@ -33,10 +33,21 @@ To specify a template for child notes, you can use a `~child:template` relation
> Changing the template hierarchy after the parent note is created will not retroactively apply to newly created child notes.
> For example, if you initially use `~child:template` and later switch to `~child:child:template`, it will not automatically apply the new template to the grandchild notes. Only the structure present at the time of note creation is considered.
+## Regarding note types
+
+By default, newly created notes are <a class="reference-link" href="../Note%20Types/Text.md">Text</a> notes. If a parent note defines a `child:template` pointing to a template of a different type (e.g. a Code note), the behavior depends on how the new note is created:
+
+* If no note type is explicitly chosen (e.g. the + button in the <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Note%20Tree.md">Note Tree</a>), the template is applied and the new note takes the type and content of the template.
+* If a note type is explicitly selected (e.g. <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Note%20Tree.md">Note Tree</a> → _Insert note after_ / _Insert child note_):
+ * If the selected type matches the template's type, the template is applied.
+ * If the selected type differs, the template is disregarded entirely — the new note is an empty note of the selected type.
+
+Selecting a specific template from the creation menu always takes precedence over `child:template`.
+
## Additional Notes
From a visual perspective, templates can define `#iconClass` and `#cssClass` attributes, allowing all instance notes (e.g., books) to display a specific icon and CSS style.
-Explore the concept further in the [demo notes](Database.md), including examples like the [Relation Map](../Note%20Types/Relation%20Map.md), [Task Manager](Advanced%20Showcases/Task%20Manager.md), and [Day Notes](Advanced%20Showcases/Day%20Notes.md).
+Explore the concept further in the <a class="reference-link" href="Database/Demo%20Notes.md">Demo Notes</a>, including examples like the <a class="reference-link" href="../Note%20Types/Relation%20Map.md">Relation Map</a>, <a class="reference-link" href="Advanced%20Showcases/Task%20Manager.md">Task Manager</a>, and <a class="reference-link" href="Advanced%20Showcases/Day%20Notes.md">Day Notes</a>.
-Additionally, see [default note title](Default%20Note%20Title.md) for creating title templates. Note templates and title templates can be combined by creating a `#titleTemplate` for a template note.
\ No newline at end of file
+Additionally, see <a class="reference-link" href="Default%20Note%20Title.md">Default Note Title</a> for creating title templates. Note templates and title templates can be combined by creating a `#titleTemplate` for a template note.
\ No newline at end of file
diff --git a/packages/trilium-core/src/services/notes.spec.ts b/packages/trilium-core/src/services/notes.spec.ts
index ce2421132a..36f5c9c935 100644
--- a/packages/trilium-core/src/services/notes.spec.ts
+++ b/packages/trilium-core/src/services/notes.spec.ts
@@ -125,6 +125,69 @@ describe("notes service (real DB)", () => {
expect(note.mime).toBe("text/special");
expect(note.getRelationValue("template")).toBe(template.note.noteId);
});
+
+ it("inherits the parent's child:template when the new note's type matches the template's", () => {
+ const template = createNote("root", { title: "spec-child-tmpl-match", content: "<p>day template</p>" });
+ const parent = createNote("root", {
+ title: "spec-child-tmpl-parent-match",
+ attributes: [{ type: "relation", name: "child:template", value: template.note.noteId }]
+ });
+
+ const { note } = createNote(parent.note.noteId, { title: "spec-child-tmpl-text", content: "" });
+
+ expect(note.getRelationValue("template")).toBe(template.note.noteId);
+ expect(note.type).toBe("text");
+ expect(note.getContent()).toBe("<p>day template</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", {
+ title: "spec-child-tmpl-parent-mismatch",
+ attributes: [
+ { type: "relation", name: "child:template", value: template.note.noteId },
+ { type: "label", name: "child:myLabel", value: "v1" }
+ ]
+ });
+
+ const { note } = createNote(parent.note.noteId, { title: "spec-child-tmpl-code", content: "", type: "code" });
+
+ // the explicitly chosen type wins: no template relation, no content/type override
+ expect(note.getRelationValue("template")).toBeNull();
+ expect(note.type).toBe("code");
+ expect(note.mime).toBe("text/plain");
+ expect(note.getContent()).toBe("");
+ // other child: attributes are still inherited
+ expect(note.getLabelValue("myLabel")).toBe("v1");
+ });
+
+ it("applies a mismatched child:template when no type was explicitly chosen (+ button)", () => {
+ const template = createNote("root", {
+ title: "spec-child-tmpl-plus",
+ type: "code",
+ mime: "text/x-python",
+ content: "print('hi')"
+ });
+ const parent = createNote("root", {
+ title: "spec-child-tmpl-parent-plus",
+ attributes: [{ type: "relation", name: "child:template", value: template.note.noteId }]
+ });
+
+ // the + button sends no type at all; the server derives one from the parent,
+ // which must not count as an explicit user choice
+ const { note } = getContext().init(() =>
+ noteService.createNewNoteWithTarget("into", undefined, {
+ parentNoteId: parent.note.noteId,
+ title: "spec-child-tmpl-untyped",
+ content: ""
+ })
+ );
+
+ expect(note.getRelationValue("template")).toBe(template.note.noteId);
+ expect(note.type).toBe("code");
+ expect(note.mime).toBe("text/x-python");
+ expect(note.getContent()).toBe("print('hi')");
+ });
});
describe("createNewNote logging", () => {
diff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.ts
index 3efbf5d21a..0ea0d27b17 100644
--- a/packages/trilium-core/src/services/notes.ts
+++ b/packages/trilium-core/src/services/notes.ts
@@ -61,6 +61,12 @@ export interface NoteParams {
notePosition?: number;
dateCreated?: string;
utcDateCreated?: string;
+ /**
+ * Set internally when the client requested no `type` and it was derived from the parent note instead.
+ * In that case a `child:template` of a different type is still applied (and converts the note), whereas
+ * an explicitly chosen type wins over such a template (#3015).
+ */
+ isTypeDefaulted?: boolean;
ignoreForbiddenParents?: boolean;
target?: "into";
/** Attributes to be set on the note. These are set atomically on note creation, so entity changes are not sent for attributes defined here. */
@@ -100,16 +106,24 @@ function deriveMime(type: string, mime?: string) {
return noteTypesService.getDefaultMimeForNoteType(type);
}
-function copyChildAttributes(parentNote: BNote, childNote: BNote) {
+function copyChildAttributes(parentNote: BNote, childNote: BNote, isTypeDefaulted = false) {
for (const attr of parentNote.getAttributes()) {
if (attr.name.startsWith("child:")) {
const name = attr.name.substring(6);
- const hasAlreadyTemplate = childNote.hasRelation("template");
- if (hasAlreadyTemplate && attr.type === "relation" && name === "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
- continue;
+ 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
+ continue;
+ }
+
+ const templateNote = becca.getNote(attr.value);
+ if (!isTypeDefaulted && templateNote && templateNote.type !== childNote.type) {
+ // the explicitly chosen note type wins over the default template, which would otherwise
+ // convert the note and fill it with content of a different type (#3015)
+ continue;
+ }
}
new BAttribute({
@@ -306,7 +320,7 @@ function createNewNote(params: NoteParams): {
// no special handling for ~inherit since it doesn't matter if it's assigned with the note creation or later
}
- copyChildAttributes(parentNote, note);
+ copyChildAttributes(parentNote, note, params.isTypeDefaulted);
eventService.emit(eventService.ENTITY_CREATED, { entityName: "notes", entity: note });
eventService.emit(eventService.ENTITY_CHANGED, { entityName: "notes", entity: note });
@@ -330,15 +344,19 @@ function createNewNote(params: NoteParams): {
});
}
-function createNewNoteWithTarget(target: "into" | "after" | "before", targetBranchId: string | undefined, params: NoteParams) {
- if (!params.type) {
- const parentNote = becca.notes[params.parentNoteId];
+function createNewNoteWithTarget(target: "into" | "after" | "before", targetBranchId: string | undefined, rawParams: Omit<NoteParams, "type"> & { type?: NoteType }) {
+ if (!rawParams.type) {
+ const parentNote = becca.notes[rawParams.parentNoteId];
// code note type can be inherited, otherwise "text" is the default
- params.type = parentNote.type === "code" ? "code" : "text";
- params.mime = parentNote.type === "code" ? parentNote.mime : "text/html";
+ rawParams.type = parentNote.type === "code" ? "code" : "text";
+ rawParams.mime = parentNote.type === "code" ? parentNote.mime : "text/html";
+ // the type is not an explicit user choice, so a child:template of another type may still convert the note
+ rawParams.isTypeDefaulted = true;
}
+ const params = rawParams as NoteParams;
+
if (target === "into") {
return createNewNote(params);
} else if (target === "after" && targetBranchId) {