Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(icon packs): log skipped icon keys once per pack
Elian Dorana95affeefc93
diff --git a/packages/trilium-core/src/services/icon_packs.spec.ts b/packages/trilium-core/src/services/icon_packs.spec.tsindex 6d37f24f47..81d0891163 100644--- a/packages/trilium-core/src/services/icon_packs.spec.ts+++ b/packages/trilium-core/src/services/icon_packs.spec.ts@@ -3,6 +3,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";import becca from "../becca/becca";import { buildNote } from "../test/becca_easy_mocking";import { determineBestFontAttachment, generateCss, generateIconRegistry, generateIconTransformCss, getIconPacks, IconPackManifest, processIconPack } from "./icon_packs";+import { getLog } from "./log";import { getPlatform } from "./platform";import search from "./search/services/search";@@ -227,6 +228,21 @@ describe("Generating CSS for untrusted manifests", () => {expect(css).not.toContain(".un.un evil");});+ it("reports the skipped icon keys of a pack in a single log line", () => {+ const errorSpy = vi.spyOn(getLog(), "error").mockImplementation(() => {});+ generateCssFor({+ "un-ok": { glyph: "", terms: [ "ok" ] },+ "un-a,b": { glyph: "", terms: [ "a" ] },+ "un-c,d": { glyph: "", terms: [ "c" ] },+ "un e": { glyph: "", terms: [ "e" ] }+ });++ expect(errorSpy).toHaveBeenCalledOnce();+ expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("Skipping 3 icons of icon pack 'un'"));+ expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("'un-a,b', 'un-c,d', 'un e'"));+ errorSpy.mockRestore();+ });+it("keeps non-ASCII icon keys, which are valid CSS class names", () => {const css = generateCssFor({"un-caf\u00e9": { glyph: "\ue9c2", terms: [ "coffee" ] },diff --git a/packages/trilium-core/src/services/icon_packs.ts b/packages/trilium-core/src/services/icon_packs.tsindex 90813bcec6..278bf2051e 100644--- a/packages/trilium-core/src/services/icon_packs.ts+++ b/packages/trilium-core/src/services/icon_packs.ts@@ -31,6 +31,7 @@ const MIME_TO_CSS_FORMAT_MAPPINGS: Record<typeof PREFERRED_MIME_TYPE[number], st* inline `<style>` element that `generateCss()` output is served in.*/const CSS_CLASS_NAME_PATTERN = /^[a-zA-Z0-9_\u0080-\uFFFF-]+$/;+const MAX_LOGGED_SKIPPED_KEYS = 10;export const MIME_TO_EXTENSION_MAPPINGS: Record<string, string> = {"font/ttf": "ttf",@@ -236,9 +237,10 @@ export function generateIconTransformCss(): string {export function generateCss({ manifest, fontMime, builtin, fontAttachmentId, prefix }: ProcessedIconPack, fontUrl: string) {try {const iconDeclarations: string[] = [];+ const skippedKeys: string[] = [];for (const [ key, mapping ] of Object.entries(manifest.icons)) {if (!CSS_CLASS_NAME_PATTERN.test(key)) {- getLog().error(`Skipping icon '${key}' of icon pack '${prefix}': keys allow only letters, digits, dashes, underscores and non-ASCII characters.`);+ skippedKeys.push(key);continue;}@@ -246,6 +248,12 @@ export function generateCss({ manifest, fontMime, builtin, fontAttachmentId, preiconDeclarations.push(`.${prefix}.${key}::before { content: "${glyph}"; }`);}+ if (skippedKeys.length) {+ const examples = skippedKeys.slice(0, MAX_LOGGED_SKIPPED_KEYS).map((key) => `'${key}'`).join(", ");+ const rest = skippedKeys.length > MAX_LOGGED_SKIPPED_KEYS ? ", …" : "";+ getLog().error(`Skipping ${skippedKeys.length} icons of icon pack '${prefix}' (${examples}${rest}): keys allow only letters, digits, dashes, underscores and non-ASCII characters.`);+ }+const fontFamily = builtin ? fontAttachmentId : `trilium-icon-pack-${prefix}`;const fontFace = [`font-family: '${fontFamily}';`,