Skip to content

Build your personal knowledge base with Trilium Notes

feat(notes): show each code language's icon in the language pickers

Elian Dorana9f35e69bcca
diff --git a/apps/client/src/services/note_types.spec.ts b/apps/client/src/services/note_types.spec.ts
index d1e92342c2..bb37e0c64f 100644
--- a/apps/client/src/services/note_types.spec.ts
+++ b/apps/client/src/services/note_types.spec.ts
@@ -558,6 +558,8 @@ describe("new template badges", () => {
// 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);
+ // The icon the note takes in the tree once created.
+ expect(languages.map((i: any) => i.uiIcon)).toEqual([ "bx bx-file", "bx bxl-python" ]);
const configure = code.items.at(-1);
expect(configure.command).toBeUndefined();
@@ -568,6 +570,8 @@ describe("new template badges", () => {
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");
+ // A language without an icon of its own takes that of the code note type.
+ expect(again.items.find((i: any) => i.mime === "text/apl").uiIcon).toBe("bx bx-code");
} finally {
options.set("codeNotesMimeTypes", original);
diff --git a/apps/client/src/services/note_types.ts b/apps/client/src/services/note_types.ts
index 34dcdcd04b..71892df0a2 100644
--- a/apps/client/src/services/note_types.ts
+++ b/apps/client/src/services/note_types.ts
@@ -1,5 +1,5 @@
import {
- buildNoteTypeId, buildTemplateId, type NoteType as CommonNoteType, type NoteTypeId,
+ buildNoteTypeId, buildTemplateId, getCodeLanguageIcon, type NoteType as CommonNoteType, type NoteTypeId,
type TemplatesResponse
} from "@triliumnext/commons";
@@ -311,7 +311,13 @@ function getCodeLanguageItems(command?: TreeCommandNames): MenuItem<TreeCommandN
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 }));
+ .map<MenuItem<TreeCommandNames>>((mimeType) => ({
+ title: escapeHtml(mimeType.title),
+ uiIcon: getCodeLanguageIcon(mimeType),
+ command,
+ type: "code",
+ mime: mimeType.mime
+ }));
return [
...languages,
diff --git a/apps/client/src/widgets/layout/StatusBar.tsx b/apps/client/src/widgets/layout/StatusBar.tsx
index b8dc1c0933..a29868e102 100644
--- a/apps/client/src/widgets/layout/StatusBar.tsx
+++ b/apps/client/src/widgets/layout/StatusBar.tsx
@@ -1,6 +1,6 @@
import "./StatusBar.css";
-import { Locale, NOTE_TYPE_ICONS, NoteType } from "@triliumnext/commons";
+import { getCodeLanguageIcon, Locale, NOTE_TYPE_ICONS, NoteType } from "@triliumnext/commons";
import clsx from "clsx";
import { type ComponentChildren, createPortal, RefObject } from "preact";
import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";
@@ -606,7 +606,7 @@ function CodeNoteSwitcher({ note }: StatusBarContext) {
return (noteType === "code" &&
<>
<StatusBarDropdown
- icon={correspondingMimeType?.icon ?? "bx bx-code-curly"}
+ icon={getCodeLanguageIcon(correspondingMimeType)}
text={correspondingMimeType?.title}
title={t("status_bar.code_note_switcher")}
dropdownContainerClassName="dropdown-code-note-switcher"
diff --git a/apps/client/src/widgets/ribbon/BasicPropertiesTab.spec.tsx b/apps/client/src/widgets/ribbon/BasicPropertiesTab.spec.tsx
index 2e24ad5bd2..3e8a6776b4 100644
--- a/apps/client/src/widgets/ribbon/BasicPropertiesTab.spec.tsx
+++ b/apps/client/src/widgets/ribbon/BasicPropertiesTab.spec.tsx
@@ -12,7 +12,7 @@ import { codeLanguageItems } from "./BasicPropertiesTab";
describe("codeLanguageItems", () => {
const mimeTypes = [
- { title: "Plain text", mime: "text/plain" },
+ { title: "Plain text", mime: "text/plain", icon: "bx bx-file" },
{ title: "C++ <templates>", mime: "text/x-c++src" }
] as MimeType[];
@@ -26,6 +26,8 @@ describe("codeLanguageItems", () => {
"Plain text", "C++ &lt;templates&gt;", "separator", "basic_properties.configure_code_notes"
]);
expect(items.map((item) => "checked" in item && item.checked)).toEqual([ false, true, false, false ]);
+ // A language's own icon, or the code note type's when it has none, as the note tree shows it.
+ expect(items.slice(0, 2).map((item) => "uiIcon" in item && item.uiIcon)).toEqual([ "bx bx-file", "bx bx-code" ]);
const [ plain, , , configure ] = items;
if ("kind" in plain || "kind" in configure) throw new Error("expected rows");
diff --git a/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx b/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx
index c1e095cdad..08e039e932 100644
--- a/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx
+++ b/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx
@@ -1,6 +1,6 @@
import "./BasicPropertiesTab.css";
-import { MimeType, NoteType, ToggleInParentResponse } from "@triliumnext/commons";
+import { getCodeLanguageIcon, MimeType, NoteType, ToggleInParentResponse } from "@triliumnext/commons";
import { createPortal } from "preact";
import { Dispatch, StateUpdater, useCallback, useEffect, useMemo, useState } from "preact/hooks";
@@ -148,10 +148,11 @@ interface CodeLanguageListProps {
/** The code languages to switch a note to, the current one ticked, as menu items. */
export function codeLanguageItems({ currentMimeType, mimeTypes, changeNoteType, onConfigure }: CodeLanguageListProps) {
- const items: MenuItem<unknown>[] = mimeTypes.map(({ title, mime }) => ({
- title: escapeHtml(title),
- checked: mime === currentMimeType,
- handler: () => changeNoteType("code", mime)
+ const items: MenuItem<unknown>[] = mimeTypes.map((mimeType) => ({
+ title: escapeHtml(mimeType.title),
+ uiIcon: getCodeLanguageIcon(mimeType),
+ checked: mimeType.mime === currentMimeType,
+ handler: () => changeNoteType("code", mimeType.mime)
}));
if (onConfigure) {
items.push({ kind: "separator" }, { title: t("basic_properties.configure_code_notes"), uiIcon: "bx bx-cog", handler: onConfigure });
diff --git a/packages/commons/src/lib/notes.ts b/packages/commons/src/lib/notes.ts
index 2259cd2cd4..f1119ea085 100644
--- a/packages/commons/src/lib/notes.ts
+++ b/packages/commons/src/lib/notes.ts
@@ -3,7 +3,7 @@
*/
import { isFontMimeType } from "./font_mimes.js";
-import { MIME_TYPES_DICT } from "./mime_type.js";
+import { MIME_TYPES_DICT, type MimeTypeDefinition } from "./mime_type.js";
import { NoteType } from "./rows.js";
export const NOTE_TYPE_ICONS = {
@@ -175,8 +175,7 @@ export function getNoteIcon({
}
return "bx bx-note";
} else if (type === "code") {
- const correspondingMimeType = MIME_TYPES_DICT.find(m => m.mime === mime);
- return correspondingMimeType?.icon ?? NOTE_TYPE_ICONS.code;
+ return getCodeLanguageIcon(MIME_TYPES_DICT.find(m => m.mime === mime));
} else if (type === "file") {
return getFileMimeIcon(mime);
} else if (type === "image") {
@@ -201,6 +200,14 @@ export function getMimeIcon(mime: string | undefined | null): string {
return mime.startsWith("image/") ? getImageMimeIcon(mime) : getFileMimeIcon(mime);
}
+/**
+ * The icon of a code note in `language`: the language's own, or the code note type's for a language
+ * without one or a mime `MIME_TYPES_DICT` does not know.
+ */
+export function getCodeLanguageIcon(language: Pick<MimeTypeDefinition, "icon"> | undefined): string {
+ return language?.icon ?? NOTE_TYPE_ICONS.code;
+}
+
function getFileMimeIcon(mime: string): string {
if (mime.startsWith("video/")) return "bx bx-video";
if (mime.startsWith("audio/")) return "bx bx-music";