forked from tandpfun/skill-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
197 lines (184 loc) · 4.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const icons = require('./dist/icons.json');
const iconNameList = [...new Set(Object.keys(icons).map(i => i.split('-')[0]))];
const shortNames = {
js: 'javascript',
ts: 'typescript',
py: 'python',
tailwind: 'tailwindcss',
vue: 'vuejs',
nuxt: 'nuxtjs',
go: 'golang',
cf: 'cloudflare',
wasm: 'webassembly',
postgres: 'postgresql',
k8s: 'kubernetes',
next: 'nextjs',
mongo: 'mongodb',
md: 'markdown',
ps: 'photoshop',
ai: 'illustrator',
pr: 'premiere',
ae: 'aftereffects',
scss: 'sass',
sc: 'scala',
net: 'dotnet',
gatsbyjs: 'gatsby',
gql: 'graphql',
vlang: 'v',
amazonwebservices: 'aws',
bots: 'discordbots',
express: 'expressjs',
googlecloud: 'gcp',
mui: 'materialui',
windi: 'windicss',
unreal: 'unrealengine',
nest: 'nestjs',
};
const themedIcons = [
'nodejs',
'python',
'tailwindcss',
'vuejs',
'nuxtjs',
'figma',
'react',
'cloudflare',
'java',
'php',
'kotlin',
'dart',
'mysql',
'postgresql',
'redis',
'angular',
'deno',
'vim',
'nextjs',
'grafana',
'clojure',
'coffeescript',
'lua',
'markdown',
'r',
'unity',
'zig',
'workers',
'linux',
'jenkins',
'bash',
'haxe',
'godot',
'scala',
'regex',
'firebase',
'graphql',
'latex',
'supabase',
'v',
'vscode',
'svg',
'activitypub',
'aiscript',
'autocad',
'aws',
'azure',
'blender',
'bsd',
'eclipse',
'emotion',
'expressjs',
'flutter',
'gcp',
'haxeflixel',
'idea',
'laravel',
'nestjs',
'materialui',
'nim',
'plan9',
'spring',
'styledcomponents',
'windicss',
];
const ICONS_PER_LINE = 15;
const ONE_ICON = 48;
const SCALE = ONE_ICON / (300 - 44);
function generateSvg(iconNames) {
const iconSvgList = iconNames.map(i => icons[i]);
const length = Math.min(ICONS_PER_LINE * 300, iconNames.length * 300) - 44;
const height = Math.ceil(iconSvgList.length / ICONS_PER_LINE) * 300 - 44;
const scaledHeight = height * SCALE;
const scaledWidth = length * SCALE;
return `
<svg width="${scaledWidth}" height="${scaledHeight}" viewBox="0 0 ${length} ${height}" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
${iconSvgList
.map(
(i, index) =>
`
<g transform="translate(${(index % ICONS_PER_LINE) * 300}, ${
Math.floor(index / ICONS_PER_LINE) * 300
})">
${i}
</g>
`
)
.join(' ')}
</svg>
`;
}
function parseShortNames(names, theme = 'dark') {
return names.map(name => {
if (iconNameList.includes(name))
return name + (themedIcons.includes(name) ? `-${theme}` : '');
else if (name in shortNames)
return (
shortNames[name] +
(themedIcons.includes(shortNames[name]) ? `-${theme}` : '')
);
});
}
async function handleRequest(request) {
const { pathname, searchParams } = new URL(request.url);
const path = pathname.replace(/^\/|\/$/g, '');
if (path === 'icons') {
const iconParam = searchParams.get('i') || searchParams.get('icons');
if (!iconParam)
return new Response("You didn't specify any icons!", { status: 400 });
const theme = searchParams.get('t') || searchParams.get('theme');
if (theme && theme !== 'dark' && theme !== 'light')
return new Response('Theme must be either "light" or "dark"', {
status: 400,
});
let iconShortNames = [];
if (iconParam === 'all') iconShortNames = iconNameList;
else iconShortNames = iconParam.split(',');
const iconNames = parseShortNames(iconShortNames, theme || undefined);
if (!iconNames)
return new Response("You didn't format the icons param correctly!", {
status: 400,
});
const svg = generateSvg(iconNames);
return new Response(svg, { headers: { 'Content-Type': 'image/svg+xml' } });
} else if (path === 'api/icons') {
return new Response(JSON.stringify(iconNameList), {
headers: {
'content-type': 'application/json;charset=UTF-8',
},
});
} else if (path === 'api/svgs') {
return new Response(JSON.stringify(icons), {
headers: {
'content-type': 'application/json;charset=UTF-8',
},
});
} else {
return fetch(request);
}
}
addEventListener('fetch', event => {
event.respondWith(
handleRequest(event.request).catch(
err => new Response(err.stack, { status: 500 })
)
);
});