Skip to content

Commit c09fdc7

Browse files
committed
fix(custom-icons): add warning to custom icon name conventions, #265, #257
1 parent 7e2f5f0 commit c09fdc7

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed
+8
Loading

src/collections.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,37 @@ export function getCollectionPath(collection: string) {
3030
: `@iconify-json/${collection}/icons.json`
3131
}
3232

33+
// https://github.com/iconify/iconify/blob/2274c033b49c01a50dc89b490b89d803d19d95dc/packages/utils/src/icon/name.ts#L15-L18
34+
export const validIconNameRE = /^[a-z0-9]+(-[a-z0-9]+)*$/
35+
3336
export async function loadCustomCollection(collection: CustomCollection, nuxt: Nuxt): Promise<IconifyJSON> {
3437
const dir = isAbsolute(collection.dir)
3538
? collection.dir
3639
: 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+
}))
3846
.sort()
3947

4048
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-z0-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+
4264
let svg = await fs.readFile(join(dir, file), 'utf-8')
4365
const cleanupIdx = svg.indexOf('<svg')
4466
if (cleanupIdx > 0)

0 commit comments

Comments
 (0)