feat(icon_pack): generate icon registry for client

This commit is contained in:
Elian Doran
2025-12-26 19:10:28 +02:00
parent e346963e76
commit e2f6f8a4e4
5 changed files with 62 additions and 4 deletions

View File

@@ -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) %>
};
</script>

View File

@@ -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)
});
}

View File

@@ -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"
}
]
});
});
});

View File

@@ -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) {

View File

@@ -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;
}[]
}[];
}