Skip to content

Build your personal knowledge base with Trilium Notes

feat(tree): offer the enabled code languages under Code in the insert menus

Elian Doraneeb778a0306c
diff --git a/apps/client/src/services/note_types.spec.ts b/apps/client/src/services/note_types.spec.ts
index 7fa47c9f09..1194946deb 100644
--- a/apps/client/src/services/note_types.spec.ts
+++ b/apps/client/src/services/note_types.spec.ts
@@ -1,7 +1,9 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
+import appContext from "../components/app_context";
import { buildNote } from "../test/easy-froca";
import froca from "./froca";
+import options from "./options";
import server from "./server.js";
// i18next is not initialized in the test env, so the real `t` returns undefined.
@@ -477,6 +479,43 @@ describe("new template badges", () => {
restore();
}
});
+
+ it("offers the enabled code languages in a submenu of Code, as they stand when the menu opens", async () => {
+ withTemplates();
+ const restore = withTemplatesRoot([]);
+ const triggerCommand = vi.spyOn(appContext, "triggerCommand").mockImplementation(async () => undefined);
+ const original = options.get("codeNotesMimeTypes");
+ try {
+ const data = await noteTypesService.loadNoteTypeData();
+ const codeRow = (items: any[]) => items.find((i) => i.type === "code" && i.mime === "text/plain");
+
+ options.set("codeNotesMimeTypes", JSON.stringify([ "text/x-python", "text/x-markdown" ]));
+ const code = codeRow(noteTypesService.buildNoteTypeItems(data, "insertChildNote" as never));
+ // The row still makes a plain text code note of its own.
+ expect(code.command).toBe("insertChildNote");
+ const languages = code.items.filter((i: any) => i.mime);
+ // Plain text is always enabled; Markdown has an entry of its own.
+ expect(languages.map((i: any) => i.mime)).toEqual([ "text/plain", "text/x-python" ]);
+ expect(languages.every((i: any) => i.type === "code" && i.command === "insertChildNote")).toBe(true);
+
+ const configure = code.items.at(-1);
+ expect(configure.command).toBeUndefined();
+ configure.handler();
+ expect(triggerCommand).toHaveBeenCalledWith("showOptions", { section: "_optionsCodeNotes" });
+
+ // A language enabled since is offered the next time the menu opens.
+ options.set("codeNotesMimeTypes", JSON.stringify([ "text/x-python", "text/apl" ]));
+ const again = codeRow(noteTypesService.buildNoteTypeItems(data, "insertChildNote" as never));
+ expect(again.items.filter((i: any) => i.mime).map((i: any) => i.mime)).toContain("text/apl");
+
+ // The chooser's flat list cannot open a submenu.
+ expect(codeRow(await noteTypesService.getNoteTypeItems()).items).toBeUndefined();
+ } finally {
+ options.set("codeNotesMimeTypes", original);
+ triggerCommand.mockRestore();
+ restore();
+ }
+ });
});
describe("isCurrentNoteType", () => {
diff --git a/apps/client/src/services/note_types.ts b/apps/client/src/services/note_types.ts
index 128e5e13d9..fcf423c2cc 100644
--- a/apps/client/src/services/note_types.ts
+++ b/apps/client/src/services/note_types.ts
@@ -3,6 +3,7 @@ import {
type TemplatesResponse
} from "@triliumnext/commons";
+import appContext from "../components/app_context.js";
import type FNote from "../entities/fnote.js";
import type { NoteType } from "../entities/fnote.js";
import type { MenuCommandItem, MenuItem, MenuItemBadge, MenuSeparatorItem } from "../menus/context_menu.js";
@@ -10,7 +11,9 @@ import type { TreeCommandNames } from "../menus/tree_context_menu.js";
import { isExperimentalFeatureEnabled } from "./experimental_features.js";
import froca from "./froca.js";
import { t } from "./i18n.js";
+import mimeTypes from "./mime_types.js";
import server from "./server.js";
+import { escapeHtml } from "./utils.js";
export interface NoteTypeMapping {
type: NoteType;
@@ -230,6 +233,9 @@ function buildNoteTypeItems(data: NoteTypeData, command?: TreeCommandNames, layo
const items: MenuItem<TreeCommandNames>[] = [];
for (const blankType of getBlankNoteTypes(command)) {
+ if (layout === "menu" && "mime" in blankType && blankType.type === "code" && blankType.mime === "text/plain") {
+ blankType.items = getCodeLanguageItems(command);
+ }
items.push(blankType);
if ("type" in blankType && blankType.type === "llmChat") items.push(...builtIn("aiQuickAction"));
}
@@ -279,6 +285,28 @@ function getBlankNoteTypes(command?: TreeCommandNames): MenuItem<TreeCommandName
});
}
+/**
+ * The code languages enabled in the code note options, Markdown aside since it has an entry of its
+ * own, then a row to configure them. The options are read again each time, so a language enabled
+ * since the last menu is offered.
+ */
+function getCodeLanguageItems(command?: TreeCommandNames): MenuItem<TreeCommandNames>[] {
+ mimeTypes.loadMimeTypes();
+ const languages = mimeTypes.getMimeTypes()
+ .filter((mimeType) => mimeType.enabled && mimeType.mime !== MARKDOWN_NOTE_TYPE_MIME)
+ .map<MenuItem<TreeCommandNames>>(({ title, mime }) => ({ title: escapeHtml(title), command, type: "code", mime }));
+
+ return [
+ ...languages,
+ SEPARATOR,
+ {
+ title: t("basic_properties.configure_code_notes"),
+ uiIcon: "bx bx-cog",
+ handler: () => void appContext.triggerCommand("showOptions", { section: "_optionsCodeNotes" })
+ }
+ ];
+}
+
function getUserTemplates(command: TreeCommandNames | undefined, templateNotes: FNote[], newTemplates: Set<string>) {
if (templateNotes.length === 0) {
return [];
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Code.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Code.html
index de2a0f92ea..46c74ac1e5 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Code.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Code.html
@@ -19,6 +19,16 @@
<p>
<img src="Code_image.png">
</p>
+<h2>Creating a code note</h2>
+<p>In the <a href="#root/_help_oPVyFC7WL2Lp">Note Tree</a>, right-click a
+ note and select <em>Insert child note</em> or <em>Insert note after</em>,
+ then:</p>
+<ul>
+ <li>To create a plain text code note, select <em>Code</em>.</li>
+ <li>To create a code note in a given language, hover <em>Code</em> and select
+ the language. The list shows the languages enabled in the options (see
+ below), and ends with <em>Configure code notes...</em>, which opens them.</li>
+</ul>
<h2>Adjusting the language of a code note</h2>
<p>In the&nbsp;<a href="#root/_help_BlN9DFI679QC">Ribbon</a>, look for the <em>Note type</em> selector
and click it to reveal the possible note types. Inside of it there will
diff --git a/docs/User Guide/User Guide/Note Types/Code.md b/docs/User Guide/User Guide/Note Types/Code.md
index 0ada2af7b8..4fb53d90bd 100644
--- a/docs/User Guide/User Guide/Note Types/Code.md
+++ b/docs/User Guide/User Guide/Note Types/Code.md
@@ -12,6 +12,13 @@ For shorter snippets of code that can be embedded in [Text](Text.md) notes, se
![](Code_image.png)
+## Creating a code note
+
+In the [Note Tree](../Basic%20Concepts%20and%20Features/UI%20Elements/Note%20Tree.md), right-click a note and select _Insert child note_ or _Insert note after_, then:
+
+* To create a plain text code note, select _Code_.
+* To create a code note in a given language, hover _Code_ and select the language. The list shows the languages enabled in the options (see below), and ends with _Configure code notes..._, which opens them.
+
## Adjusting the language of a code note
In the [Ribbon](../Basic%20Concepts%20and%20Features/UI%20Elements/Ribbon.md), look for the _Note type_ selector and click it to reveal the possible note types. Inside of it there will be a section called _Code_, select any one of the languages.