Repository navigation
Expand file tree
/
Copy pathbuild.mjs
More file actions
417 lines (383 loc) · 13.5 KB
/
Copy pathbuild.mjs
File metadata and controls
417 lines (383 loc) · 13.5 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const root = path.dirname(fileURLToPath(import.meta.url));
const sourceDirectory = path.join(root, "src");
const outputDirectory = path.join(root, "dist");
const sourceAssetsDirectory = path.join(sourceDirectory, "assets");
const site = Object.freeze({
url: "https://webmaxru.github.io/",
title: "Maxim Salnikov | Projects and Experiments",
description:
"Explore selected projects by Maxim Salnikov, including useful web apps, developer tools, browser AI experiments, and inventive hackathon prototypes.",
author: "Maxim Salnikov",
language: "en",
locale: "en_US",
socialImagePath: "assets/brand/social-card.png",
portraitPath: "assets/brand/portrait-512.png",
themeColor: "#b11f4b",
backgroundColor: "#efebe4",
});
const projects = JSON.parse(
fs.readFileSync(path.join(root, "projects.json"), "utf8"),
);
const escapeHtml = (value) =>
String(value)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """)
.replaceAll("'", "'");
const escapeXml = escapeHtml;
const stringifyJsonForHtml = (value) =>
JSON.stringify(value, null, 2)
.replaceAll("<", "\\u003c")
.replaceAll(">", "\\u003e")
.replaceAll("&", "\\u0026");
const slugify = (value) =>
value
.toLowerCase()
.replaceAll(/[^a-z0-9]+/g, "-")
.replaceAll(/^-|-$/g, "");
const getInitials = (name) => {
const words = name.split(/\s+/).filter(Boolean);
if (words.length === 1) {
return words[0].slice(0, 2).toUpperCase();
}
return words
.slice(0, 2)
.map((word) => word[0])
.join("")
.toUpperCase();
};
const getThumbnailUrl = (project) => {
const thumbnail = project.thumbnail ?? "generated";
if (thumbnail === "generated") {
return thumbnail;
}
if (
path.isAbsolute(thumbnail) ||
thumbnail.startsWith("//") ||
/^[a-z][a-z\d+.-]*:/i.test(thumbnail)
) {
throw new Error(`${project.name} must use a local thumbnail asset.`);
}
const thumbnailPath = path.resolve(sourceDirectory, thumbnail);
const relativePath = path.relative(sourceDirectory, thumbnailPath);
if (relativePath.startsWith("..") || path.isAbsolute(relativePath)) {
throw new Error(`${project.name} thumbnail must stay inside src.`);
}
if (!fs.existsSync(thumbnailPath)) {
throw new Error(`${project.name} thumbnail not found: ${thumbnail}`);
}
return thumbnail.replaceAll("\\", "/");
};
const groupedProjects = projects.reduce((groups, project) => {
const group = groups.get(project.category) ?? [];
group.push(project);
groups.set(project.category, group);
return groups;
}, new Map());
const socialImageUrl = new URL(site.socialImagePath, site.url).href;
const portraitUrl = new URL(site.portraitPath, site.url).href;
const githubProfileUrl = "https://github.com/webmaxru";
const linkedinProfileUrl = "https://www.linkedin.com/in/webmax/";
const categoryNavigation = [...groupedProjects.entries()]
.map(
([category, categoryProjects], index) => `
<a class="category-link" href="#${slugify(category)}">
<span class="category-number">${String(index + 1).padStart(2, "0")}</span>
<span>${escapeHtml(category)}</span>
<span class="category-count">${categoryProjects.length}</span>
</a>`,
)
.join("");
const structuredData = {
"@context": "https://schema.org",
"@graph": [
{
"@type": "Person",
"@id": `${site.url}#person`,
name: site.author,
url: site.url,
image: portraitUrl,
sameAs: [githubProfileUrl, linkedinProfileUrl],
},
{
"@type": "WebSite",
"@id": `${site.url}#website`,
url: site.url,
name: site.title,
description: site.description,
inLanguage: site.language,
image: socialImageUrl,
author: {
"@id": `${site.url}#person`,
},
publisher: {
"@id": `${site.url}#person`,
},
},
],
};
const projectMarkdownLink = (project) => {
const repositoryUrl = `https://github.com/${project.repo}`;
const projectUrl = project.live ?? repositoryUrl;
return `- [${project.name}](${projectUrl}): ${project.description} Source: [GitHub](${repositoryUrl}).`;
};
const llmsText = [
`# ${site.title}`,
"",
`> ${site.description}`,
"",
"This is the personal project portfolio of Maxim Salnikov. The site is a static, server-rendered catalog organized by project category.",
"",
"## Main pages",
"",
`- [Portfolio](${site.url}): Browse all ${projects.length} projects and experiments.`,
`- [Full project catalog](${new URL("llms-full.txt", site.url).href}): Read every project description and authoritative destination in one document.`,
"",
"## Project categories",
"",
...[...groupedProjects.entries()].map(
([category, categoryProjects]) =>
`- [${category}](${site.url}#${slugify(category)}): ${categoryProjects.length} ${categoryProjects.length === 1 ? "project" : "projects"}.`,
),
"",
"## Authoritative profiles",
"",
`- [GitHub](${githubProfileUrl}): Source repositories and ongoing work by Maxim Salnikov.`,
`- [LinkedIn](${linkedinProfileUrl}): Professional profile for Maxim Salnikov.`,
"",
].join("\n");
const llmsFullText = [
`# ${site.title} - Full project catalog`,
"",
`> ${site.description}`,
"",
"Maxim Salnikov builds web applications, developer tools, browser AI experiments, and hackathon prototypes. The following catalog mirrors the human-readable portfolio.",
"",
...[...groupedProjects.entries()].flatMap(([category, categoryProjects]) => [
`## ${category}`,
"",
...categoryProjects.flatMap((project) => [
`### ${project.name}`,
"",
project.description,
"",
`- Project: ${project.live ?? `https://github.com/${project.repo}`}`,
`- Source: https://github.com/${project.repo}`,
"",
]),
]),
"## Authoritative profiles",
"",
`- GitHub: ${githubProfileUrl}`,
`- LinkedIn: ${linkedinProfileUrl}`,
"",
].join("\n");
const aiCrawlers = [
"GPTBot",
"OAI-SearchBot",
"ChatGPT-User",
"ClaudeBot",
"Claude-User",
"anthropic-ai",
"PerplexityBot",
"Perplexity-User",
"Google-Extended",
"Applebot-Extended",
"CCBot",
"cohere-ai",
"Amazonbot",
"meta-externalagent",
];
const robotsText = [
"User-agent: *",
"Allow: /",
"",
...aiCrawlers.flatMap((crawler) => [
`User-agent: ${crawler}`,
"Allow: /",
"",
]),
`Sitemap: ${new URL("sitemap.xml", site.url).href}`,
"",
].join("\n");
const buildDate = new Date().toISOString().slice(0, 10);
const sitemapXml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>${escapeXml(site.url)}</loc>
<lastmod>${buildDate}</lastmod>
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>
</urlset>
`;
const manifest = {
name: site.title,
short_name: "Maxim Builds",
description: site.description,
id: "/",
start_url: "/",
scope: "/",
display: "standalone",
background_color: site.backgroundColor,
theme_color: site.themeColor,
lang: site.language,
dir: "ltr",
categories: ["portfolio", "developer", "productivity"],
icons: [
{
src: "/assets/brand/icon-192.png",
sizes: "192x192",
type: "image/png",
purpose: "any",
},
{
src: "/assets/brand/icon-512.png",
sizes: "512x512",
type: "image/png",
purpose: "any",
},
{
src: "/assets/brand/icon-maskable-512.png",
sizes: "512x512",
type: "image/png",
purpose: "maskable",
},
],
};
const renderGitHubIcon = () => `
<svg viewBox="0 0 24 24" aria-hidden="true">
<path fill="currentColor" d="M12 .75a11.25 11.25 0 0 0-3.56 21.92c.56.1.77-.24.77-.54v-2.1c-3.14.68-3.8-1.34-3.8-1.34-.51-1.3-1.25-1.65-1.25-1.65-1.02-.7.08-.68.08-.68 1.13.08 1.73 1.16 1.73 1.16 1 1.72 2.62 1.22 3.26.93.1-.72.4-1.22.71-1.5-2.5-.28-5.13-1.25-5.13-5.56 0-1.23.44-2.23 1.16-3.02-.12-.28-.5-1.43.11-2.98 0 0 .95-.3 3.1 1.15a10.75 10.75 0 0 1 5.64 0c2.15-1.46 3.1-1.15 3.1-1.15.61 1.55.23 2.7.11 2.98.72.79 1.16 1.79 1.16 3.02 0 4.32-2.63 5.28-5.14 5.56.41.36.76 1.08.76 2.18v3.24c0 .3.2.65.78.54A11.25 11.25 0 0 0 12 .75Z"/>
</svg>`;
const renderStarIcon = () => `
<svg viewBox="0 0 24 24" aria-hidden="true">
<path fill="currentColor" d="m12 2.4 2.96 6 6.62.96-4.79 4.67 1.13 6.6L12 17.51l-5.92 3.12 1.13-6.6-4.79-4.67 6.62-.96L12 2.4Z"/>
</svg>`;
const renderProjectCard = (project) => {
const repositoryUrl = `https://github.com/${project.repo}`;
const thumbnailUrl = getThumbnailUrl(project);
const useGeneratedThumbnail = thumbnailUrl === "generated";
const liveUrl = project.live ?? repositoryUrl;
const liveLabel = project.liveLabel ?? "Open live project";
const fallbackLabel = getInitials(project.name);
const thumbClass = useGeneratedThumbnail
? "project-thumb has-fallback"
: "project-thumb";
const imageMarkup = useGeneratedThumbnail
? ""
: `
<img
src="${escapeHtml(thumbnailUrl)}"
alt="${escapeHtml(project.name)} thumbnail"
loading="lazy"
decoding="async"
onerror="this.hidden=true;this.parentElement.classList.add('has-fallback')"
/>`;
return `
<article class="project-card">
<div class="${thumbClass}">
<div class="thumb-fallback" aria-hidden="true">
<span class="thumb-mark">${escapeHtml(fallbackLabel)}</span>
<span class="thumb-fallback-name">${escapeHtml(project.name)}</span>
</div>
${imageMarkup}
<span class="thumb-label">${escapeHtml(project.category)}</span>
</div>
<div class="card-body">
<div class="card-title-row">
<h3>${escapeHtml(project.name)}</h3>
<span class="repo-name">${escapeHtml(project.repo.split("/").at(-1))}</span>
</div>
<p>${escapeHtml(project.description)}</p>
<div class="card-links">
<a class="live-link" href="${escapeHtml(liveUrl)}" target="_blank" rel="noopener noreferrer">
<span>${escapeHtml(liveLabel)}</span>
<span aria-hidden="true">→</span>
</a>
<div class="card-actions">
${
project.stars
? `<span class="star-count" aria-label="${escapeHtml(project.stars)} GitHub stars">
${renderStarIcon()}
<span>${escapeHtml(project.stars)} stars</span>
</span>`
: ""
}
<a class="github-link" href="${escapeHtml(repositoryUrl)}" target="_blank" rel="noopener noreferrer" aria-label="View ${escapeHtml(project.name)} on GitHub">
${renderGitHubIcon()}
</a>
</div>
</div>
</div>
</article>`;
};
const projectSections = [...groupedProjects.entries()]
.map(
([category, categoryProjects]) => `
<section class="project-section" id="${slugify(category)}" aria-labelledby="${slugify(category)}-heading">
<div class="section-heading">
<span class="section-index">${String([...groupedProjects.keys()].indexOf(category) + 1).padStart(2, "0")}</span>
<h2 id="${slugify(category)}-heading">${escapeHtml(category)}</h2>
<span class="section-rule" aria-hidden="true"></span>
<span class="section-count">${categoryProjects.length} ${categoryProjects.length === 1 ? "project" : "projects"}</span>
</div>
<div class="project-grid">
${categoryProjects.map(renderProjectCard).join("")}
</div>
</section>`,
)
.join("");
const template = fs.readFileSync(
path.join(sourceDirectory, "template.html"),
"utf8",
);
const output = template
.replaceAll("{{SITE_TITLE}}", escapeHtml(site.title))
.replaceAll("{{SITE_DESCRIPTION}}", escapeHtml(site.description))
.replaceAll("{{SITE_URL}}", escapeHtml(site.url))
.replaceAll("{{SOCIAL_IMAGE_URL}}", escapeHtml(socialImageUrl))
.replaceAll("{{STRUCTURED_DATA}}", stringifyJsonForHtml(structuredData))
.replaceAll("{{CATEGORY_NAVIGATION}}", categoryNavigation)
.replaceAll("{{PROJECT_SECTIONS}}", projectSections);
const requiredBrandAssets = [
"favicon.svg",
"favicon.ico",
"apple-touch-icon.png",
"icon-192.png",
"icon-512.png",
"icon-maskable-512.png",
"portrait-512.png",
"social-card.png",
];
for (const asset of requiredBrandAssets) {
const assetPath = path.join(sourceAssetsDirectory, "brand", asset);
if (!fs.existsSync(assetPath)) {
throw new Error(`Required brand asset not found: ${assetPath}`);
}
}
fs.rmSync(outputDirectory, { recursive: true, force: true });
fs.mkdirSync(outputDirectory, { recursive: true });
fs.writeFileSync(path.join(outputDirectory, "index.html"), output);
fs.copyFileSync(
path.join(sourceDirectory, "404.html"),
path.join(outputDirectory, "404.html"),
);
fs.copyFileSync(path.join(root, ".nojekyll"), path.join(outputDirectory, ".nojekyll"));
// ads.txt must sit at the domain root (https://salnikov.no/ads.txt) for Google AdSense.
fs.copyFileSync(path.join(root, "ads.txt"), path.join(outputDirectory, "ads.txt"));
fs.cpSync(sourceAssetsDirectory, path.join(outputDirectory, "assets"), {
recursive: true,
});
fs.writeFileSync(
path.join(outputDirectory, "site.webmanifest"),
`${JSON.stringify(manifest, null, 2)}\n`,
);
fs.writeFileSync(path.join(outputDirectory, "robots.txt"), robotsText);
fs.writeFileSync(path.join(outputDirectory, "sitemap.xml"), sitemapXml);
fs.writeFileSync(path.join(outputDirectory, "llms.txt"), llmsText);
fs.writeFileSync(path.join(outputDirectory, "llms-full.txt"), llmsFullText);
console.log(`Built ${projects.length} projects across ${groupedProjects.size} categories.`);