From 5ad7323d033df9f715e4420a11839dcd97294d5d Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Fri, 26 Dec 2025 17:28:19 +0200 Subject: [PATCH] chore(icon_pack): map woff2 attachment --- apps/server/src/becca/entities/battachment.ts | 19 +++++++-------- apps/server/src/services/icon_packs.spec.ts | 19 ++++++++++++++- apps/server/src/services/icon_packs.ts | 22 +++++++++++++++++- apps/server/src/test/becca_easy_mocking.ts | 23 +++++++++++++++++++ 4 files changed, 72 insertions(+), 11 deletions(-) diff --git a/apps/server/src/becca/entities/battachment.ts b/apps/server/src/becca/entities/battachment.ts index ec50e7a6a..924dd972a 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 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 dateUtils from "../../services/date_utils.js"; +import log from "../../services/log.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 432d964a5..3e2bd8688 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 0813d709d..c313c0259 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(); + 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 8bce9efc9..b36a9cdb3 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; }