All checks were successful
Deploy to Pages / build (push) Successful in 2m51s
169 lines
4.9 KiB
TypeScript
169 lines
4.9 KiB
TypeScript
import { defineCollection, reference } from 'astro:content';
|
|
import type { SchemaContext } from 'astro:content';
|
|
import { glob } from 'astro/loaders';
|
|
import { z } from 'astro/zod';
|
|
|
|
function isRootRelativeUrl(url: string) {
|
|
return url.startsWith('/') && !url.startsWith('//');
|
|
}
|
|
|
|
const linkUrl = z.string().refine(
|
|
(url) => {
|
|
if (isRootRelativeUrl(url)) return true;
|
|
try {
|
|
const parsed = new URL(url);
|
|
return ['https:', 'mailto:'].includes(parsed.protocol);
|
|
} catch {
|
|
return false;
|
|
}
|
|
},
|
|
{ message: 'URL must be an absolute https/mailto URL or a root-relative path.' }
|
|
);
|
|
|
|
const mediaUrl = z.string().refine(
|
|
(url) => {
|
|
if (isRootRelativeUrl(url)) return true;
|
|
try {
|
|
return new URL(url).protocol === 'https:';
|
|
} catch {
|
|
return false;
|
|
}
|
|
},
|
|
{ message: 'Media URL must be an absolute https URL or a root-relative path.' }
|
|
);
|
|
|
|
function isIframeUrl(url: string) {
|
|
if (isRootRelativeUrl(url)) return true;
|
|
try {
|
|
return new URL(url).protocol === 'https:';
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
const linkSchema = z.object({
|
|
label: z.string(),
|
|
url: linkUrl,
|
|
download: z.boolean().optional(),
|
|
});
|
|
|
|
const thumbnailSchema = ({ image }: SchemaContext) =>
|
|
z.object({
|
|
src: image(),
|
|
alt: z.string().min(1, 'Thumbnail alt text must not be empty.'),
|
|
});
|
|
|
|
const mediaSchema = ({ image }: SchemaContext) =>
|
|
z
|
|
.discriminatedUnion('type', [
|
|
z.object({
|
|
type: z.enum(['image', 'diagram']),
|
|
src: image(),
|
|
alt: z.string().optional(),
|
|
decorative: z.boolean().optional(),
|
|
caption: z.string().optional(),
|
|
transcript: z.string().optional(),
|
|
}),
|
|
z
|
|
.object({
|
|
type: z.literal('video'),
|
|
poster: image().optional(),
|
|
mp4: mediaUrl.optional(),
|
|
webm: mediaUrl.optional(),
|
|
captions: mediaUrl.optional(),
|
|
captionsLabel: z.string().default('English captions'),
|
|
alt: z.string().optional(),
|
|
decorative: z.boolean().optional(),
|
|
caption: z.string().optional(),
|
|
transcript: z.string().optional(),
|
|
})
|
|
.refine((item) => Boolean(item.mp4) || Boolean(item.webm), {
|
|
message: 'Video media needs at least one mp4 or webm source.',
|
|
}),
|
|
])
|
|
.refine((item) => item.decorative || (Boolean(item.alt) && Boolean(item.caption)), {
|
|
message: 'Meaningful media needs both alt text and a caption.',
|
|
})
|
|
.refine(
|
|
(item) => item.type !== 'video' || item.decorative || Boolean(item.captions),
|
|
{
|
|
message: 'Meaningful video needs captions.',
|
|
}
|
|
)
|
|
.refine(
|
|
(item) => item.type !== 'video' || item.decorative || Boolean(item.transcript),
|
|
{
|
|
message: 'Meaningful video needs a transcript.',
|
|
}
|
|
);
|
|
|
|
const posts = defineCollection({
|
|
loader: glob({ pattern: '**/*.md', base: './src/content/posts' }),
|
|
schema: ({ image }) =>
|
|
z
|
|
.object({
|
|
title: z.string(),
|
|
description: z.string().max(160),
|
|
date: z.coerce.date(),
|
|
updated: z.coerce.date().optional(),
|
|
draft: z.boolean().default(false),
|
|
thumbnail: thumbnailSchema({ image }),
|
|
iframeThumbnail: z.boolean().default(false),
|
|
tags: z.array(
|
|
z.enum([
|
|
'ai',
|
|
'systems',
|
|
'graphics',
|
|
'simulation',
|
|
'embedded',
|
|
'web',
|
|
'tools',
|
|
'games',
|
|
])
|
|
),
|
|
featuredOrder: z.number().optional(),
|
|
projectPeriod: z.string().optional(),
|
|
role: z.string().optional(),
|
|
stack: z.array(z.string()).optional(),
|
|
scale: z.string().optional(),
|
|
outcome: z.string().optional(),
|
|
audience: z
|
|
.enum(['general', 'technical', 'recruiter-relevant'])
|
|
.default('technical'),
|
|
links: z.array(linkSchema).default([]),
|
|
media: z.array(mediaSchema({ image })).default([]),
|
|
})
|
|
.refine(
|
|
(post) =>
|
|
!post.iframeThumbnail ||
|
|
post.links.some(
|
|
(link) =>
|
|
!link.download &&
|
|
link.label.trim().toLowerCase() === 'demo' &&
|
|
isIframeUrl(link.url)
|
|
),
|
|
{
|
|
path: ['iframeThumbnail'],
|
|
message:
|
|
'iframeThumbnail requires a non-download Demo link with an https or root-relative URL.',
|
|
}
|
|
),
|
|
});
|
|
|
|
const projects = defineCollection({
|
|
loader: glob({ pattern: '**/*.md', base: './src/content/projects' }),
|
|
schema: ({ image }) =>
|
|
z.object({
|
|
title: z.string(),
|
|
description: z.string().max(160),
|
|
thumbnail: thumbnailSchema({ image }),
|
|
period: z.string(),
|
|
sortDate: z.coerce.date(),
|
|
technologies: z.array(z.string()).default([]),
|
|
selected: z.boolean().default(false),
|
|
essay: reference('posts').optional(),
|
|
links: z.array(linkSchema).default([]),
|
|
}),
|
|
});
|
|
|
|
export const collections = { posts, projects };
|