@@ -30,15 +30,37 @@ export function getCollectionPath(collection: string) {
30
30
: `@iconify-json/${ collection } /icons.json`
31
31
}
32
32
33
+ // https://github.com/iconify/iconify/blob/2274c033b49c01a50dc89b490b89d803d19d95dc/packages/utils/src/icon/name.ts#L15-L18
34
+ export const validIconNameRE = / ^ [ a - z 0 - 9 ] + ( - [ a - z 0 - 9 ] + ) * $ /
35
+
33
36
export async function loadCustomCollection ( collection : CustomCollection , nuxt : Nuxt ) : Promise < IconifyJSON > {
34
37
const dir = isAbsolute ( collection . dir )
35
38
? collection . dir
36
39
: join ( nuxt . options . rootDir , collection . dir )
37
- const files = ( await glob ( [ '*.svg' ] , { cwd : dir , onlyFiles : true , expandDirectories : false } ) )
40
+
41
+ const files = ( await glob ( [ '*.svg' ] , {
42
+ cwd : dir ,
43
+ onlyFiles : true ,
44
+ expandDirectories : false ,
45
+ } ) )
38
46
. sort ( )
39
47
40
48
const parsedIcons = await Promise . all ( files . map ( async ( file ) => {
41
- const name = basename ( file , '.svg' )
49
+ let name = basename ( file , '.svg' )
50
+
51
+ // Currently Iconify only supports kebab-case icon names
52
+ // https://github.com/nuxt/icon/issues/265#issuecomment-2441604639
53
+ // We normalize the icon name to kebab-case and warn user about it
54
+ if ( ! validIconNameRE . test ( name ) ) {
55
+ const normalized = name
56
+ . replace ( / ( [ a - z ] ) ( [ A - Z ] ) / g, '$1-$2' )
57
+ . toLowerCase ( )
58
+ . replace ( / [ ^ a - z 0 - 9 - ] / g, '-' )
59
+ . replace ( / - + / g, '-' )
60
+ logger . warn ( `Custom icon \`${ name } \` is normalized to \`${ normalized } \`, we recommend to change the file name to match the icon name` )
61
+ name = normalized
62
+ }
63
+
42
64
let svg = await fs . readFile ( join ( dir , file ) , 'utf-8' )
43
65
const cleanupIdx = svg . indexOf ( '<svg' )
44
66
if ( cleanupIdx > 0 )
0 commit comments