-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
index.js
149 lines (136 loc) · 4.09 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
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',
ktorio: 'ktor',
pwsh: 'powershell',
au: 'audition',
rollup: 'rollupjs',
rxjs: 'reactivex',
rxjava: 'reactivex',
ghactions: 'githubactions',
sklearn: 'scikitlearn',
};
const themedIcons = [
...Object.keys(icons)
.filter(i => i.includes('-light') || i.includes('-dark'))
.map(i => i.split('-')[0]),
];
const ICONS_PER_LINE = 15;
const ONE_ICON = 48;
const SCALE = ONE_ICON / (300 - 44);
function generateSvg(iconNames, perLine) {
const iconSvgList = iconNames.map(i => icons[i]);
const length = Math.min(perLine * 300, iconNames.length * 300) - 44;
const height = Math.ceil(iconSvgList.length / perLine) * 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 % perLine) * 300}, ${
Math.floor(index / perLine) * 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,
});
const perLine = searchParams.get('perline') || ICONS_PER_LINE;
if (isNaN(perLine) || perLine < -1 || perLine > 50)
return new Response('Icons per line must be a number between 1 and 50', {
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, perLine);
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 })
)
);
});