Maybe clean up

This commit is contained in:
Andras Schmelczer 2026-05-25 09:49:09 +01:00
parent 2165ed0c33
commit db8d4597df
40 changed files with 404 additions and 332 deletions

View file

@ -3,9 +3,22 @@ import type { SchemaContext } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';
const safeUrl = z.string().refine(
(url) => {
if (url.startsWith('/')) return !url.startsWith('//');
try {
const parsed = new URL(url);
return ['http:', 'https:', 'mailto:'].includes(parsed.protocol);
} catch {
return false;
}
},
{ message: 'URL must be an absolute http(s)/mailto URL or a root-relative path.' }
);
const linkSchema = z.object({
label: z.string(),
url: z.string(),
url: safeUrl,
download: z.boolean().optional(),
});
@ -17,17 +30,30 @@ const thumbnailSchema = ({ image }: SchemaContext) =>
const mediaSchema = ({ image }: SchemaContext) =>
z
.object({
type: z.enum(['image', 'video', 'diagram']),
src: image().optional(),
poster: image().optional(),
mp4: z.string().optional(),
webm: z.string().optional(),
alt: z.string().optional(),
decorative: z.boolean().optional(),
caption: z.string().optional(),
transcript: z.string().optional(),
})
.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: safeUrl.optional(),
webm: safeUrl.optional(),
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.',
});
@ -72,7 +98,6 @@ const projects = defineCollection({
loader: glob({ pattern: '**/*.md', base: './src/content/projects' }),
schema: ({ image }) =>
z.object({
sourceProjectId: z.string(),
title: z.string(),
description: z.string().max(160),
thumbnail: thumbnailSchema({ image }),