diff --git a/apps/server/src/assets/views/partials/windowGlobal.ejs b/apps/server/src/assets/views/partials/windowGlobal.ejs index c69351e1a..31911bc68 100644 --- a/apps/server/src/assets/views/partials/windowGlobal.ejs +++ b/apps/server/src/assets/views/partials/windowGlobal.ejs @@ -19,6 +19,7 @@ platform: "<%= platform %>", hasNativeTitleBar: <%= hasNativeTitleBar %>, TRILIUM_SAFE_MODE: <%= !!process.env.TRILIUM_SAFE_MODE %>, - isRtl: <%= !!currentLocale.rtl %> + isRtl: <%= !!currentLocale.rtl %>, + iconRegistry: <%- JSON.stringify(iconRegistry) %> }; \ No newline at end of file diff --git a/apps/server/src/routes/index.ts b/apps/server/src/routes/index.ts index 97bfb88d8..bc94dc7bc 100644 --- a/apps/server/src/routes/index.ts +++ b/apps/server/src/routes/index.ts @@ -7,7 +7,7 @@ import assetPath from "../services/asset_path.js"; import attributeService from "../services/attributes.js"; import config from "../services/config.js"; import { getCurrentLocale } from "../services/i18n.js"; -import { generateCss, getIconPacks } from "../services/icon_packs.js"; +import { generateCss, generateIconRegistry, getIconPacks } from "../services/icon_packs.js"; import log from "../services/log.js"; import optionService from "../services/options.js"; import protectedSessionService from "../services/protected_session.js"; @@ -62,7 +62,8 @@ function index(req: Request, res: Response) { appPath, baseApiUrl: 'api/', currentLocale: getCurrentLocale(), - iconPackCss: iconPacks.map(p => generateCss(p)).join("\n\n") + iconPackCss: iconPacks.map(p => generateCss(p)).join("\n\n"), + iconRegistry: generateIconRegistry(iconPacks) }); } diff --git a/apps/server/src/services/icon_packs.spec.ts b/apps/server/src/services/icon_packs.spec.ts index 0da99d4c0..7a26cba69 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 { determineBestFontAttachment, generateCss, IconPackManifest, processIconPack } from "./icon_packs"; +import { determineBestFontAttachment, generateCss, generateIconRegistry, IconPackManifest, processIconPack } from "./icon_packs"; const manifest: IconPackManifest = { name: "Boxicons v2", @@ -137,3 +137,29 @@ describe("CSS generation", () => { expect(css).toContain(".bx.bxs-party::before { content: '\\ec92'; }"); }); }); + +describe("Icon registery", () => { + it("generates the registry", () => { + const iconPack = processIconPack(buildNote({ + type: "text", + content: JSON.stringify(manifest), + attachments: [ defaultAttachment ] + })); + const registry = generateIconRegistry([ iconPack! ]); + expect(registry.sources).toHaveLength(1); + expect(registry.sources[0]).toMatchObject({ + name: "Boxicons v2", + prefix: "bx", + icons: [ + { + id: "bx-ball", + label: "ball" + }, + { + id: "bxs-party", + label: "party" + } + ] + }); + }); +}); diff --git a/apps/server/src/services/icon_packs.ts b/apps/server/src/services/icon_packs.ts index 406d32811..946e3cfd7 100644 --- a/apps/server/src/services/icon_packs.ts +++ b/apps/server/src/services/icon_packs.ts @@ -1,3 +1,5 @@ +import { IconRegistry } from "@triliumnext/commons"; + import type BAttachment from "../becca/entities/battachment"; import type BNote from "../becca/entities/bnote"; import log from "./log"; @@ -33,6 +35,23 @@ export function getIconPacks() { .filter(Boolean) as ProcessResult[]; } +export function generateIconRegistry(iconPacks: ProcessResult[]): IconRegistry { + const sources: IconRegistry["sources"] = []; + + for (const { manifest } of iconPacks) { + sources.push({ + prefix: manifest.prefix, + name: manifest.name, + icons: Object.keys(manifest.icons).map(id => ({ + id, + label: id.split("-").at(-1) ?? id + })) + }); + } + + return { sources }; +} + export function processIconPack(iconPackNote: BNote): ProcessResult | undefined { const manifest = iconPackNote.getJsonContentSafely() as IconPackManifest; if (!manifest) { diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.ts index 978d7b429..3f3d706f8 100644 --- a/packages/commons/src/lib/server_api.ts +++ b/packages/commons/src/lib/server_api.ts @@ -285,3 +285,14 @@ export interface RenderMarkdownResponse { export interface ToMarkdownResponse { markdownContent: string; } + +export interface IconRegistry { + sources: { + prefix: string; + name: string; + icons: { + id: string; + label: string; + }[] + }[]; +}