Skip to content

Build your personal knowledge base with Trilium Notes

client/snippets: define text/markdown/code snippet templates

Adorian Doran94dd84070cc7
diff --git a/apps/server/src/assets/translations/en/server.json b/apps/server/src/assets/translations/en/server.json
index f7062a6bed..fb5e9773a6 100644
--- a/apps/server/src/assets/translations/en/server.json
+++ b/apps/server/src/assets/translations/en/server.json
@@ -393,7 +393,8 @@
},
"hidden_subtree_templates": {
"text-snippet": "Text Snippet",
- "snippet": "Snippet",
+ "markdown-snippet": "Markdown Snippet",
+ "code-snippet": "Code Snippet",
"description": "Description",
"list-view": "List View",
"grid-view": "Grid View",
diff --git a/packages/commons/src/lib/hidden_subtree.ts b/packages/commons/src/lib/hidden_subtree.ts
index 03c137653f..0998ef4b15 100644
--- a/packages/commons/src/lib/hidden_subtree.ts
+++ b/packages/commons/src/lib/hidden_subtree.ts
@@ -22,6 +22,11 @@ export interface HiddenSubtreeItem {
id: string;
title: string;
type: LauncherNoteType;
+ /**
+ * The MIME type to use for this item (e.g. `text/x-markdown`). Only relevant for code notes;
+ * if omitted, the default MIME for the type is used.
+ */
+ mime?: string;
/**
* The icon to use for this item, in the format "bx-icon-name" (e.g., `bx-file-blank`), *without* the leading `bx `.
*/
diff --git a/packages/trilium-core/src/services/hidden_subtree.ts b/packages/trilium-core/src/services/hidden_subtree.ts
index eaa0812c70..ce06e2f580 100644
--- a/packages/trilium-core/src/services/hidden_subtree.ts
+++ b/packages/trilium-core/src/services/hidden_subtree.ts
@@ -429,6 +429,7 @@ function checkHiddenSubtreeRecursively(parentNoteId: string, item: HiddenSubtree
noteId: item.id,
title: item.title,
type: item.type,
+ mime: item.mime,
parentNoteId,
content: item.content ?? "",
ignoreForbiddenParents: true
@@ -514,6 +515,12 @@ function checkHiddenSubtreeRecursively(parentNoteId: string, item: HiddenSubtree
note.save();
}
+ if (item.mime && note.mime !== item.mime) {
+ // enforce a correct MIME type
+ note.mime = item.mime;
+ note.save();
+ }
+
if (branch) {
// in case of launchers the branch ID is not preserved and should not be relied upon - launchers which move between
// visible and available will change branch since the branch's parent-child relationship is immutable
diff --git a/packages/trilium-core/src/services/hidden_subtree_templates.spec.ts b/packages/trilium-core/src/services/hidden_subtree_templates.spec.ts
index d3bd0720a1..fc8be179a7 100644
--- a/packages/trilium-core/src/services/hidden_subtree_templates.spec.ts
+++ b/packages/trilium-core/src/services/hidden_subtree_templates.spec.ts
@@ -37,6 +37,8 @@ describe("buildHiddenSubtreeTemplates", () => {
expect(ids).toEqual([
"_template_text_snippet",
+ "_template_markdown_snippet",
+ "_template_code_snippet",
"_template_list_view",
"_template_grid_view",
"_template_calendar",
@@ -98,6 +100,29 @@ describe("buildHiddenSubtreeTemplates", () => {
expect(descriptor?.value).toContain("text");
});
+ it("configures the markdown and code snippet templates as code notes with the unified snippet label", () => {
+ const templates = buildHiddenSubtreeTemplates();
+ const expectedMimes: Record<string, string> = {
+ _template_markdown_snippet: "text/x-markdown",
+ _template_code_snippet: "text/plain"
+ };
+
+ for (const [id, mime] of Object.entries(expectedMimes)) {
+ const snippet = childById(templates, id);
+
+ expect(snippet.type, id).toBe("code");
+ expect(snippet.mime, id).toBe(mime);
+
+ const names = (snippet.attributes ?? []).map((a) => a.name);
+ expect(names, id).toContain("template");
+ expect(names, id).toContain("snippet");
+
+ const descriptor = snippet.attributes?.find((a) => a.name === "label:snippetDescription");
+ expect(descriptor?.type, id).toBe("label");
+ expect(descriptor?.value, id).toContain("promoted");
+ }
+ });
+
it("applies the shared hidden-subtree label on the templates that hide their subtree", () => {
const templates = buildHiddenSubtreeTemplates();
const hidingIds = ["_template_calendar", "_template_table", "_template_geo_map", "_template_board"];
diff --git a/packages/trilium-core/src/services/hidden_subtree_templates.ts b/packages/trilium-core/src/services/hidden_subtree_templates.ts
index 1421971334..80ced0d94b 100644
--- a/packages/trilium-core/src/services/hidden_subtree_templates.ts
+++ b/packages/trilium-core/src/services/hidden_subtree_templates.ts
@@ -35,9 +35,32 @@ export default function buildHiddenSubtreeTemplates() {
]
},
{
- id: "_template_snippet",
+ id: "_template_markdown_snippet",
type: "code",
- title: t("hidden_subtree_templates.snippet"),
+ mime: "text/x-markdown",
+ title: t("hidden_subtree_templates.markdown-snippet"),
+ icon: "bx-align-left",
+ attributes: [
+ {
+ name: "template",
+ type: "label"
+ },
+ {
+ name: "snippet",
+ type: "label"
+ },
+ {
+ name: "label:snippetDescription",
+ type: "label",
+ value: `promoted,alias=${t("hidden_subtree_templates.description")},single,text`
+ }
+ ]
+ },
+ {
+ id: "_template_code_snippet",
+ type: "code",
+ mime: "text/plain",
+ title: t("hidden_subtree_templates.code-snippet"),
icon: "bx-align-left",
attributes: [
{