Skip to content

Build your personal knowledge base with Trilium Notes

chore(icon_pack): map woff2 attachment

Elian Doran5ad7323d033d
diff --git a/apps/server/src/becca/entities/battachment.ts b/apps/server/src/becca/entities/battachment.ts
index ec50e7a6a6..924dd972ab 100644
--- a/apps/server/src/becca/entities/battachment.ts
+++ b/apps/server/src/becca/entities/battachment.ts
@@ -1,15 +1,16 @@
-"use strict";
-import utils from "../../services/utils.js";
+
+import type { AttachmentRow } from "@triliumnext/commons";
+
import dateUtils from "../../services/date_utils.js";
-import AbstractBeccaEntity from "./abstract_becca_entity.js";
-import sql from "../../services/sql.js";
-import protectedSessionService from "../../services/protected_session.js";
import log from "../../services/log.js";
-import type { AttachmentRow } from "@triliumnext/commons";
-import type BNote from "./bnote.js";
-import type BBranch from "./bbranch.js";
import noteService from "../../services/notes.js";
+import protectedSessionService from "../../services/protected_session.js";
+import sql from "../../services/sql.js";
+import utils from "../../services/utils.js";
+import AbstractBeccaEntity from "./abstract_becca_entity.js";
+import type BBranch from "./bbranch.js";
+import type BNote from "./bnote.js";
const attachmentRoleToNoteTypeMapping = {
image: "image",
diff --git a/apps/server/src/services/icon_packs.spec.ts b/apps/server/src/services/icon_packs.spec.ts
index 432d964a5f..3e2bd86883 100644
--- a/apps/server/src/services/icon_packs.spec.ts
+++ b/apps/server/src/services/icon_packs.spec.ts
@@ -1,5 +1,5 @@
import { buildNote } from "../test/becca_easy_mocking";
-import { IconPackManifest, processIconPack } from "./icon_packs";
+import { determineBestFontAttachment, IconPackManifest, processIconPack } from "./icon_packs";
describe("Processing icon packs", () => {
it("doesn't crash if icon pack is incorrect type", () => {
@@ -29,3 +29,20 @@ describe("Processing icon packs", () => {
});
});
});
+
+describe("Mapping attachments", () => {
+ it("handles woff2", () => {
+ const iconPackNote = buildNote({
+ type: "text",
+ attachments: [
+ {
+ role: "file",
+ title: "Font",
+ mime: "font/woff2"
+ }
+ ]
+ });
+ const attachment = determineBestFontAttachment(iconPackNote);
+ expect(attachment?.mime).toStrictEqual("font/woff2");
+ });
+});
diff --git a/apps/server/src/services/icon_packs.ts b/apps/server/src/services/icon_packs.ts
index 0813d709d3..c313c0259d 100644
--- a/apps/server/src/services/icon_packs.ts
+++ b/apps/server/src/services/icon_packs.ts
@@ -1,6 +1,11 @@
+import type BAttachment from "../becca/entities/battachment";
import type BNote from "../becca/entities/bnote";
import log from "./log";
+const PREFERRED_MIME_TYPE = [
+ "font/woff2"
+];
+
export interface IconPackManifest {
name: string;
prefix: string;
@@ -18,8 +23,23 @@ export function processIconPack(iconPackNote: BNote): ProcessResult | undefined
return;
}
-
return {
iconMappings: manifest.icons
};
}
+
+export function determineBestFontAttachment(iconPackNote: BNote) {
+ // Map all the attachments by their MIME.
+ const mappings = new Map<string, BAttachment>();
+ for (const attachment of iconPackNote.getAttachmentsByRole("file")) {
+ mappings.set(attachment.mime, attachment);
+ }
+
+ // Return the icon formats in order of preference.
+ for (const preferredMimeType of PREFERRED_MIME_TYPE) {
+ const correspondingAttachment = mappings.get(preferredMimeType);
+ if (correspondingAttachment) return correspondingAttachment;
+ }
+
+ return null;
+}
diff --git a/apps/server/src/test/becca_easy_mocking.ts b/apps/server/src/test/becca_easy_mocking.ts
index 8bce9efc91..b36a9cdb3c 100644
--- a/apps/server/src/test/becca_easy_mocking.ts
+++ b/apps/server/src/test/becca_easy_mocking.ts
@@ -1,5 +1,6 @@
import { NoteType } from "@triliumnext/commons";
+import BAttachment from "../becca/entities/battachment.js";
import BAttribute from "../becca/entities/battribute.js";
import BBranch from "../becca/entities/bbranch.js";
import BNote from "../becca/entities/bnote.js";
@@ -15,6 +16,11 @@ interface NoteDefinition extends AttributeDefinitions, RelationDefinitions {
type?: NoteType;
mime?: string;
children?: NoteDefinition[];
+ attachments?: {
+ title: string;
+ role: string;
+ mime: string;
+ }[];
}
/**
@@ -106,5 +112,22 @@ export function buildNote(noteDef: NoteDefinition) {
position++;
}
+
+ // Handle attachments.
+ if (noteDef.attachments) {
+ const allAttachments: BAttachment[] = [];
+ for (const { title, role, mime } of noteDef.attachments) {
+ const attachment = new BAttachment({
+ ownerId: note.noteId,
+ title,
+ role,
+ mime
+ });
+ allAttachments.push(attachment);
+ }
+
+ note.getAttachmentsByRole = (role) => allAttachments.filter(a => a.role === role);
+ }
+
return note;
}