@kayjs/icons 2.0.0-beta.1 → 2.0.0-beta.3
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/package.json +3 -2
- package/src/index.ts +29 -2
- package/src/name-words.ts +7 -0
- package/src/search.ts +121 -0
- package/src/spelling.ts +72 -0
- package/src/table.ts +41 -0
- package/src/words.json +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kayjs/icons",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.3",
|
|
4
4
|
"description": "Compile-time Iconify SVG icon plugin (~icons/<set>/<name>) for Kay v2",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
],
|
|
11
11
|
"exports": {
|
|
12
12
|
".": "./src/index.ts",
|
|
13
|
+
"./search": "./src/search.ts",
|
|
13
14
|
"./ambient": "./src/ambient.d.ts"
|
|
14
15
|
},
|
|
15
16
|
"engines": {
|
|
@@ -21,6 +22,6 @@
|
|
|
21
22
|
"directory": "packages/plugin-icons"
|
|
22
23
|
},
|
|
23
24
|
"peerDependencies": {
|
|
24
|
-
"@kayjs/kay": "^2.0.0-beta.
|
|
25
|
+
"@kayjs/kay": "^2.0.0-beta.3"
|
|
25
26
|
}
|
|
26
27
|
}
|
package/src/index.ts
CHANGED
|
@@ -5,12 +5,15 @@
|
|
|
5
5
|
import type { BunPlugin } from 'bun'
|
|
6
6
|
import type { KayPlugin } from '@kayjs/kay/server'
|
|
7
7
|
import type { IconData, IconifyIcon, IconSet } from './types'
|
|
8
|
+
import { join } from 'node:path'
|
|
9
|
+
import { definePlugin } from '@kayjs/kay/server'
|
|
8
10
|
|
|
9
11
|
export { Icon } from './Icon'
|
|
10
12
|
export type { IconComponentProps } from './Icon'
|
|
11
13
|
export type { IconData, IconifyIcon, IconSet } from './types'
|
|
12
14
|
|
|
13
15
|
const DEFAULT_SIZE = 16
|
|
16
|
+
const FOUND_PER_SET = 10
|
|
14
17
|
const ICON_SPEC_RE = /^~icons\/([^/]+)\/(.+)$/
|
|
15
18
|
|
|
16
19
|
function resolveIconSetFile(prefix: string, root: string): string {
|
|
@@ -84,7 +87,7 @@ export interface IconsPluginOptions {
|
|
|
84
87
|
export function icons(options: IconsPluginOptions = {}): KayPlugin {
|
|
85
88
|
const cache = new Map<string, IconSet>(Object.entries(options.sets ?? {}))
|
|
86
89
|
|
|
87
|
-
return {
|
|
90
|
+
return definePlugin({
|
|
88
91
|
name: '@kayjs/icons',
|
|
89
92
|
bunPlugins: ({ root }): BunPlugin[] => [{
|
|
90
93
|
name: 'kayjs-icons',
|
|
@@ -116,5 +119,29 @@ export function icons(options: IconsPluginOptions = {}): KayPlugin {
|
|
|
116
119
|
})
|
|
117
120
|
},
|
|
118
121
|
}],
|
|
119
|
-
|
|
122
|
+
commands: {
|
|
123
|
+
// Prints the `~icons/<set>/<name>` specifiers of the installed sets matching the words, so an agent imports icons that exist.
|
|
124
|
+
'icons-find': async (args, root) => {
|
|
125
|
+
const query = args.join(' ')
|
|
126
|
+
if (query.trim() === '') {
|
|
127
|
+
console.error('Usage: kay icons-find <words>')
|
|
128
|
+
process.exit(1)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const files = [...new Bun.Glob('node_modules/@iconify-json/*/icons.json').scanSync({ cwd: root, followSymlinks: true })].sort()
|
|
132
|
+
if (files.length === 0) {
|
|
133
|
+
console.error('No icon set installed. Install one, such as @iconify-json/tabler.')
|
|
134
|
+
process.exit(1)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Imported here so the word table never reaches a bundle that only draws icons.
|
|
138
|
+
const { findIcons } = await import('./search')
|
|
139
|
+
for (const file of files) {
|
|
140
|
+
const set = await Bun.file(join(root, file)).json() as IconSet
|
|
141
|
+
for (const name of findIcons(set, query, { limit: FOUND_PER_SET }))
|
|
142
|
+
console.log(`~icons/${set.prefix}/${name}`)
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
})
|
|
120
147
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
const SPLIT = /[^a-z0-9]+/
|
|
2
|
+
const DIGITS = /^\d+$/
|
|
3
|
+
|
|
4
|
+
// The words of an icon name or a query, `arrow-left-2` giving `arrow` and `left`.
|
|
5
|
+
export function nameWords(text: string): string[] {
|
|
6
|
+
return text.toLowerCase().split(SPLIT).filter(word => word.length > 1 && !DIGITS.test(word))
|
|
7
|
+
}
|
package/src/search.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { nameWords } from './name-words'
|
|
2
|
+
import { closeWords, completions } from './spelling'
|
|
3
|
+
import { relatedWords, tableWords } from './table'
|
|
4
|
+
|
|
5
|
+
// An iconify set as `@iconify-json/<prefix>/icons.json` holds it, of which only the names of its icons and aliases are read.
|
|
6
|
+
export interface IconifyNames {
|
|
7
|
+
icons: Record<string, unknown>
|
|
8
|
+
aliases?: Record<string, unknown> | undefined
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface IconPage {
|
|
12
|
+
limit?: number | undefined
|
|
13
|
+
offset?: number | undefined
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface Index {
|
|
17
|
+
names: readonly string[]
|
|
18
|
+
byWord: Map<string, string[]>
|
|
19
|
+
wordCount: Map<string, number>
|
|
20
|
+
order: Map<string, number>
|
|
21
|
+
// The words a typed word may be completed or respelled into: the names' own and the table's.
|
|
22
|
+
vocabulary: string[]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const SPACES = /\s+/g
|
|
26
|
+
// A word typed as given counts twice what a related word, a completion or a respelling brings.
|
|
27
|
+
const OWN_WEIGHT = 2
|
|
28
|
+
const COMPLETION_WEIGHT = 0.7
|
|
29
|
+
const RESPELLING_WEIGHT = 0.8
|
|
30
|
+
|
|
31
|
+
// One index per names array or set object, so a set imported once is indexed once.
|
|
32
|
+
const indexes = new WeakMap<readonly string[] | IconifyNames, Index>()
|
|
33
|
+
|
|
34
|
+
function indexOf(source: readonly string[] | IconifyNames): Index {
|
|
35
|
+
const known = indexes.get(source)
|
|
36
|
+
|
|
37
|
+
if (known)
|
|
38
|
+
return known
|
|
39
|
+
|
|
40
|
+
const names = isNames(source) ? source : [...Object.keys(source.icons), ...Object.keys(source.aliases ?? {})]
|
|
41
|
+
const byWord = new Map<string, string[]>()
|
|
42
|
+
const wordCount = new Map<string, number>()
|
|
43
|
+
|
|
44
|
+
for (const name of names) {
|
|
45
|
+
const words = new Set(nameWords(name))
|
|
46
|
+
|
|
47
|
+
wordCount.set(name, words.size)
|
|
48
|
+
|
|
49
|
+
for (const word of words) {
|
|
50
|
+
const list = byWord.get(word)
|
|
51
|
+
|
|
52
|
+
if (list)
|
|
53
|
+
list.push(name)
|
|
54
|
+
else
|
|
55
|
+
byWord.set(word, [name])
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const index = { names, byWord, wordCount, order: new Map(names.map((name, i) => [name, i])), vocabulary: [...new Set([...byWord.keys(), ...tableWords()])] }
|
|
60
|
+
|
|
61
|
+
indexes.set(source, index)
|
|
62
|
+
|
|
63
|
+
return index
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function isNames(source: readonly string[] | IconifyNames): source is readonly string[] {
|
|
67
|
+
return Array.isArray(source)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// What a typed word stands for: itself when the names or the table know it, else its completions and close spellings.
|
|
71
|
+
function readings(word: string, index: Index): [string, number][] {
|
|
72
|
+
if (index.byWord.has(word) || relatedWords(word).length > 0)
|
|
73
|
+
return [[word, 1]]
|
|
74
|
+
|
|
75
|
+
const known = (candidate: string) => index.byWord.get(candidate)?.length ?? 0
|
|
76
|
+
|
|
77
|
+
return [
|
|
78
|
+
...completions(word, index.vocabulary, known).map(found => [found, COMPLETION_WEIGHT] as [string, number]),
|
|
79
|
+
...closeWords(word, index.vocabulary).map(found => [found, RESPELLING_WEIGHT] as [string, number]),
|
|
80
|
+
]
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function literalRank(name: string, literal: string): number {
|
|
84
|
+
if (name === literal)
|
|
85
|
+
return 0
|
|
86
|
+
|
|
87
|
+
return name.startsWith(literal) ? 1 : 2
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// A page of the icon names of a set, or of a list of names, matching `query`: the names holding it first (the exact name, then names starting with it, then the rest), then the icons a related word, a completion or a respelling names, best first.
|
|
91
|
+
export function findIcons(source: readonly string[] | IconifyNames, query: string, page: IconPage = {}): string[] {
|
|
92
|
+
const { limit = Infinity, offset = 0 } = page
|
|
93
|
+
const needle = query.trim().toLowerCase()
|
|
94
|
+
const index = indexOf(source)
|
|
95
|
+
const { names } = index
|
|
96
|
+
const literal = needle.replace(SPACES, '-')
|
|
97
|
+
const literals = names.filter(name => name.includes(literal)).sort((a, b) => literalRank(a, literal) - literalRank(b, literal))
|
|
98
|
+
const scores = new Map<string, number>()
|
|
99
|
+
|
|
100
|
+
const credit = (word: string, weight: number) => {
|
|
101
|
+
for (const name of index.byWord.get(word) ?? [])
|
|
102
|
+
scores.set(name, (scores.get(name) ?? 0) + weight / index.wordCount.get(name)!)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
for (const word of nameWords(needle)) {
|
|
106
|
+
for (const [reading, weight] of readings(word, index)) {
|
|
107
|
+
credit(reading, weight * OWN_WEIGHT)
|
|
108
|
+
|
|
109
|
+
for (const [related, strength] of relatedWords(reading))
|
|
110
|
+
credit(related, weight * strength)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const shown = new Set(literals)
|
|
115
|
+
const related = [...scores]
|
|
116
|
+
.filter(([name]) => !shown.has(name))
|
|
117
|
+
.sort((a, b) => b[1] - a[1] || index.order.get(a[0])! - index.order.get(b[0])!)
|
|
118
|
+
.map(([name]) => name)
|
|
119
|
+
|
|
120
|
+
return [...literals, ...related].slice(offset, offset + limit)
|
|
121
|
+
}
|
package/src/spelling.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// A word this long or more is completed or respelled; shorter ones match too much.
|
|
2
|
+
const SHORTEST_COMPLETED = 3
|
|
3
|
+
const SHORTEST_RESPELLED = 4
|
|
4
|
+
// From this length a word may be two edits off, else one.
|
|
5
|
+
const TWO_EDITS_FROM = 8
|
|
6
|
+
const MOST = 3
|
|
7
|
+
|
|
8
|
+
// The known words starting with `word`, the most used first, then the shortest.
|
|
9
|
+
export function completions(word: string, vocabulary: Iterable<string>, uses: (word: string) => number): string[] {
|
|
10
|
+
if (word.length < SHORTEST_COMPLETED)
|
|
11
|
+
return []
|
|
12
|
+
|
|
13
|
+
const found: string[] = []
|
|
14
|
+
|
|
15
|
+
for (const candidate of vocabulary) {
|
|
16
|
+
if (candidate.startsWith(word))
|
|
17
|
+
found.push(candidate)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return found.sort((a, b) => uses(b) - uses(a) || a.length - b.length).slice(0, MOST)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// The known words one edit from `word` (two for a long one), a swap of neighbour letters counting as one, the closest first.
|
|
24
|
+
export function closeWords(word: string, vocabulary: Iterable<string>): string[] {
|
|
25
|
+
if (word.length < SHORTEST_RESPELLED)
|
|
26
|
+
return []
|
|
27
|
+
|
|
28
|
+
const most = word.length >= TWO_EDITS_FROM ? 2 : 1
|
|
29
|
+
const found: [string, number][] = []
|
|
30
|
+
|
|
31
|
+
for (const candidate of vocabulary) {
|
|
32
|
+
if (Math.abs(candidate.length - word.length) > most)
|
|
33
|
+
continue
|
|
34
|
+
|
|
35
|
+
const edits = distance(word, candidate, most)
|
|
36
|
+
|
|
37
|
+
if (edits <= most)
|
|
38
|
+
found.push([candidate, edits])
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return found.sort((a, b) => a[1] - b[1]).slice(0, MOST).map(([candidate]) => candidate)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Damerau-Levenshtein distance (optimal string alignment), giving up past `most` once a whole row is above it.
|
|
45
|
+
export function distance(a: string, b: string, most = Infinity): number {
|
|
46
|
+
let before: number[] = []
|
|
47
|
+
let previous = Array.from({ length: b.length + 1 }, (_, j) => j)
|
|
48
|
+
|
|
49
|
+
for (let i = 1; i <= a.length; i++) {
|
|
50
|
+
const row = [i]
|
|
51
|
+
let lowest = i
|
|
52
|
+
|
|
53
|
+
for (let j = 1; j <= b.length; j++) {
|
|
54
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1
|
|
55
|
+
let value = Math.min(previous[j]! + 1, row[j - 1]! + 1, previous[j - 1]! + cost)
|
|
56
|
+
|
|
57
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1])
|
|
58
|
+
value = Math.min(value, before[j - 2]! + 1)
|
|
59
|
+
|
|
60
|
+
row.push(value)
|
|
61
|
+
lowest = Math.min(lowest, value)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (lowest > most)
|
|
65
|
+
return lowest
|
|
66
|
+
|
|
67
|
+
before = previous
|
|
68
|
+
previous = row
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return previous[b.length]!
|
|
72
|
+
}
|
package/src/table.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// words.json maps a word to its related icon-name words as `word percent word percent`, built by build/table.ts; read on the first search, as it weighs about 2 MB.
|
|
2
|
+
let table: Record<string, string> | undefined
|
|
3
|
+
const decoded = new Map<string, [string, number][]>()
|
|
4
|
+
const PERCENT = 100
|
|
5
|
+
|
|
6
|
+
function load(): Record<string, string> {
|
|
7
|
+
// A synchronous lazy load keeps findIcons synchronous.
|
|
8
|
+
table ??= require('./words.json') as Record<string, string>
|
|
9
|
+
|
|
10
|
+
return table
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Swaps the table for one built in memory, as the bench scores tables learned without the set they are tested on.
|
|
14
|
+
export function replaceTable(next: Record<string, string>): void {
|
|
15
|
+
table = next
|
|
16
|
+
decoded.clear()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// The words the table knows, each a word a search may be typed with.
|
|
20
|
+
export function tableWords(): string[] {
|
|
21
|
+
return Object.keys(load())
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// The icon-name words related to `word`, each with its strength from 0 to 1, the strongest first.
|
|
25
|
+
export function relatedWords(word: string): [string, number][] {
|
|
26
|
+
const known = decoded.get(word)
|
|
27
|
+
|
|
28
|
+
if (known)
|
|
29
|
+
return known
|
|
30
|
+
|
|
31
|
+
const line = Object.hasOwn(load(), word) ? load()[word]! : ''
|
|
32
|
+
const parts = line === '' ? [] : line.split(' ')
|
|
33
|
+
const related: [string, number][] = []
|
|
34
|
+
|
|
35
|
+
for (let i = 0; i + 1 < parts.length; i += 2)
|
|
36
|
+
related.push([parts[i]!, Number(parts[i + 1]) / PERCENT])
|
|
37
|
+
|
|
38
|
+
decoded.set(word, related)
|
|
39
|
+
|
|
40
|
+
return related
|
|
41
|
+
}
|