Skip to content

Build your personal knowledge base with Trilium Notes

feat(fonts): name font notes after the family, and mark them as fonts

Elian Doranbd9698496f72
diff --git a/apps/client/src/widgets/type_widgets/File.spec.tsx b/apps/client/src/widgets/type_widgets/File.spec.tsx
index c52c261588..cff407b0e3 100644
--- a/apps/client/src/widgets/type_widgets/File.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/File.spec.tsx
@@ -18,12 +18,7 @@ vi.mock("../react/ActionButton", () => ({ default: () => <button class="code-blo
// the module graph; the markers let FileTypeWidget's mime routing be asserted without rendering them.
vi.mock("../react/hooks", () => ({ useNoteBlob: vi.fn(() => null) }));
vi.mock("./file/MediaPreview", () => ({ default: () => <div class="media-preview-stub" /> }));
-// Only the rendered preview is stubbed here — `isFontMimeType`, which the routing below is
-// asserted on, stays the real one.
-vi.mock("./file/FontPreview", async () => ({
- ...await vi.importActual<typeof import("./file/FontPreview")>("./file/FontPreview"),
- default: () => <div class="font-preview-stub" />
-}));
+vi.mock("./file/FontPreview", () => ({ default: () => <div class="font-preview-stub" /> }));
vi.mock("./file/Office", () => ({ default: () => <div class="office-preview-stub" /> }));
vi.mock("./file/Pdf", () => ({ default: () => <div class="pdf-preview-stub" /> }));
diff --git a/apps/client/src/widgets/type_widgets/File.tsx b/apps/client/src/widgets/type_widgets/File.tsx
index d1c78a22d1..a9cf3e7aad 100644
--- a/apps/client/src/widgets/type_widgets/File.tsx
+++ b/apps/client/src/widgets/type_widgets/File.tsx
@@ -1,13 +1,13 @@
import "./File.css";
-import { isOfficeMimeType } from "@triliumnext/commons";
+import { isFontMimeType, isOfficeMimeType } from "@triliumnext/commons";
import { GPX_MIME } from "../../services/gpx";
import { t } from "../../services/i18n";
import Alert from "../react/Alert";
import CodeBlock from "../react/CodeBlock";
import { useNoteBlob } from "../react/hooks";
-import FontPreview, { isFontMimeType } from "./file/FontPreview";
+import FontPreview from "./file/FontPreview";
import GpxPreview from "./file/GpxPreview";
import MediaPreview from "./file/MediaPreview";
import OfficePreview from "./file/Office";
diff --git a/apps/client/src/widgets/type_widgets/file/FontPreview.spec.tsx b/apps/client/src/widgets/type_widgets/file/FontPreview.spec.tsx
index f0b6183803..bed51cb68d 100644
--- a/apps/client/src/widgets/type_widgets/file/FontPreview.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/file/FontPreview.spec.tsx
@@ -7,7 +7,7 @@ vi.mock("../../../services/i18n", () => ({
t: (key: string, opts?: Record<string, unknown>) => [ key, ...Object.values(opts ?? {}) ].join("|")
}));
-const { default: FontPreview, isFontMimeType } = await import("./FontPreview");
+const { default: FontPreview } = await import("./FontPreview");
/** The faces `document.fonts` currently holds, as the stubbed registry below sees them. */
let registeredFonts: Set<FontStub>;
@@ -153,20 +153,3 @@ describe("FontPreview", () => {
expect(fetchMock).not.toHaveBeenCalled();
});
});
-
-describe("isFontMimeType", () => {
- it("accepts the formats FontFace rasterizes, including the legacy aliases", () => {
- for (const mime of [ "font/woff2", "font/woff", "font/ttf", "font/otf", "font/sfnt", "application/font-woff", "application/x-font-ttf", "application/x-font-truetype", "application/x-font-otf", "application/x-font-opentype" ]) {
- expect(isFontMimeType(mime), mime).toBe(true);
- }
- expect(isFontMimeType("FONT/WOFF2")).toBe(true);
- });
-
- it("rejects font formats no engine loads this way, and non-fonts", () => {
- // EOT and TrueType collections keep the generic no-preview notice rather than a preview
- // that could only fail.
- for (const mime of [ "application/vnd.ms-fontobject", "font/collection", "application/octet-stream", "image/png", "" ]) {
- expect(isFontMimeType(mime), mime).toBe(false);
- }
- });
-});
diff --git a/apps/client/src/widgets/type_widgets/file/FontPreview.tsx b/apps/client/src/widgets/type_widgets/file/FontPreview.tsx
index 444b2a2be2..a8e7cb37fa 100644
--- a/apps/client/src/widgets/type_widgets/file/FontPreview.tsx
+++ b/apps/client/src/widgets/type_widgets/file/FontPreview.tsx
@@ -96,26 +96,6 @@ export default function FontPreview({ note, blob }: FontPreviewProps) {
);
}
-/** Font formats `FontFace` rasterizes. EOT and TrueType collections are left out: no engine loads
- * them this way, so a preview of one could only fail. */
-const PREVIEWABLE_MIME_TYPES = new Set([
- "font/otf",
- "font/sfnt",
- "font/ttf",
- "font/woff",
- "font/woff2",
- "application/font-woff",
- "application/font-woff2",
- "application/x-font-opentype",
- "application/x-font-otf",
- "application/x-font-truetype",
- "application/x-font-ttf"
-]);
-
-export function isFontMimeType(mime: string) {
- return PREVIEWABLE_MIME_TYPES.has(mime.toLowerCase());
-}
-
/**
* Registers the note's font with the document for as long as the preview is mounted, and returns
* the family name to render it with — `undefined` while it loads, `null` if the file is not a font
diff --git a/packages/commons/src/index.ts b/packages/commons/src/index.ts
index 42a9d03a14..632f8717f8 100644
--- a/packages/commons/src/index.ts
+++ b/packages/commons/src/index.ts
@@ -6,6 +6,7 @@ export * from "./lib/hidden_subtree.js";
export * from "./lib/rows.js";
export * from "./lib/test-utils.js";
export * from "./lib/attachment_roles.js";
+export * from "./lib/font_mimes.js";
export * from "./lib/image_mimes.js";
export * from "./lib/mime_type.js";
export * from "./lib/office.js";
diff --git a/packages/commons/src/lib/font_mimes.spec.ts b/packages/commons/src/lib/font_mimes.spec.ts
new file mode 100644
index 0000000000..4e4f4b33ed
--- /dev/null
+++ b/packages/commons/src/lib/font_mimes.spec.ts
@@ -0,0 +1,22 @@
+import { describe, expect, it } from "vitest";
+
+import { FONT_MIMES, isFontMimeType } from "./font_mimes.js";
+
+describe("isFontMimeType", () => {
+ it("accepts every listed type, whatever its casing, and nothing else", () => {
+ for (const mime of FONT_MIMES) {
+ expect(isFontMimeType(mime), mime).toBe(true);
+ }
+ expect(isFontMimeType("FONT/WOFF2")).toBe(true);
+
+ // EOT and TrueType collections are deliberately absent: no engine loads either through
+ // `FontFace`, so neither gets the font icon or a preview that could only fail.
+ for (const mime of [ "application/vnd.ms-fontobject", "font/collection", "application/octet-stream", "image/png" ]) {
+ expect(isFontMimeType(mime), mime).toBe(false);
+ }
+
+ expect(isFontMimeType(undefined)).toBe(false);
+ expect(isFontMimeType(null)).toBe(false);
+ expect(isFontMimeType("")).toBe(false);
+ });
+});
diff --git a/packages/commons/src/lib/font_mimes.ts b/packages/commons/src/lib/font_mimes.ts
new file mode 100644
index 0000000000..34be1bed2d
--- /dev/null
+++ b/packages/commons/src/lib/font_mimes.ts
@@ -0,0 +1,30 @@
+/**
+ * The media types Trilium handles as fonts, shared by everything that acts on one: the file note's
+ * preview draws them, the note icon marks them, and the imported title drops their extension.
+ *
+ * The list is what `FontFace` rasterizes. EOT and TrueType collections are deliberately absent — no
+ * engine loads either through `FontFace`, so a file of one keeps the plain file icon and the
+ * "preview not available" notice rather than an icon and a preview promising what cannot be drawn.
+ */
+export const FONT_MIMES = [
+ "font/otf",
+ "font/sfnt",
+ "font/ttf",
+ "font/woff",
+ "font/woff2",
+ // The names fonts were served under before the `font/*` tree was registered; importers and
+ // older databases still carry them.
+ "application/font-woff",
+ "application/font-woff2",
+ "application/x-font-opentype",
+ "application/x-font-otf",
+ "application/x-font-truetype",
+ "application/x-font-ttf"
+] as const;
+
+const FONT_MIME_SET: ReadonlySet<string> = new Set(FONT_MIMES);
+
+/** Whether content of this media type is a font Trilium can draw. */
+export function isFontMimeType(mime: string | undefined | null): boolean {
+ return !!mime && FONT_MIME_SET.has(mime.toLowerCase());
+}
diff --git a/packages/commons/src/lib/notes.spec.ts b/packages/commons/src/lib/notes.spec.ts
index f2a8307a4a..ac8df8cef8 100644
--- a/packages/commons/src/lib/notes.spec.ts
+++ b/packages/commons/src/lib/notes.spec.ts
@@ -141,6 +141,13 @@ describe("getNoteIcon", () => {
expect(icon).toBe("bx bxs-file-pdf");
});
+ it("marks a font file as a font, whichever media type it arrived under", () => {
+ expect(getNoteIcon(buildArgs({ type: "file", mime: "font/woff2" }))).toBe("bx bx-font");
+ expect(getNoteIcon(buildArgs({ type: "file", mime: "application/x-font-ttf" }))).toBe("bx bx-font");
+ // EOT is no font Trilium can draw, so it stays a plain file.
+ expect(getNoteIcon(buildArgs({ type: "file", mime: "application/vnd.ms-fontobject" }))).toBe("bx bx-file");
+ });
+
it("marks a GPX track as the journey it holds rather than as a file", () => {
const icon = getNoteIcon(buildArgs({ type: "file", mime: "application/gpx+xml" }));
expect(icon).toBe("bx bx-trip");
diff --git a/packages/commons/src/lib/notes.ts b/packages/commons/src/lib/notes.ts
index c9dfbb8a16..c67d629154 100644
--- a/packages/commons/src/lib/notes.ts
+++ b/packages/commons/src/lib/notes.ts
@@ -2,6 +2,7 @@
* @module notes Common logic for notes (across front-end and back-end)
*/
+import { isFontMimeType } from "./font_mimes.js";
import { MIME_TYPES_DICT } from "./mime_type.js";
import { NoteType } from "./rows.js";
@@ -165,6 +166,7 @@ export function getMimeIcon(mime: string | undefined | null): string {
function getFileMimeIcon(mime: string): string {
if (mime.startsWith("video/")) return "bx bx-video";
if (mime.startsWith("audio/")) return "bx bx-music";
+ if (isFontMimeType(mime)) return "bx bx-font";
return lookUpMime(FILE_MIME_MAPPINGS, mime) ?? NOTE_TYPE_ICONS.file;
}
diff --git a/packages/trilium-core/src/services/import/zip.spec.ts b/packages/trilium-core/src/services/import/zip.spec.ts
index 3d10490831..29578e99dd 100644
--- a/packages/trilium-core/src/services/import/zip.spec.ts
+++ b/packages/trilium-core/src/services/import/zip.spec.ts
@@ -545,6 +545,21 @@ describe("processNoteContent", () => {
expect(note?.mime).toBe("text/csv");
});
+ it("drops the extension of a font or a recording in a plain archive, as a single-file import does", async () => {
+ const zipBuffer = await createZipBuffer({
+ "Iosevka-Regular.ttf": Buffer.from("\0\u0001\0\0"),
+ "Interview.mp3": Buffer.from("ID3"),
+ // Nothing records that this is a spreadsheet but its name, so the name keeps it.
+ "Numbers.ods": Buffer.from("PK")
+ });
+ const { rootNote } = await testImportBuffer(zipBuffer, "import-font-zip", {});
+ const titleOf = (mime: string) => rootNote.getChildNotes().find((note) => note.mime === mime)?.title;
+
+ expect(titleOf("font/ttf")).toBe("Iosevka-Regular");
+ expect(titleOf("audio/mpeg")).toBe("Interview");
+ expect(titleOf("application/vnd.oasis.opendocument.spreadsheet")).toBe("Numbers.ods");
+ });
+
it("skips macOS resource-fork entries and reports an archive that holds nothing else", async () => {
// A zip zipped on macOS carries a __MACOSX/ sidecar for every real entry. With nothing but
// those, no note is ever created and the import has no root to hand back.
diff --git a/packages/trilium-core/src/services/import/zip.ts b/packages/trilium-core/src/services/import/zip.ts
index d8c4a4cc0b..5e22956bb9 100644
--- a/packages/trilium-core/src/services/import/zip.ts
+++ b/packages/trilium-core/src/services/import/zip.ts
@@ -650,7 +650,10 @@ async function importZip(taskContext: TaskContext<"importNotes">, source: ZipSou
content = processStringOrBuffer(content);
}
- const noteTitle = getNoteTitle(filePath, taskContext.data?.replaceUnderscoresWithSpaces || false, noteMeta);
+ // A plain archive carries no metadata, so the detected mime stands in for it: getNoteTitle
+ // reads the mime to drop the extension of the types that record their format in the note's
+ // own mime (video, audio, fonts), which single-file import already does.
+ const noteTitle = getNoteTitle(filePath, taskContext.data?.replaceUnderscoresWithSpaces || false, noteMeta ?? { mime });
// Generic Markdown (not a Trilium export, which carries its attributes in !!!meta.json) may begin
// with a YAML front matter block; lift it into labels and strip it before the body is rendered.
diff --git a/packages/trilium-core/src/services/utils/index.spec.ts b/packages/trilium-core/src/services/utils/index.spec.ts
index 3c18292570..7669e8d4c2 100644
--- a/packages/trilium-core/src/services/utils/index.spec.ts
+++ b/packages/trilium-core/src/services/utils/index.spec.ts
@@ -1039,6 +1039,16 @@ describe("#removeFileExtension (media types)", () => {
expect(utils.removeFileExtension("clip.mov", "video/quicktime")).toBe("clip");
expect(utils.removeFileExtension("song.flac", "audio/flac")).toBe("song");
});
+
+ it("strips the extension for the fonts Trilium draws, and leaves the rest alone", () => {
+ expect(utils.removeFileExtension("Iosevka-Regular.ttf", "font/ttf")).toBe("Iosevka-Regular");
+ expect(utils.removeFileExtension("Inter.woff2", "font/woff2")).toBe("Inter");
+ expect(utils.removeFileExtension("Legacy.ttf", "application/x-font-ttf")).toBe("Legacy");
+ // Neither an EOT nor a font arriving as an opaque binary is one Trilium can draw, so the
+ // name it was uploaded under is all there is to say what it holds.
+ expect(utils.removeFileExtension("Old.eot", "application/vnd.ms-fontobject")).toBe("Old.eot");
+ expect(utils.removeFileExtension("Mystery.ttf", "application/octet-stream")).toBe("Mystery.ttf");
+ });
});
describe("#compareVersions", () => {
diff --git a/packages/trilium-core/src/services/utils/index.ts b/packages/trilium-core/src/services/utils/index.ts
index b96cac468e..096f782994 100644
--- a/packages/trilium-core/src/services/utils/index.ts
+++ b/packages/trilium-core/src/services/utils/index.ts
@@ -1,3 +1,5 @@
+import { isFontMimeType } from "@triliumnext/commons/src/lib/font_mimes.js";
+
import { getCrypto } from "../encryption/crypto";
import { getPlatform } from "../platform";
import { sanitizeFileName } from "../sanitizer";
@@ -274,7 +276,9 @@ export function escapeRegExp(str: string) {
export function removeFileExtension(filePath: string, mime?: string) {
const extension = extname(filePath).toLowerCase();
- if (mime?.startsWith("video/") || mime?.startsWith("audio/")) {
+ // Dropped by media type rather than by extension: what these carry after the dot is the
+ // format the file is in, which the note's own mime already records.
+ if (mime?.startsWith("video/") || mime?.startsWith("audio/") || isFontMimeType(mime)) {
return filePath.substring(0, filePath.length - extension.length);
}