feat: import icon sets from 'icons' directory, env variable to control Iconify icon sets import

This commit is contained in:
Vjacheslav Trushkin 2023-01-08 12:56:10 +02:00
parent 6e9abebfe5
commit 6c613eeba6
3 changed files with 42 additions and 8 deletions

26
icons/README.md Normal file
View File

@ -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.

View File

@ -20,8 +20,8 @@ export async function getImporters(): Promise<Importer[]> {
*
* 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<Importer[]> {
}
/**
* 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<ImportedData>('json'), {
createJSONDirectoryImporter(new DirectoryDownloader<ImportedData>('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;

View File

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