@prunellejs/icons 0.1.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/llms.txt +2 -2
- package/package.json +2 -2
- package/src/licence.ts +25 -0
- package/src/plugin.ts +18 -4
- package/src/sets.ts +18 -0
- package/src/svg-dir.ts +34 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kaynooo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/llms.txt
CHANGED
|
@@ -12,9 +12,9 @@
|
|
|
12
12
|
## Plugin Setup (`icons(options?)`)
|
|
13
13
|
|
|
14
14
|
- `sets`: optional `Record<string, IconSet>` of custom in-memory sets keyed by prefix.
|
|
15
|
-
- `dirs`: optional `Record<string, string>` mapping prefixes to local `.svg` folders relative to app root (e.g. `{ brand: 'assets/brand' }`). `src/icons` is automatically mounted as `~icons/local/<name>` unless overridden; edited files reload in `prunelle dev`.
|
|
15
|
+
- `dirs`: optional `Record<string, string>` mapping prefixes to local `.svg` folders relative to app root (e.g. `{ brand: 'assets/brand' }`). `src/icons` is automatically mounted as `~icons/local/<name>` unless overridden; edited files reload in `prunelle dev`. A file is inlined as is: `prunelle check` warns `[icons/svg-script]` on a `<script>`, an `on*` attribute or a `javascript:` URL in one, and the root paint attributes are escaped.
|
|
16
16
|
- Install Iconify icon packs as dev dependencies (e.g. `bun add -d @iconify-json/tabler`), then import `~icons/tabler/home`.
|
|
17
|
-
- CLI & checks: run `prunelle icons-find <words> [--set <prefix>] [--limit <n>]` instead of guessing names. `prunelle check` and `prunelle build` fail on missing/invalid icons and suggest closest matches.
|
|
17
|
+
- CLI & checks: run `prunelle icons-find <words> [--set <prefix>] [--limit <n>]` instead of guessing names. `prunelle check` and `prunelle build` fail on missing/invalid icons and suggest closest matches; `prunelle check` warns `[icons/licence]` at the first import of a set under CC BY, a copyleft or non-commercial licence, or none.
|
|
18
18
|
|
|
19
19
|
## `<Icon>` Component
|
|
20
20
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunellejs/icons",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Compile-time Iconify SVG icon plugin (~icons/<set>/<name>) for Prunelle",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -24,6 +24,6 @@
|
|
|
24
24
|
"directory": "packages/plugin-icons"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"prunelle": "^0.
|
|
27
|
+
"prunelle": "^0.7.0"
|
|
28
28
|
}
|
|
29
29
|
}
|
package/src/licence.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// What an installed Iconify set's licence asks of an app that inlines its icons: nothing for MIT, Apache, ISC, BSD, OFL, MPL, CC0 or the Unlicense, a credit or more for the others.
|
|
2
|
+
|
|
3
|
+
import type { SetInfo } from './sets'
|
|
4
|
+
|
|
5
|
+
const NON_COMMERCIAL_RE = /-NC(?:-|$)/i
|
|
6
|
+
const COPYLEFT_RE = /^(?:A?GPL|LGPL)-|-SA(?:-|$)/i
|
|
7
|
+
const ATTRIBUTION_RE = /^CC-BY-/i
|
|
8
|
+
const FREE_ALTERNATIVE = 'or use a set under MIT or Apache-2.0'
|
|
9
|
+
|
|
10
|
+
/** Why the set `prefix` needs a look before its icons ship, as the detail and the fix of a warning; undefined when its licence asks nothing. */
|
|
11
|
+
export function licenceProblem(prefix: string, info: SetInfo | undefined): { detail: string, fix: string } | undefined {
|
|
12
|
+
const spdx = info?.license?.spdx?.trim() ?? ''
|
|
13
|
+
const named = `the icon set "${prefix}"${info?.name === undefined ? '' : ` (${info.name})`}`
|
|
14
|
+
if (spdx === '')
|
|
15
|
+
return { detail: `${named} names no licence, so nothing grants the app the use of its icons`, fix: `ask its author for terms, ${FREE_ALTERNATIVE}` }
|
|
16
|
+
const licence = `${named} is under ${info?.license?.title ?? spdx}${info?.license?.url === undefined ? '' : ` (${info.license.url})`}`
|
|
17
|
+
const author = info?.author?.name === undefined ? 'its author' : `${info.author.name}${info.author.url === undefined ? '' : ` (${info.author.url})`}`
|
|
18
|
+
if (NON_COMMERCIAL_RE.test(spdx))
|
|
19
|
+
return { detail: `${licence}, which forbids commercial use`, fix: `use it on a non-commercial site only, ${FREE_ALTERNATIVE}` }
|
|
20
|
+
if (COPYLEFT_RE.test(spdx))
|
|
21
|
+
return { detail: `${licence}, which asks that a work including its icons be shared under the same licence${ATTRIBUTION_RE.test(spdx) ? `, crediting ${author}` : ''}`, fix: `check the app may be, ${FREE_ALTERNATIVE}` }
|
|
22
|
+
if (ATTRIBUTION_RE.test(spdx))
|
|
23
|
+
return { detail: `${licence}, which asks for credit to ${author} wherever its icons show`, fix: `credit it, on the legal notice page for instance, ${FREE_ALTERNATIVE}` }
|
|
24
|
+
return undefined
|
|
25
|
+
}
|
package/src/plugin.ts
CHANGED
|
@@ -13,7 +13,8 @@ import { definePlugin } from 'prunelle/config'
|
|
|
13
13
|
import { iconData } from './icon-data'
|
|
14
14
|
import { iconProblem } from './diagnose'
|
|
15
15
|
import { iconsFind } from './find'
|
|
16
|
-
import {
|
|
16
|
+
import { licenceProblem } from './licence'
|
|
17
|
+
import { installedPrefixes, isSetPrefix, setFile, setInfo } from './sets'
|
|
17
18
|
import { readSvgDir } from './svg-dir'
|
|
18
19
|
|
|
19
20
|
const ICON_SPEC_RE = /^~icons\/([^/]+)\/(.+)$/
|
|
@@ -119,10 +120,15 @@ export function icons(options: IconsPluginOptions = {}): PrunellePlugin {
|
|
|
119
120
|
ctx.error(`@prunellejs/icons: icons({ dirs }) reads the set "${prefix}" from ${dir}, which does not exist.`)
|
|
120
121
|
}
|
|
121
122
|
for (const prefix of lookup.dirs.keys()) {
|
|
122
|
-
|
|
123
|
+
const dirSet = await lookup.dirSet(prefix)
|
|
124
|
+
for (const [name, problem] of dirSet?.problems ?? [])
|
|
123
125
|
ctx.error(`${relative(ctx.root, join(lookup.dirs.get(prefix)!, `${name}.svg`))}:1:1: error: @prunellejs/icons: this file ${problem}.`)
|
|
126
|
+
for (const [name, hazard] of dirSet?.hazards ?? [])
|
|
127
|
+
ctx.warn(`${relative(ctx.root, join(lookup.dirs.get(prefix)!, `${name}.svg`))}:1:1: warning: [icons/svg-script] this file holds ${hazard}, which runs once the icon is inlined in a page: remove it from the file, or take the icon from a source you trust.`)
|
|
124
128
|
}
|
|
125
|
-
|
|
129
|
+
// Where each installed set is first imported, for the one warning its licence may need.
|
|
130
|
+
const firstImport = new Map<string, string>()
|
|
131
|
+
for (const file of [...new Bun.Glob('src/**/*.{ts,tsx,mts}').scanSync({ cwd: ctx.root })].sort()) {
|
|
126
132
|
const code = await Bun.file(join(ctx.root, file)).text()
|
|
127
133
|
const imported = importedIcons(code, file)
|
|
128
134
|
for (const found of code.matchAll(ICON_IMPORT_RE)) {
|
|
@@ -133,11 +139,19 @@ export function icons(options: IconsPluginOptions = {}): PrunellePlugin {
|
|
|
133
139
|
// A broken file is reported once above, not at each import.
|
|
134
140
|
if (await fileProblem(lookup, ctx.root, prefix!, name!) !== undefined)
|
|
135
141
|
continue
|
|
142
|
+
const at = `${file}:${lineAndColumn(code, found.index + found[0].length - found[2]!.length - 2)}`
|
|
136
143
|
const problem = await iconProblem(prefix!, name!, '', lookup)
|
|
137
144
|
if (problem !== undefined)
|
|
138
|
-
ctx.error(`${
|
|
145
|
+
ctx.error(`${at}: error: @prunellejs/icons: ${problem}`)
|
|
146
|
+
else if (!lookup.dirs.has(prefix!) && options.sets?.[prefix!] === undefined && !firstImport.has(prefix!))
|
|
147
|
+
firstImport.set(prefix!, at)
|
|
139
148
|
}
|
|
140
149
|
}
|
|
150
|
+
for (const [prefix, at] of firstImport) {
|
|
151
|
+
const licence = licenceProblem(prefix, await setInfo(prefix, ctx.root))
|
|
152
|
+
if (licence !== undefined)
|
|
153
|
+
ctx.warn(`${at}: warning: [icons/licence] ${licence.detail}: ${licence.fix}.`)
|
|
154
|
+
}
|
|
141
155
|
},
|
|
142
156
|
watchPaths: root => [...lookupIn(root).dirs.values()],
|
|
143
157
|
bunPlugins: ({ root, target }): BunPlugin[] => [{
|
package/src/sets.ts
CHANGED
|
@@ -33,6 +33,24 @@ export function setFile(prefix: string, root: string): string | undefined {
|
|
|
33
33
|
return undefined
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
/** The metadata of an installed set: Iconify writes it beside `icons.json` as `info.json`, or under `info` in an `@iconify/json` set. */
|
|
37
|
+
export interface SetInfo {
|
|
38
|
+
name?: string | undefined
|
|
39
|
+
author?: { name?: string | undefined, url?: string | undefined } | undefined
|
|
40
|
+
license?: { title?: string | undefined, spdx?: string | undefined, url?: string | undefined } | undefined
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The metadata of the set `prefix` installed for the app at `root`, undefined when it is not installed or carries none. */
|
|
44
|
+
export async function setInfo(prefix: string, root: string): Promise<SetInfo | undefined> {
|
|
45
|
+
const file = setFile(prefix, root)
|
|
46
|
+
if (file === undefined)
|
|
47
|
+
return undefined
|
|
48
|
+
if (basename(file) !== 'icons.json')
|
|
49
|
+
return (await Bun.file(file).json() as { info?: SetInfo }).info
|
|
50
|
+
const info = join(dirname(file), 'info.json')
|
|
51
|
+
return existsSync(info) ? await Bun.file(info).json() as SetInfo : undefined
|
|
52
|
+
}
|
|
53
|
+
|
|
36
54
|
/** The prefix of every set installed for the app at `root`. */
|
|
37
55
|
export function installedPrefixes(root: string): string[] {
|
|
38
56
|
const found = new Set<string>()
|
package/src/svg-dir.ts
CHANGED
|
@@ -16,8 +16,14 @@ export interface SvgDirSet {
|
|
|
16
16
|
set: IconSet
|
|
17
17
|
/** Why a file is not an icon, keyed by the icon name it would have. */
|
|
18
18
|
problems: Map<string, string>
|
|
19
|
+
/** What a file holds that runs script once inlined, keyed by the icon name; the icon still loads. */
|
|
20
|
+
hazards: Map<string, string>
|
|
19
21
|
}
|
|
20
22
|
|
|
23
|
+
const SCRIPT_TAG_RE = /<script\b/i
|
|
24
|
+
const EVENT_ATTR_RE = /<[^>]*\son[a-z]+\s*=/i
|
|
25
|
+
const SCRIPT_URL_RE = /javascript\s*:/i
|
|
26
|
+
|
|
21
27
|
function attributes(source: string): Map<string, string> {
|
|
22
28
|
const found = new Map<string, string>()
|
|
23
29
|
for (const [, name, double, single] of source.matchAll(ATTR_RE))
|
|
@@ -30,6 +36,22 @@ function number(value: string | undefined): number | undefined {
|
|
|
30
36
|
return match === null ? undefined : Number(match[1])
|
|
31
37
|
}
|
|
32
38
|
|
|
39
|
+
/** What an SVG file's source holds that runs script once the icon is inlined in a page, undefined when it holds none. */
|
|
40
|
+
export function svgHazard(source: string): string | undefined {
|
|
41
|
+
const text = source.replace(COMMENT_RE, '')
|
|
42
|
+
if (SCRIPT_TAG_RE.test(text))
|
|
43
|
+
return 'a <script> element'
|
|
44
|
+
if (EVENT_ATTR_RE.test(text))
|
|
45
|
+
return 'an on* event attribute'
|
|
46
|
+
if (SCRIPT_URL_RE.test(text))
|
|
47
|
+
return 'a javascript: URL'
|
|
48
|
+
return undefined
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function escapeAttr(value: string): string {
|
|
52
|
+
return value.replaceAll('"', '"').replaceAll('<', '<').replaceAll('>', '>')
|
|
53
|
+
}
|
|
54
|
+
|
|
33
55
|
/** The icon drawn by an SVG file's source, or why it cannot be one. */
|
|
34
56
|
export function svgIcon(source: string): IconifyIcon | string {
|
|
35
57
|
const svg = SVG_RE.exec(source.replace(COMMENT_RE, ''))
|
|
@@ -41,7 +63,7 @@ export function svgIcon(source: string): IconifyIcon | string {
|
|
|
41
63
|
if (width === undefined || height === undefined || width <= 0 || height <= 0)
|
|
42
64
|
return 'has no viewBox, nor a width and height, to size it: add viewBox="0 0 <width> <height>" to its <svg>'
|
|
43
65
|
|
|
44
|
-
const paint = [...attrs].filter(([name]) => PAINT_ATTR_RE.test(name)).map(([name, value]) => ` ${name}="${value}"`).join('')
|
|
66
|
+
const paint = [...attrs].filter(([name]) => PAINT_ATTR_RE.test(name)).map(([name, value]) => ` ${name}="${escapeAttr(value)}"`).join('')
|
|
45
67
|
const body = svg[2]!.trim()
|
|
46
68
|
return { body: paint === '' ? body : `<g${paint}>${body}</g>`, left: left!, top: top!, width, height }
|
|
47
69
|
}
|
|
@@ -52,13 +74,19 @@ export async function readSvgDir(prefix: string, dir: string): Promise<SvgDirSet
|
|
|
52
74
|
return undefined
|
|
53
75
|
const set: IconSet = { prefix, icons: {} }
|
|
54
76
|
const problems = new Map<string, string>()
|
|
77
|
+
const hazards = new Map<string, string>()
|
|
55
78
|
for (const file of new Bun.Glob('*.svg').scanSync({ cwd: dir })) {
|
|
56
79
|
const name = file.slice(0, -'.svg'.length)
|
|
57
|
-
const
|
|
58
|
-
|
|
80
|
+
const source = await Bun.file(join(dir, file)).text()
|
|
81
|
+
const icon = svgIcon(source)
|
|
82
|
+
if (typeof icon === 'string') {
|
|
59
83
|
problems.set(name, icon)
|
|
60
|
-
|
|
61
|
-
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
set.icons[name] = icon
|
|
87
|
+
const hazard = svgHazard(source)
|
|
88
|
+
if (hazard !== undefined)
|
|
89
|
+
hazards.set(name, hazard)
|
|
62
90
|
}
|
|
63
|
-
return { set, problems }
|
|
91
|
+
return { set, problems, hazards }
|
|
64
92
|
}
|