Skip to content

Build your personal knowledge base with Trilium Notes

fix(ribbon): show which language a code note is in the note type dropdown

Elian Doran4a5ad71b6fad
diff --git a/apps/client/src/services/note_types.spec.ts b/apps/client/src/services/note_types.spec.ts
index 00b343c7c8..0f31cbe027 100644
--- a/apps/client/src/services/note_types.spec.ts
+++ b/apps/client/src/services/note_types.spec.ts
@@ -17,7 +17,7 @@ vi.mock("./experimental_features.js", () => ({
}));
import { isExperimentalFeatureEnabled } from "./experimental_features.js";
-import noteTypesService, { isCurrentNoteType } from "./note_types";
+import noteTypesService, { isCurrentNoteType, selectableNoteTypes } from "./note_types";
const llmFlag = vi.mocked(isExperimentalFeatureEnabled);
@@ -436,3 +436,32 @@ describe("isCurrentNoteType", () => {
expect(isCurrentNoteType(CODE, null)).toBe(false);
});
});
+
+describe("selectableNoteTypes", () => {
+ // The suite's other resets live inside their own describes, so pin the flag here rather than
+ // inheriting whatever the last test left behind.
+ beforeEach(() => llmFlag.mockReturnValue(false));
+
+ it("drops Markdown only where the MIME list already offers it", () => {
+ const markdownEntry = (withMimeList: boolean) =>
+ selectableNoteTypes(withMimeList).filter((nt) => nt.mime === "text/x-markdown");
+
+ // Beside the MIME list the code entries are only its heading, so one heading is left.
+ expect(markdownEntry(true)).toHaveLength(0);
+ expect(selectableNoteTypes(true).filter((nt) => nt.type === "code")).toHaveLength(1);
+
+ // On its own the menu is the only way to reach Markdown, so it stays.
+ expect(markdownEntry(false)).toHaveLength(1);
+ });
+
+ it("leaves out what cannot be created either way", () => {
+ for (const withMimeList of [ true, false ]) {
+ const types = selectableNoteTypes(withMimeList);
+ expect(types.some((nt) => nt.reserved)).toBe(false);
+ expect(types.some((nt) => nt.static)).toBe(false);
+ // The chat type is behind an experimental flag, mocked off for this suite.
+ expect(types.some((nt) => nt.type === "llmChat")).toBe(false);
+ expect(types.some((nt) => nt.type === "text")).toBe(true);
+ }
+ });
+});
diff --git a/apps/client/src/services/note_types.ts b/apps/client/src/services/note_types.ts
index f8f53d9a71..ff7a661c56 100644
--- a/apps/client/src/services/note_types.ts
+++ b/apps/client/src/services/note_types.ts
@@ -80,6 +80,19 @@ export function isCurrentNoteType(entry: Pick<NoteTypeMapping, "type" | "mime">,
return note.isMarkdown() === (entry.mime === MARKDOWN_NOTE_TYPE_MIME);
}
+/**
+ * The entries a note type menu offers, leaving out what the reader cannot create.
+ *
+ * `withMimeList` says the caller also renders the code MIME list, which already offers Markdown
+ * and every other language — so the Markdown entry is dropped and the code entries are left as
+ * that list's heading.
+ */
+export function selectableNoteTypes(withMimeList: boolean) {
+ return NOTE_TYPES.filter((nt) => !nt.reserved && !nt.static
+ && (nt.type !== "llmChat" || isExperimentalFeatureEnabled("llm"))
+ && (!withMimeList || nt.mime !== MARKDOWN_NOTE_TYPE_MIME));
+}
+
/**
* One thing a new note can be made from: a blank note type, or a note carrying `#template`.
*
diff --git a/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx b/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx
index f612da5e74..c43c56481b 100644
--- a/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx
+++ b/apps/client/src/widgets/ribbon/BasicPropertiesTab.tsx
@@ -11,7 +11,7 @@ import { isExperimentalFeatureEnabled } from "../../services/experimental_featur
import { getAvailableLocales, getLocaleById, t } from "../../services/i18n";
import { resolveContentLanguage } from "../../utils/formatters";
import mime_types from "../../services/mime_types";
-import { isCurrentNoteType, NOTE_TYPES } from "../../services/note_types";
+import { isCurrentNoteType, NOTE_TYPES, selectableNoteTypes } from "../../services/note_types";
import protected_session from "../../services/protected_session";
import server from "../../services/server";
import sync from "../../services/sync";
@@ -76,7 +76,7 @@ export function NoteTypeDropdownContent({ currentNoteType, currentNoteMime, note
noCodeNotes?: boolean;
}) {
const { enabledMimeTypes } = useMimeTypes();
- const noteTypes = useMemo(() => NOTE_TYPES.filter((nt) => !nt.reserved && !nt.static && (nt.type !== "llmChat" || isExperimentalFeatureEnabled("llm"))), []);
+ const noteTypes = useMemo(() => selectableNoteTypes(!noCodeNotes), [ noCodeNotes ]);
const changeNoteType = useCallback(async (type: NoteType, mime?: string) => {
if (!note || (type === currentNoteType && mime === currentNoteMime)) {
return;
@@ -111,11 +111,10 @@ export function NoteTypeDropdownContent({ currentNoteType, currentNoteMime, note
});
}
- const checked = isCurrentNoteType({ type, mime }, note);
if (noCodeNotes || type !== "code") {
return (
<FormListItem
- checked={checked}
+ checked={isCurrentNoteType({ type, mime }, note)}
badges={badges}
onClick={() => changeNoteType(type, mime)}
>{title}</FormListItem>
@@ -124,17 +123,14 @@ export function NoteTypeDropdownContent({ currentNoteType, currentNoteMime, note
return (
<>
<FormDropdownDivider />
- <FormListItem
- checked={checked}
- disabled
- >
+ <FormListItem disabled>
<strong>{title}</strong>
</FormListItem>
</>
);
})}
- {!noCodeNotes && <NoteTypeCodeNoteList mimeTypes={enabledMimeTypes} changeNoteType={changeNoteType} setModalShown={setModalShown} />}
+ {!noCodeNotes && <NoteTypeCodeNoteList currentMimeType={currentNoteMime ?? undefined} mimeTypes={enabledMimeTypes} changeNoteType={changeNoteType} setModalShown={setModalShown} />}
</>
);
}