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): accept non-ASCII characters in prefixes and icon keys
Elian Doran85d5398c8580
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Theme development/Creating an icon pack.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Theme development/Creating an icon pack.htmlindex c35d0a2053..56e2800e6c 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Theme development/Creating an icon pack.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Theme development/Creating an icon pack.html@@ -19,7 +19,8 @@be <code spellcheck="false">font/woff2</code>, <code spellcheck="false">font/woff</code>,or <code spellcheck="false">font/ttf</code> and role <code spellcheck="false">file</code>).</li><li>Add the label <code spellcheck="false">#iconPack=<prefix></code> to- the note (prefix: alphanumeric, hyphen, underscore only).</li>+ the note (prefix: letters, digits, hyphen, underscore and non-ASCII characters+ only).</li><li>Refresh the client and verify the icon pack appears in the icon selector.</li></ol><h2>Verify the icon set</h2>@@ -102,6 +103,12 @@appear strange (e.g. ), but it will be rendered properly regardless.</li><li>The manifest keys (e.g. <code spellcheck="false">ph-acorn</code>) shouldmatch the class names used by the font (prefix + name is a common pattern).</li>+ <li>Like the prefix, a manifest key becomes a CSS class name, so it can contain+ only letters, digits, hyphens, underscores and non-ASCII characters. Punctuation+ and whitespace (e.g. <code spellcheck="false">ph.acorn</code> or <code spellcheck="false">ph acorn</code>)+ are not usable as class names; icons with such a key are skipped and an+ error is logged in <a class="reference-link" href="#root/_help_bnyigUA2UK7s">Backend (server) logs</a>,+ while the rest of the pack still loads.</li></ul></aside><h2>Concrete example: Phosphor Icons</h2>@@ -196,8 +203,8 @@ processIconPack("light");</code></pre>since it also matches the prefix set in the original CSS. So in this caseit would be <code spellcheck="false">#iconPack=ph</code>.</p><aside class="admonition important">- <p>The prefix must consist of only alphanumeric characters, hyphens and underscore.- If the prefix doesn't match these constraints, the icon pack will be ignored+ <p>The prefix must consist of only letters, digits, hyphens, underscores and+ non-ASCII characters. If the prefix doesn't match these constraints, the icon pack will be ignoredand an error will be logged in <a class="reference-link" href="#root/_help_bnyigUA2UK7s">Backend (server) logs</a>.</p></aside><h2>Creating the Trilium icon pack note</h2>@@ -260,6 +267,10 @@ processIconPack("light");</code></pre><li>Make sure the prefix is unique and not already taken by some other iconpack. When there are two icon packs with the same prefix, only one is used.The server logs will indicate if this situation occurs.</li>- <li>Make sure the prefix consists only of alphanumeric characters, hyphens- and underscore.</li>+ <li>Make sure the prefix consists only of letters, digits, hyphens, underscores+ and non-ASCII characters.</li>+ <li>If the pack loads but individual icons are missing, check the logs for+ <code spellcheck="false">Skipping icon '<key>' of icon pack</code> —+ that key uses characters outside the allowed set and needs renaming in the+ manifest.</li></ul>\ No newline at end of filediff --git a/docs/User Guide/User Guide/Theme development/Creating an icon pack.md b/docs/User Guide/User Guide/Theme development/Creating an icon pack.mdindex 3afb286444..6c9c4deee7 100644--- a/docs/User Guide/User Guide/Theme development/Creating an icon pack.md+++ b/docs/User Guide/User Guide/Theme development/Creating an icon pack.md@@ -11,7 +11,7 @@ First read the quick flow to get the overall steps. After that there is a concre3. Create a manifest JSON that maps icon ids to glyphs and search terms.4. Create a Trilium note of type Code, set language to JSON, paste the manifest as the note content.5. Upload the font file as an attachment to the same note (MIME type must be `font/woff2`, `font/woff`, or `font/ttf` and role `file`).-6. Add the label `#iconPack=<prefix>` to the note (prefix: alphanumeric, hyphen, underscore only).+6. Add the label `#iconPack=<prefix>` to the note (prefix: letters, digits, hyphen, underscore and non-ASCII characters only).7. Refresh the client and verify the icon pack appears in the icon selector.## Verify the icon set@@ -64,7 +64,7 @@ Example minimal manifest:> * You can supply glyph as the escaped `\uXXXX` sequence or as the actual UTF‑8 character.> * It is also possible to use the unescaped glyph inside the JSON. It will appear strange (e.g. ), but it will be rendered properly regardless.> * The manifest keys (e.g. `ph-acorn`) should match the class names used by the font (prefix + name is a common pattern).-> * Like the prefix, a manifest key must consist of only alphanumeric characters, hyphens and underscore, since it becomes a CSS class name. Icons whose key contains anything else are skipped and an error is logged in <a class="reference-link" href="../Troubleshooting/Error%20logs/Backend%20(server)%20logs.md">Backend (server) logs</a>; the rest of the pack still loads.+> * Like the prefix, a manifest key becomes a CSS class name, so it can contain only letters, digits, hyphens, underscores and non-ASCII characters. Punctuation and whitespace (e.g. `ph.acorn` or `ph acorn`) are not usable as class names; icons with such a key are skipped and an error is logged in <a class="reference-link" href="../Troubleshooting/Error%20logs/Backend%20(server)%20logs.md">Backend (server) logs</a>, while the rest of the pack still loads.## Concrete example: Phosphor Icons@@ -157,7 +157,7 @@ In order for an icon pack to be recognized, the prefix must be specified in theFor our example with Phosphor Icons, we can use the `ph` prefix since it also matches the prefix set in the original CSS. So in this case it would be `#iconPack=ph`.> [!IMPORTANT]-> The prefix must consist of only alphanumeric characters, hyphens and underscore. If the prefix doesn't match these constraints, the icon pack will be ignored and an error will be logged in <a class="reference-link" href="../Troubleshooting/Error%20logs/Backend%20(server)%20logs.md">Backend (server) logs</a>.+> The prefix must consist of only letters, digits, hyphens, underscores and non-ASCII characters. If the prefix doesn't match these constraints, the icon pack will be ignored and an error will be logged in <a class="reference-link" href="../Troubleshooting/Error%20logs/Backend%20(server)%20logs.md">Backend (server) logs</a>.## Creating the Trilium icon pack note@@ -188,5 +188,5 @@ If the icon pack doesn't show up, look through the <a class="reference-link" hr* One example is if the font could not be retrieved: `ERROR: Icon pack is missing WOFF/WOFF2/TTF attachment: Boxicons v3 400 (dup) (XRzqDQ67fHEK)`.* Make sure the prefix is unique and not already taken by some other icon pack. When there are two icon packs with the same prefix, only one is used. The server logs will indicate if this situation occurs.-* Make sure the prefix consists only of alphanumeric characters, hyphens and underscore.+* Make sure the prefix consists only of letters, digits, hyphens, underscores and non-ASCII characters.* If the pack loads but individual icons are missing, check the logs for `Skipping icon '<key>' of icon pack` — that key uses characters outside the allowed set and needs renaming in the manifest.\ No newline at end of filediff --git a/packages/trilium-core/src/services/icon_packs.spec.ts b/packages/trilium-core/src/services/icon_packs.spec.tsindex be338bc8fa..dfa4979db9 100644--- a/packages/trilium-core/src/services/icon_packs.spec.ts+++ b/packages/trilium-core/src/services/icon_packs.spec.ts@@ -191,6 +191,26 @@ describe("Generating CSS for untrusted manifests", () => {expect(css).not.toContain(".un.un evil");});+ it("keeps non-ASCII icon keys, which are valid CSS class names", () => {+ const css = generateCssFor({+ "un-caf\u00e9": { glyph: "\ue9c2", terms: [ "coffee" ] },+ "un-\u56fe\u6807": { glyph: "\ue9c3", terms: [ "icon" ] }+ });++ expect(css).toContain(`.un.un-caf\u00e9::before { content: "\ue9c2"; }`);+ expect(css).toContain(`.un.un-\u56fe\u6807::before { content: "\ue9c3"; }`);+ });++ it("keeps emoji glyphs and keys, whose surrogate pairs sit above U+0080", () => {+ const css = generateCssFor({+ "utf-grinning-face": { glyph: "\u{1F600}", terms: [ "grinning face" ] },+ "utf-\u{1F600}": { glyph: "\u{1F600}", terms: [ "grinning face" ] }+ });++ expect(css).toContain(`.un.utf-grinning-face::before { content: "\u{1F600}"; }`);+ expect(css).toContain(`.un.utf-\u{1F600}::before { content: "\u{1F600}"; }`);+ });+it("escapes a glyph so it cannot end the style element it is served in", () => {const css = generateCssFor({"un-evil": { glyph: `</style><script>alert(1)</script>`, terms: [ "evil" ] }diff --git a/packages/trilium-core/src/services/icon_packs.ts b/packages/trilium-core/src/services/icon_packs.tsindex 2dff90ce07..9d1c49f8d0 100644--- a/packages/trilium-core/src/services/icon_packs.ts+++ b/packages/trilium-core/src/services/icon_packs.ts@@ -20,11 +20,12 @@ const MIME_TO_CSS_FORMAT_MAPPINGS: Record<typeof PREFERRED_MIME_TYPE[number], st};/**- * Charset a pack's prefix and each of its icon keys must match. Both are interpolated into- * a CSS class selector, so anything outside it would need escaping there — and a key holding- * `</style>` would end the inline `<style>` element the stylesheet is served in.+ * Character set a pack's prefix and each of its icon keys must match. Both are interpolated+ * into a CSS class selector, which takes letters, digits, `-`, `_` and any code point at or+ * above U+0080 unescaped. Anything else has to be rejected: a key holding `</style>` ends the+ * inline `<style>` element that `generateCss()` output is served in.*/-const CSS_CLASS_NAME_PATTERN = /^[a-zA-Z0-9-_]+$/;+const CSS_CLASS_NAME_PATTERN = /^[a-zA-Z0-9_\u0080-\uFFFF-]+$/;export const MIME_TO_EXTENSION_MAPPINGS: Record<string, string> = {"font/ttf": "ttf",@@ -126,9 +127,8 @@ export function processIconPack(iconPackNote: BNote): ProcessedIconPack | undefireturn;}- // Ensure prefix is alphanumeric only, dashes and underscores.if (!CSS_CLASS_NAME_PATTERN.test(prefix)) {- getLog().error(`Icon pack has invalid 'iconPack' prefix (only alphanumeric characters, dashes and underscores are allowed): ${iconPackNote.title} (${iconPackNote.noteId})`);+ getLog().error(`Icon pack has invalid 'iconPack' prefix (only letters, digits, dashes, underscores and non-ASCII characters are allowed): ${iconPackNote.title} (${iconPackNote.noteId})`);return;}@@ -165,7 +165,7 @@ export function generateCss({ manifest, fontMime, builtin, fontAttachmentId, preconst iconDeclarations: 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 alphanumeric characters, dashes and underscores.`);+ getLog().error(`Skipping icon '${key}' of icon pack '${prefix}': keys allow only letters, digits, dashes, underscores and non-ASCII characters.`);continue;}