Skip to content

Build your personal knowledge base with Trilium Notes

chore(notes): keep one list of note types

Elian Doran2f6813ac2b87
diff --git a/apps/client/src/entities/fnote.spec.ts b/apps/client/src/entities/fnote.spec.ts
index 4638ba5f3e..d022390f69 100644
--- a/apps/client/src/entities/fnote.spec.ts
+++ b/apps/client/src/entities/fnote.spec.ts
@@ -1,3 +1,4 @@
+import type { NoteType } from "@triliumnext/commons";
import { afterEach, describe, expect, it, vi } from "vitest";
import froca from "../services/froca.js";
@@ -10,7 +11,7 @@ import FAttachment from "./fattachment.js";
import FAttribute from "./fattribute.js";
import FBlob from "./fblob.js";
import FBranch from "./fbranch.js";
-import FNote, { type FNoteRow, type NoteType } from "./fnote.js";
+import FNote, { type FNoteRow } from "./fnote.js";
vi.mock("../services/search.js", () => ({
default: {
diff --git a/apps/client/src/entities/fnote.ts b/apps/client/src/entities/fnote.ts
index fb7b6906ca..e2c0f5eded 100644
--- a/apps/client/src/entities/fnote.ts
+++ b/apps/client/src/entities/fnote.ts
@@ -1,4 +1,4 @@
-import { getNoteIcon, HighlightedTokenInfo, isMarkdownMime } from "@triliumnext/commons";
+import { getNoteIcon, HighlightedTokenInfo, isMarkdownMime, type NoteType } from "@triliumnext/commons";
import cssClassManager from "../services/css_class_manager.js";
import type { Froca } from "../services/froca-interface.js";
@@ -12,13 +12,6 @@ import type { AttributeType, default as FAttribute } from "./fattribute.js";
const LABEL = "label";
const RELATION = "relation";
-/**
- * There are many different Note types, some of which are entirely opaque to the
- * end user. Those types should be used only for checking against, they are
- * not for direct use.
- */
-export type NoteType = "file" | "image" | "search" | "noteMap" | "launcher" | "doc" | "contentWidget" | "text" | "relationMap" | "render" | "canvas" | "mermaid" | "book" | "webView" | "code" | "mindMap" | "spreadsheet" | "llmChat";
-
export interface NotePathRecord {
isArchived: boolean;
isInHoistedSubTree: boolean;
diff --git a/apps/client/src/services/note_types.ts b/apps/client/src/services/note_types.ts
index 53b24eff9a..2059755914 100644
--- a/apps/client/src/services/note_types.ts
+++ b/apps/client/src/services/note_types.ts
@@ -1,11 +1,10 @@
import {
- buildNoteTypeId, buildTemplateId, getCodeLanguageIcon, type NoteType as CommonNoteType, type NoteTypeId,
+ buildNoteTypeId, buildTemplateId, getCodeLanguageIcon, type NoteType, type NoteTypeId,
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";
import type { TreeCommandNames } from "../menus/tree_context_menu.js";
import { isExperimentalFeatureEnabled } from "./experimental_features.js";
@@ -130,7 +129,7 @@ export async function getNoteTypeOptions(): Promise<NoteTypeOption[]> {
.filter((mapping) => !mapping.reserved && mapping.type !== "book")
.filter((mapping) => mapping.type !== "llmChat" || isExperimentalFeatureEnabled("llm"))
.map<NoteTypeOption>((mapping) => ({
- id: buildNoteTypeId(mapping.type as CommonNoteType, mapping.mime),
+ id: buildNoteTypeId(mapping.type, mapping.mime),
title: mapping.title,
icon: `bx ${mapping.icon ?? "bx-note"}`,
group: "type",
diff --git a/apps/client/src/widgets/collections/geomap/api.ts b/apps/client/src/widgets/collections/geomap/api.ts
index 3c7ded7906..62c033518d 100644
--- a/apps/client/src/widgets/collections/geomap/api.ts
+++ b/apps/client/src/widgets/collections/geomap/api.ts
@@ -1,6 +1,6 @@
-import { AttributeRow } from "@triliumnext/commons";
+import { AttributeRow, type NoteType } from "@triliumnext/commons";
-import FNote, { type NoteType } from "../../../entities/fnote";
+import FNote from "../../../entities/fnote";
import attributes from "../../../services/attributes";
import dialog from "../../../services/dialog";
import { t } from "../../../services/i18n";
diff --git a/apps/server/src/etapi/validators.ts b/apps/server/src/etapi/validators.ts
index 799a871e3f..c57f8ab158 100644
--- a/apps/server/src/etapi/validators.ts
+++ b/apps/server/src/etapi/validators.ts
@@ -1,6 +1,5 @@
-import { date_utils as dateUtils, note_types as noteTypeService, utils } from "@triliumnext/core";
-
-import { becca } from "@triliumnext/core";
+import { ALLOWED_NOTE_TYPES, isNoteType as isKnownNoteType } from "@triliumnext/commons";
+import { becca, date_utils as dateUtils, utils } from "@triliumnext/core";
function mandatory(obj: unknown) {
if (obj === undefined) {
@@ -79,10 +78,8 @@ function isNoteType(obj: unknown) {
return;
}
- const noteTypes = noteTypeService.getNoteTypeNames();
-
- if (typeof obj !== "string" || !noteTypes.includes(obj)) {
- return `'${obj}' is not a valid note type, allowed types are: ${noteTypes.join(", ")}`;
+ if (!isKnownNoteType(obj)) {
+ return `'${obj}' is not a valid note type, allowed types are: ${ALLOWED_NOTE_TYPES.join(", ")}`;
}
}
diff --git a/packages/commons/src/lib/note_type_ids.ts b/packages/commons/src/lib/note_type_ids.ts
index b700d7e5af..df4c34aaab 100644
--- a/packages/commons/src/lib/note_type_ids.ts
+++ b/packages/commons/src/lib/note_type_ids.ts
@@ -1,4 +1,4 @@
-import { ALLOWED_NOTE_TYPES, type NoteType } from "./rows.js";
+import { isNoteType, type NoteType } from "./rows.js";
/**
* Naming what a note is made from, as one string.
@@ -61,8 +61,8 @@ export function parseNoteTypeId(id: NoteTypeId): NoteTypeSelection | undefined {
const type = separator < 0 ? rest : rest.slice(0, separator);
const mime = separator < 0 ? "" : rest.slice(separator + 1);
- return (ALLOWED_NOTE_TYPES as readonly string[]).includes(type)
- ? { kind: "type", type: type as NoteType, mime: mime || undefined }
+ return isNoteType(type)
+ ? { kind: "type", type, mime: mime || undefined }
: undefined;
}
diff --git a/packages/commons/src/lib/rows.spec.ts b/packages/commons/src/lib/rows.spec.ts
index d961180a4d..4553721be9 100644
--- a/packages/commons/src/lib/rows.spec.ts
+++ b/packages/commons/src/lib/rows.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { ALLOWED_NOTE_TYPES, REVISION_SOURCES } from "./rows.js";
+import { ALLOWED_NOTE_TYPES, isNoteType, REVISION_SOURCES } from "./rows.js";
describe("REVISION_SOURCES", () => {
it("contains the expected revision source values in order", () => {
@@ -43,3 +43,14 @@ describe("ALLOWED_NOTE_TYPES", () => {
]);
});
});
+
+describe("isNoteType", () => {
+ it("accepts exactly the allowed note types", () => {
+ for (const type of ALLOWED_NOTE_TYPES) {
+ expect(isNoteType(type)).toBe(true);
+ }
+ for (const value of ["EVILTYPE", "html", "Text", "", undefined, null, 42, {}]) {
+ expect(isNoteType(value)).toBe(false);
+ }
+ });
+});
diff --git a/packages/commons/src/lib/rows.ts b/packages/commons/src/lib/rows.ts
index 90d2ed54f6..8e1e42240f 100644
--- a/packages/commons/src/lib/rows.ts
+++ b/packages/commons/src/lib/rows.ts
@@ -133,6 +133,10 @@ export const ALLOWED_NOTE_TYPES = [
] as const;
export type NoteType = (typeof ALLOWED_NOTE_TYPES)[number];
+export function isNoteType(value: unknown): value is NoteType {
+ return typeof value === "string" && (ALLOWED_NOTE_TYPES as readonly string[]).includes(value);
+}
+
export interface NoteRow {
noteId: string;
deleteId?: string;
diff --git a/packages/trilium-core/src/services/import/zip.ts b/packages/trilium-core/src/services/import/zip.ts
index 4426ef14f2..951f19e88b 100644
--- a/packages/trilium-core/src/services/import/zip.ts
+++ b/packages/trilium-core/src/services/import/zip.ts
@@ -1,5 +1,5 @@
import {
- ALLOWED_NOTE_TYPES, CANVAS_ATTACHMENT_MIME, getDrawingPictureTitle, getImageAttachmentTitle,
+ CANVAS_ATTACHMENT_MIME, getDrawingPictureTitle, getImageAttachmentTitle, isNoteType,
type NoteType
} from "@triliumnext/commons";
import { basename, dirname } from "../utils/path.js";
@@ -1014,8 +1014,8 @@ function resolveNoteType(type: string | undefined): NoteType {
return "book";
}
- if (type && (ALLOWED_NOTE_TYPES as readonly string[]).includes(type)) {
- return type as NoteType;
+ if (isNoteType(type)) {
+ return type;
}
return "text";
diff --git a/packages/trilium-core/src/services/note_types.ts b/packages/trilium-core/src/services/note_types.ts
index 1038bb7ef1..5aeeec7091 100644
--- a/packages/trilium-core/src/services/note_types.ts
+++ b/packages/trilium-core/src/services/note_types.ts
@@ -1,35 +1,35 @@
-const noteTypes = [
- { type: "text", defaultMime: "text/html" },
- { type: "code", defaultMime: "text/plain" },
- { type: "render", defaultMime: "" },
- { type: "file", defaultMime: "application/octet-stream" },
- { type: "image", defaultMime: "" },
- { type: "search", defaultMime: "" },
- { type: "relationMap", defaultMime: "application/json" },
- { type: "book", defaultMime: "" },
- { type: "noteMap", defaultMime: "" },
- { type: "mermaid", defaultMime: "text/vnd.mermaid" },
- { type: "canvas", defaultMime: "application/json" },
- { type: "webView", defaultMime: "" },
- { type: "launcher", defaultMime: "" },
- { type: "doc", defaultMime: "" },
- { type: "contentWidget", defaultMime: "" },
- { type: "mindMap", defaultMime: "application/json" },
- { type: "spreadsheet", defaultMime: "application/json" },
- { type: "llmChat", defaultMime: "application/json" }
-];
+import { ALLOWED_NOTE_TYPES, isNoteType, type NoteType } from "@triliumnext/commons";
-function getDefaultMimeForNoteType(typeName: string) {
- const typeRec = noteTypes.find((nt) => nt.type === typeName);
+const DEFAULT_MIMES: Record<NoteType, string> = {
+ text: "text/html",
+ code: "text/plain",
+ render: "",
+ file: "application/octet-stream",
+ image: "",
+ search: "",
+ relationMap: "application/json",
+ book: "",
+ noteMap: "",
+ mermaid: "text/vnd.mermaid",
+ canvas: "application/json",
+ webView: "",
+ launcher: "",
+ doc: "",
+ contentWidget: "",
+ mindMap: "application/json",
+ spreadsheet: "application/json",
+ llmChat: "application/json"
+};
- if (!typeRec) {
+function getDefaultMimeForNoteType(typeName: string) {
+ if (!isNoteType(typeName)) {
throw new Error(`Cannot find note type '${typeName}'`);
}
- return typeRec.defaultMime;
+ return DEFAULT_MIMES[typeName];
}
export default {
- getNoteTypeNames: () => noteTypes.map((nt) => nt.type),
+ getNoteTypeNames: (): string[] => [...ALLOWED_NOTE_TYPES],
getDefaultMimeForNoteType
};