8000 feat(preset-icons,transformer-directives): allow add safe icons colle… · unocss/unocss@6358f9a · GitHub
[go: up one dir, main page]

Skip to content

Commit 6358f9a

Browse files
authored
feat(preset-icons,transformer-directives): allow add safe icons collections (#4319)
1 parent 6cc5f51 commit 6358f9a

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

docs/presets/icons.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,13 @@ Extra CSS properties applied to the generated CSS.
442442
443443
Emit warning when missing icons are matched.
444444
445+
### iconifyCollectionsNames
446+
447+
- Type: `string[]`
448+
- Default: `undefined`
449+
450+
Additional `@iconify-json` collections to use. This option should be used when there are new `@iconify-json` collections not listed in the default icons preset collection names.
451+
445452
### collections
446453
447454
- Type: `Record<string, (() => Awaitable<IconifyJSON>) | undefined | CustomIconLoader | InlineCollection>`

packages/preset-icons/src/core.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function createPresetIcons(lookupIconLoader: (options: IconsOptions) => P
3232
mode = 'auto',
3333
prefix = 'i-',
3434
warn = false,
35+
iconifyCollectionsNames,
3536
collections: customCollections,
3637
extraProperties = {},
3738
customizations = {},
@@ -87,7 +88,12 @@ export function createPresetIcons(lookupIconLoader: (options: IconsOptions) => P
8788
iconLoader = iconLoader || await lookupIconLoader(options)
8889

8990
const usedProps = {}
90-
const parsed = await parseIconWithLoader(body, iconLoader, { ...loaderOptions, usedProps })
91+
const parsed = await parseIconWithLoader(
92+
body,
93+
iconLoader,
94+
{ ...loaderOptions, usedProps },
95+
iconifyCollectionsNames,
96+
)
9197

9298
if (!parsed) {
9399
if (warn && !flags.isESLint)
@@ -196,20 +202,26 @@ export function getEnvFlags() {
196202
}
197203
}
198204

199-
export async function parseIconWithLoader(body: string, loader: UniversalIconLoader, options: IconifyLoaderOptions = {}) {
205+
export async function parseIconWithLoader(
206+
body: string,
207+
loader: UniversalIconLoader,
208+
options: IconifyLoaderOptions = {},
209+
safeCollectionsNames: string[] = [],
210+
) {
200211
let collection = ''
201212
let name = ''
202213
let svg: string | undefined
203214

204-
const allCollections = [
215+
const allCollections = new Set<string>([
205216
...icons,
217+
...safeCollectionsNames,
206218
...Object.keys(options.customCollections || {}),
207-
]
219+
])
208220

209221
if (body.includes(':')) {
210222
[collection, name] = body.split(':')
211223

212-
if (!allCollections.includes(collection))
224+
if (!allCollections.has(collection))
213225
return
214226

215227
svg = await loader(collection, name, options)
@@ -219,7 +231,7 @@ export async function parseIconWithLoader(body: string, loader: UniversalIconLoa
219231
for (let i = COLLECTION_NAME_PARTS_MAX; i >= 1; i--) {
220232
collection = parts.slice(0, i).join('-')
221233

222-
if (!allCollections.includes(collection))
234+
if (!allCollections.has(collection))
223235
continue
224236

225237
name = parts.slice(i).join('-')

packages/preset-icons/src/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ export interface IconsOptions {
5555
*/
5656
warn?: boolean
5757

58+
/**
59+
* `@iconify-json` collections to use (will be also auto installed when missing and `autoInstall` enabled).
60+
*
61+
* This option should be used only when there are new `@iconify-json` collections not listed in the default icons preset collection names.
62+
*
63+
* Adding external collections will not work, you should use `FileSystemIconLoader` from
64+
* `@iconify/utils/lib/loader/fs` or `createExternalPackageIconLoader` from
65+
* `@iconify/utils/lib/loader/external-pkg` instead.
66+
*
67+
* @see https://unocss.dev/presets/icons#filesystemiconloader
68+
* @see https://unocss.dev/presets/icons#externalpackageiconloader
69+
*/
70+
iconifyCollectionsNames?: string[]
71+
5872
/**
5973
* In Node.js environment, the preset will search for the installed iconify dataset automatically.
6074
* When using in the browser, this options is provided to provide dataset with custom loading mechanism.

packages/transformer-directives/src/icon.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export async function transformIconString(uno: UnoGenerator, icon: string, color
1717
collections: customCollections,
1818
customizations = {},
1919
autoInstall = false,
20+
iconifyCollectionsNames,
2021
collectionsNodeResolvePath,
2122
unit,
2223
} = presetIcons.options as IconsOptions
@@ -51,7 +52,12 @@ export async function transformIconString(uno: UnoGenerator, icon: string, color
5152
for (const p of toArray(prefix)) {
5253
if (icon.startsWith(p)) {
5354
icon = icon.slice(p.length)
54-
const parsed = await api.parseIconWithLoader(icon, loader, loaderOptions)
55+
const parsed = await api.parseIconWithLoader(
56+
icon,
57+
loader,
58+
loaderOptions,
59+
iconifyCollectionsNames,
60+
)
5561
if (parsed)
5662
return `url("data:image/svg+xml;utf8,${color ? api.encodeSvgForCss(parsed.svg).replace(/currentcolor/gi, color) : api.encodeSvgForCss(parsed.svg)}")`
5763
}

0 commit comments

Comments
 (0)
0