From 6c613eeba6ff4ea11670be039b1e245541bb0203 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 8 Jan 2023 12:56:10 +0200 Subject: [PATCH] feat: import icon sets from 'icons' directory, env variable to control Iconify icon sets import --- icons/README.md | 26 ++++++++++++++++++++++++++ src/config/icon-sets.ts | 13 ++++++++----- src/importers/common/icon-set-json.ts | 11 ++++++++--- 3 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 icons/README.md diff --git a/icons/README.md b/icons/README.md new file mode 100644 index 0000000..386bf12 --- /dev/null +++ b/icons/README.md @@ -0,0 +1,26 @@ +# Icons + +This directory contains custom icon sets and icons. + +## Icon sets + +Icon sets are stored in IconifyJSON format. + +You can use Iconify Tools to create icon sets. See [Iconify Tools documentation](https://docs.iconify.design/tools/tools2/). + +Each icon set has prefix. For icon set to be imported, filename must match icon set prefix, for example, `line-md.json` for icon set with `line-md` prefix. + +Icon sets that have `info` property and are not marked as hidden, appear in icon sets list and search results (unless those features are disabled). +If you want icon set to be hidden, all you have to do is not add `info` property when creating icon set or remove it. + +## Icons + +TODO + +Currently not supported yet. Create icon sets instead. + +## Conflicts + +If API serves both custom and Iconify icon sets, it is possible to have conflicting names. If 2 icon sets with identical prefix exist in both sources, custom icon set will be used. + +To disable Iconify icon sets, set env variable `ICONIFY_SOURCE` to `none`. You can use `.env` file in root directory. diff --git a/src/config/icon-sets.ts b/src/config/icon-sets.ts index ac59e3f..d38c894 100644 --- a/src/config/icon-sets.ts +++ b/src/config/icon-sets.ts @@ -20,8 +20,8 @@ export async function getImporters(): Promise { * * Uses pre-configured importers. See `importers` sub-directory */ - type IconifyIconSetsOptions = 'full' | 'split' | false; - const iconifyIconSets = 'full' as IconifyIconSetsOptions; + type IconifyIconSetsOptions = 'full' | 'split' | 'none'; + const iconifyIconSets = (process.env['ICONIFY_SOURCE'] || 'full') as IconifyIconSetsOptions; switch (iconifyIconSets) { case 'full': @@ -34,11 +34,14 @@ export async function getImporters(): Promise { } /** - * Add custom icons from `json` directory + * Add custom icons from `icons` directory */ - if (await directoryExists('json')) { + if (await directoryExists('icons')) { importers.push( - createJSONDirectoryImporter(new DirectoryDownloader('json'), { + createJSONDirectoryImporter(new DirectoryDownloader('icons'), { + // Skip icon sets with mismatched prefix + ignoreInvalidPrefix: false, + // Filter icon sets. Returns true if icon set should be included, false if not. filter: (prefix) => { return true; diff --git a/src/importers/common/icon-set-json.ts b/src/importers/common/icon-set-json.ts index 76e1d32..ec20292 100644 --- a/src/importers/common/icon-set-json.ts +++ b/src/importers/common/icon-set-json.ts @@ -6,6 +6,8 @@ import { prependSlash } from '../../misc/files'; export interface IconSetJSONOptions { // Ignore bad prefix? + // false -> skip icon sets with mismatched prefix + // true -> import icon set with mismatched prefix ignoreInvalidPrefix?: boolean; } @@ -26,9 +28,12 @@ export async function importIconSetFromJSON( } if (data.prefix !== prefix) { if (!options.ignoreInvalidPrefix) { - console.error( - `Error loading "${prefix}" icon set: bad prefix (enable ignoreInvalidPrefix option in importer to skip this check)` - ); + if (options.ignoreInvalidPrefix === void 0) { + // Show warning if option is not set + console.error( + `Error loading "${prefix}" icon set: bad prefix (enable ignoreInvalidPrefix option in importer to import icon set)` + ); + } return; } data.prefix = prefix;