-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathseo-plugin.js
More file actions
494 lines (447 loc) · 22.8 KB
/
Copy pathseo-plugin.js
File metadata and controls
494 lines (447 loc) · 22.8 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import { createClient } from '@sanity/client';
const sanityClient = createClient({
projectId: 'kv5wjjmj',
dataset: 'production',
useCdn: true,
apiVersion: '2024-03-01',
});
// Tech stack filename -> human-readable name mapping for JSON-LD
const TECH_STACK_NAMES = {
'reactlogo.webp': 'React',
'htmllogo.webp': 'HTML',
'csslogo.webp': 'CSS',
'jslogo.webp': 'JavaScript',
'tailwindlogo.webp': 'Tailwind CSS',
'firebaselogo.webp': 'Firebase',
'netlifylogo.webp': 'Netlify',
'wordpresslogo.webp': 'WordPress',
'elementorlogo.webp': 'Elementor',
'phplogo.webp': 'PHP',
};
/**
* Helper to ensure dates are in ISO-8601 format with timezone for SEO.
*/
function formatIsoDate(dateString) {
if (!dateString) return undefined;
if (dateString.includes('T')) return dateString; // Already has time/timezone
return `${dateString}T12:00:00Z`; // Default to noon UTC
}
/**
* Build dynamic JSON-LD structured data from Sanity content.
* This generates schema.org entities that AI search engines (Google AI Overviews,
* Perplexity, Gemini) use to understand and cite content in their answers.
*/
function buildJsonLd(globalInfo, projects, studio, awards, faqList) {
const graph = [];
// --- 1. Person: Central node of the Knowledge Graph ---
const person = {
'@type': 'Person',
'@id': 'https://itomdev.com/#person',
name: 'Tomasz Szmajda',
alternateName: ['ITom', 'ITom Dev', 'Tomasz ITom Szmajda'],
url: 'https://itomdev.com',
jobTitle: 'Creative Frontend Developer',
description: globalInfo?.aboutMe || 'Creative developer specializing in 3D web experiences.',
knowsAbout: ['React', 'Three.js', 'JavaScript', 'TypeScript', 'GSAP', 'Next.js', 'WebGL', '3D Graphics', 'Web Development'],
sameAs: [
globalInfo?.linkedinUrl,
globalInfo?.githubUrl,
globalInfo?.instagramUrl,
globalInfo?.xUrl,
globalInfo?.tiktokUrl,
globalInfo?.youtubeUrl
].filter(Boolean)
};
graph.push(person);
// --- 2. WebSite ---
const website = {
'@type': 'WebSite',
'@id': 'https://itomdev.com/#website',
url: 'https://itomdev.com',
name: globalInfo?.siteTitle || 'Tomasz "ITom" Szmajda | Creative 3D Portfolio',
description: globalInfo?.siteDescription || 'Interactive 3D Developer Portfolio by Tomasz Szmajda',
publisher: { '@id': 'https://itomdev.com/#person' }
};
graph.push(website);
// --- 3. ProfilePage ---
const profilePage = {
'@type': 'ProfilePage',
'@id': 'https://itomdev.com/#profilepage',
url: 'https://itomdev.com',
mainEntity: { '@id': 'https://itomdev.com/#person' },
about: { '@id': 'https://itomdev.com/#person' }
};
graph.push(profilePage);
// --- 4. FAQPage (GEO & AI search engine optimizer) ---
if (faqList && faqList.length > 0) {
const faqPage = {
'@type': 'FAQPage',
'@id': 'https://itomdev.com/#faq',
mainEntity: faqList.map(item => ({
'@type': 'Question',
name: item.question,
acceptedAnswer: {
'@type': 'Answer',
text: item.answer
}
}))
};
graph.push(faqPage);
}
// --- 5. ItemList: Portfolio Projects (Google rich results for lists) ---
if (projects && projects.length > 0) {
graph.push({
'@type': 'ItemList',
'@id': 'https://itomdev.com/#projectslist',
name: 'Portfolio Projects by Tomasz "ITom" Szmajda',
description: 'Selected web development projects showcasing React, Three.js, and creative frontend engineering.',
numberOfItems: projects.length,
itemListElement: projects.map((p, i) => ({
'@type': 'ListItem',
position: i + 1,
item: {
'@type': 'CreativeWork',
name: p.seoTitle || p.title,
description: p.seoDescription || p.description || '',
url: p.url || undefined,
creator: { '@id': 'https://itomdev.com/#person' },
...(p.techStack && p.techStack.length > 0 ? {
keywords: p.techStack.map(t => TECH_STACK_NAMES[t] || t).join(', ')
} : {}),
}
}))
});
// Individual CreativeWork entries for each project (richer detail)
projects.forEach(p => {
const projectSlug = p.title.toLowerCase().replace(/[^a-z0-9]+/g, '-');
graph.push({
'@type': 'CreativeWork',
'@id': `https://itomdev.com/#project-${projectSlug}`,
name: p.seoTitle || p.title,
description: p.seoDescription || p.description || '',
url: p.url || undefined,
creator: { '@id': 'https://itomdev.com/#person' },
...(p.techStack && p.techStack.length > 0 ? {
keywords: p.techStack.map(t => TECH_STACK_NAMES[t] || t).join(', ')
} : {}),
});
});
}
// --- 6. Studio Content (YouTube -> VideoObject, Blog -> Article, TikTok -> VideoObject) ---
if (studio && studio.length > 0) {
studio.forEach((s, idx) => {
const studioSlug = `studio-item-${idx}`;
if (s.platform === 'youtube') {
let embedUrl = undefined;
if (s.url) {
const ytMatch = s.url.match(/(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))([^"&?\/\s]{11})/);
if (ytMatch && ytMatch[1]) {
embedUrl = `https://www.youtube.com/embed/${ytMatch[1]}`;
}
}
graph.push({
'@type': 'VideoObject',
'@id': `https://itomdev.com/#${studioSlug}`,
name: s.seoTitle || s.title,
description: s.seoDescription || s.description || '',
url: s.url || undefined,
contentUrl: s.url || undefined,
...(embedUrl ? { embedUrl } : {}),
thumbnailUrl: s.thumbnailUrl || 'https://itomdev.com/og-image.webp',
...(s.duration ? { duration: `PT${s.duration.replace(':', 'M')}S` } : {}),
...(s.date ? { uploadDate: formatIsoDate(s.date) } : {}),
...(s.views ? { interactionStatistic: { '@type': 'InteractionCounter', interactionType: 'https://schema.org/WatchAction', userInteractionCount: s.views } } : {}),
author: { '@id': 'https://itomdev.com/#person' },
});
} else if (s.platform === 'blog') {
graph.push({
'@type': 'Article',
'@id': `https://itomdev.com/#${studioSlug}`,
headline: s.seoTitle || s.title,
description: s.seoDescription || s.description || '',
url: s.url || undefined,
image: s.thumbnailUrl || 'https://itomdev.com/og-image.webp',
...(s.date ? { datePublished: formatIsoDate(s.date) } : {}),
...(s.readTime ? { timeRequired: `PT${s.readTime.replace(' min', '')}M` } : {}),
author: { '@id': 'https://itomdev.com/#person' },
});
} else if (s.platform === 'tiktok') {
graph.push({
'@type': 'VideoObject',
'@id': `https://itomdev.com/#${studioSlug}`,
name: s.seoTitle || s.title,
description: s.seoDescription || s.description || '',
url: s.url || undefined,
contentUrl: s.url || undefined,
thumbnailUrl: s.thumbnailUrl || 'https://itomdev.com/og-image.webp',
...(s.date ? { uploadDate: formatIsoDate(s.date) } : {}),
...(s.views ? { interactionStatistic: { '@type': 'InteractionCounter', interactionType: 'https://schema.org/WatchAction', userInteractionCount: s.views } } : {}),
...(s.likes ? { aggregateRating: { '@type': 'AggregateRating', ratingCount: s.likes } } : {}),
author: { '@id': 'https://itomdev.com/#person' },
});
} else if (s.platform === 'instagram' || s.platform === 'x' || s.platform === 'linkedin') {
graph.push({
'@type': 'SocialMediaPosting',
'@id': `https://itomdev.com/#${studioSlug}`,
headline: s.seoTitle || s.title,
description: s.seoDescription || s.description || '',
url: s.url || undefined,
image: s.thumbnailUrl || 'https://itomdev.com/og-image.webp',
...(s.date ? { datePublished: formatIsoDate(s.date) } : {}),
...(s.likes ? { interactionStatistic: { '@type': 'InteractionCounter', interactionType: 'https://schema.org/LikeAction', userInteractionCount: s.likes } } : {}),
author: { '@id': 'https://itomdev.com/#person' },
});
} else if (s.platform === 'codrops') {
graph.push({
'@type': 'Article',
'@id': `https://itomdev.com/#${studioSlug}`,
headline: s.seoTitle || s.title,
description: s.seoDescription || s.description || '',
url: s.url || undefined,
image: s.thumbnailUrl || 'https://itomdev.com/og-image.webp',
...(s.date ? { datePublished: formatIsoDate(s.date) } : {}),
author: { '@id': 'https://itomdev.com/#person' },
});
}
});
}
// --- 7. Awards as schema.org Award/CreativeWork ---
if (awards && awards.length > 0) {
const categoryLabels = { sotd: 'Site of the Day', sotm: 'Site of the Month', other: 'Honorable Mention' };
graph.push({
'@type': 'ItemList',
'@id': 'https://itomdev.com/#awardslist',
name: 'Web Design Awards received by Tomasz "ITom" Szmajda',
numberOfItems: awards.length,
itemListElement: awards.map((a, i) => ({
'@type': 'ListItem',
position: i + 1,
item: {
'@type': 'CreativeWork',
name: `${categoryLabels[a.category] || a.category} — ${a.seoTitle || a.title}`,
...(a.date ? { dateCreated: formatIsoDate(a.date) } : {}),
url: a.url || undefined,
description: a.seoDescription || undefined,
award: categoryLabels[a.category] || a.category,
creator: { '@id': 'https://itomdev.com/#person' },
}
}))
});
}
return {
'@context': 'https://schema.org',
'@graph': graph
};
}
// Helper to generate the llms.txt content in clean Markdown
function buildLlmsTxt(globalInfo, projects, studio, awards, faqList) {
const siteTitle = globalInfo?.siteTitle || 'Tomasz "ITom" Szmajda | Creative 3D Portfolio';
const siteDescription = globalInfo?.siteDescription || 'Interactive 3D Developer Portfolio';
const aboutMe = globalInfo?.aboutMe || 'I am a creative developer specializing in 3D web experiences.';
let content = `# ${siteTitle}\n`;
content += `> ${siteDescription}\n\n`;
content += `## Biography / About Me\n`;
content += `${aboutMe}\n\n`;
content += `## Core Technologies & Skills\n`;
content += `- React, Three.js, React Three Fiber (R3F), GSAP (GreenSock), JavaScript, TypeScript, Next.js, WebGL, 3D Graphics, Web Development.\n\n`;
if (projects && projects.length > 0) {
content += `## Selected Portfolio Projects\n`;
projects.forEach(p => {
const tech = p.techStack ? ` (Tech: ${p.techStack.map(t => TECH_STACK_NAMES[t] || t).join(', ')})` : '';
content += `- [${p.seoTitle || p.title}](${p.url || 'https://itomdev.com'}): ${p.seoDescription || p.description || ''}${tech}\n`;
});
content += `\n`;
}
if (studio && studio.length > 0) {
content += `## Studio Content & Publications\n`;
studio.forEach(s => {
content += `- [${s.seoTitle || s.title} (${s.platform})](${s.url || 'https://itomdev.com'}): ${s.seoDescription || s.description || ''}\n`;
});
content += `\n`;
}
if (awards && awards.length > 0) {
content += `## Design Awards & Achievements\n`;
const categoryLabels = { sotd: 'Site of the Day', sotm: 'Site of the Month', other: 'Honorable Mention' };
awards.forEach(a => {
const category = categoryLabels[a.category] || a.category;
content += `- **${category}** — [${a.seoTitle || a.title}](${a.url || 'https://itomdev.com'}): Awarded on ${a.date || 'unknown'}. ${a.seoDescription || ''}\n`;
});
content += `\n`;
}
if (faqList && faqList.length > 0) {
content += `## Frequently Asked Questions (FAQ)\n`;
faqList.forEach(item => {
content += `- **${item.question}**\n`;
content += ` ${item.answer.replace(/\n/g, '\n ')}\n`;
});
}
return content;
}
export function generateSeoHtml() {
let cachedLlmsContent = '';
async function getLlmsContent() {
if (!cachedLlmsContent) {
try {
const [globalInfo, projects, studio, awards, faqList] = await Promise.all([
sanityClient.fetch(`*[_id == "globalInfo"][0]`),
sanityClient.fetch(`*[_type == "galleryProject"]`),
sanityClient.fetch(`*[_type == "studioItem"]`),
sanityClient.fetch(`*[_type == "awardCertificate"]`),
sanityClient.fetch(`*[_type == "faq"]`)
]);
cachedLlmsContent = buildLlmsTxt(globalInfo, projects, studio, awards, faqList);
} catch (e) {
console.error('SEO Plugin Error: Failed to fetch Sanity data for llms.txt', e);
cachedLlmsContent = `# Tomasz Szmajda\n> Creative Developer\n`;
}
}
return cachedLlmsContent;
}
return {
name: 'sanity-seo-plugin',
// Serve llms.txt in local development mode
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (req.url === '/llms.txt') {
const content = await getLlmsContent();
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.end(content);
} else {
next();
}
});
},
// This hook runs when Vite generates or serves index.html
async transformIndexHtml(html) {
try {
// Fetch all data in parallel
const [globalInfo, projects, studio, awards, faqList] = await Promise.all([
sanityClient.fetch(`*[_id == "globalInfo"][0]`),
sanityClient.fetch(`*[_type == "galleryProject"]`),
sanityClient.fetch(`*[_type == "studioItem"] { ..., "thumbnailUrl": frontTexture.asset->url }`),
sanityClient.fetch(`*[_type == "awardCertificate"]`),
sanityClient.fetch(`*[_type == "faq"]`)
]);
// Fallback values if globalInfo is not yet created in Sanity
const siteTitle = globalInfo?.siteTitle || 'ITom - Creative Developer';
const siteDescription = globalInfo?.siteDescription || 'Interactive 3D portfolio of a creative web developer.';
const aboutMe = globalInfo?.aboutMe || 'I am a creative developer specializing in 3D web experiences.';
// Cache llms.txt content for later bundle emission
cachedLlmsContent = buildLlmsTxt(globalInfo, projects, studio, awards, faqList);
// ====== PART 1: Build the semantic HTML string ======
let seoHtml = `\n<div id="seo-content" class="sr-only-seo">\n`;
seoHtml += ` <header>\n`;
seoHtml += ` <h1>${siteTitle}</h1>\n`;
seoHtml += ` <p>${siteDescription}</p>\n`;
seoHtml += ` </header>\n`;
seoHtml += ` <section id="about">\n`;
seoHtml += ` <h2>About Me</h2>\n`;
seoHtml += ` <p>${aboutMe}</p>\n`;
if (globalInfo?.githubUrl) seoHtml += ` <a href="${globalInfo.githubUrl}">GitHub</a>\n`;
if (globalInfo?.linkedinUrl) seoHtml += ` <a href="${globalInfo.linkedinUrl}">LinkedIn</a>\n`;
seoHtml += ` </section>\n`;
if (projects && projects.length > 0) {
seoHtml += ` <section id="projects">\n <h2>Projects</h2>\n <ul>\n`;
projects.forEach(p => {
seoHtml += ` <li>\n <h3>${p.seoTitle || p.title}</h3>\n <p>${p.seoDescription || p.description || ''}</p>\n ${p.url ? `<a href="${p.url}">Visit ${p.seoTitle || p.title}</a>\n` : ''} </li>\n`;
});
seoHtml += ` </ul>\n </section>\n`;
}
if (studio && studio.length > 0) {
seoHtml += ` <section id="studio">\n <h2>The Studio (Content)</h2>\n <ul>\n`;
studio.forEach(s => {
seoHtml += ` <li>\n <h3>${s.seoTitle || s.title} (${s.platform})</h3>\n <p>${s.seoDescription || s.description || ''}</p>\n ${s.url ? `<a href="${s.url}">View Content</a>\n` : ''} </li>\n`;
});
seoHtml += ` </ul>\n </section>\n`;
}
if (awards && awards.length > 0) {
seoHtml += ` <section id="awards">\n <h2>Awards & Certificates</h2>\n <ul>\n`;
awards.forEach(a => {
seoHtml += ` <li>\n <h3>${a.seoTitle || a.title}</h3>\n <p>${a.category} - ${a.date}</p>\n <p>${a.seoDescription || ''}</p>\n ${a.url ? `<a href="${a.url}">Link</a>\n` : ''} </li>\n`;
});
seoHtml += ` </ul>\n </section>\n`;
}
// FAQ Section (GEO/AI search optimizer fallback)
if (faqList && faqList.length > 0) {
seoHtml += ` <section id="faq">\n`;
seoHtml += ` <h2>Frequently Asked Questions (FAQ)</h2>\n`;
faqList.forEach(item => {
seoHtml += ` <article>\n`;
seoHtml += ` <h3>${item.question}</h3>\n`;
seoHtml += ` <p>${item.answer}</p>\n`;
seoHtml += ` </article>\n`;
});
seoHtml += ` </section>\n`;
}
seoHtml += `</div>\n`;
// ====== PART 2: Build dynamic JSON-LD ======
const jsonLdSchemas = buildJsonLd(globalInfo, projects, studio, awards, faqList);
const jsonLdScript = `\n <!-- Dynamic Structured Data (JSON-LD) — generated from Sanity at build time -->\n <script type="application/ld+json">\n${JSON.stringify(jsonLdSchemas, null, 2)}\n </script>\n`;
// ====== PART 3: Transform HTML ======
// Update the <title> tag
let transformedHtml = html.replace(
/<title>(.*?)<\/title>/,
`<title>${siteTitle}</title>`
);
// Add or replace meta description
if (transformedHtml.includes('<meta name="description"')) {
transformedHtml = transformedHtml.replace(
/<meta name="description" content="(.*?)"\s*\/?>/,
`<meta name="description" content="${siteDescription}" />`
);
} else {
transformedHtml = transformedHtml.replace(
'</head>',
` <meta name="description" content="${siteDescription}" />\n</head>`
);
}
// Update Open Graph dynamic metadata
transformedHtml = transformedHtml
.replace(
/<meta\s+property="og:title"\s+content="[^"]*"\s*\/?>/i,
`<meta property="og:title" content="${siteTitle}" />`
)
.replace(
/<meta\s+property="og:description"\s+content="[^"]*"\s*\/?>/i,
`<meta property="og:description" content="${siteDescription}" />`
);
// Update Twitter card dynamic metadata
transformedHtml = transformedHtml
.replace(
/<meta\s+name="twitter:title"\s+content="[^"]*"\s*\/?>/i,
`<meta name="twitter:title" content="${siteTitle}" />`
)
.replace(
/<meta\s+name="twitter:description"\s+content="[^"]*"\s*\/?>/i,
`<meta name="twitter:description" content="${siteDescription}" />`
);
// Inject dynamic JSON-LD right before </head> (next to the existing static one)
transformedHtml = transformedHtml.replace('</head>', `${jsonLdScript}</head>`);
// Replace the static placeholder with the dynamic one to prevent duplicate #seo-content and double h1s
if (transformedHtml.includes('id="seo-content"')) {
transformedHtml = transformedHtml.replace(
/<div id="seo-content" class="sr-only-seo">[\s\S]*?<\/div>/,
seoHtml
);
} else {
// Fallback injection if the template doesn't contain the static block
transformedHtml = transformedHtml.replace('</body>', `${seoHtml}</body>`);
}
return transformedHtml;
} catch (error) {
console.error('SEO Plugin Error: Failed to fetch Sanity data', error);
// Return original HTML on failure so we don't break the build
return html;
}
},
// Emit llms.txt to the build output directory
async generateBundle() {
const content = await getLlmsContent();
this.emitFile({
type: 'asset',
fileName: 'llms.txt',
source: content
});
}
};
}