yufan.me/src/content.config.ts

155 lines
4.8 KiB
TypeScript

import { imageMetadata } from '@/helpers/images';
import { urlJoin } from '@/helpers/tools';
import options from '@/options';
import { glob } from 'astro/loaders';
import { defineCollection, z } from 'astro:content';
import { glob as Glob } from 'glob';
import path from 'node:path';
export const defaultCover = '/images/default-cover.jpg';
// Copied and modified from https://github.com/zce/velite/blob/main/src/schemas/slug.ts
// The slug is internally supported by Astro with 'content' type.
// We add the slug here for validating the YAML configuration.
const slug = () =>
z
.string()
.min(3)
.max(200)
.regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/i, 'Invalid slug');
const image = (fallbackImage: string) => z.string().optional().default(fallbackImage);
// The default toc heading level.
const toc = () =>
z
.union([
z.object({
// The level to start including headings at in the table of contents. Default: 2.
minHeadingLevel: z.number().int().min(1).max(6).optional().default(options.settings.toc.minHeadingLevel),
// The level to stop including headings at in the table of contents. Default: 3.
maxHeadingLevel: z.number().int().min(1).max(6).optional().default(options.settings.toc.maxHeadingLevel),
}),
z.boolean().transform((enabled) =>
enabled
? {
minHeadingLevel: options.settings.toc.minHeadingLevel,
maxHeadingLevel: options.settings.toc.maxHeadingLevel,
}
: false,
),
])
.default(false)
.refine((toc) => (toc ? toc.minHeadingLevel <= toc.maxHeadingLevel : true), {
message: 'minHeadingLevel must be less than or equal to maxHeadingLevel',
});
// Images Collection
const imagesCollection = defineCollection({
loader: async () => {
const publicDirectory = path.join(process.cwd(), 'public');
const imagePaths = await Glob(path.join(publicDirectory, '**/*.{jpg,jpeg,gif,svg,png,webp}'));
const metas = imagePaths
.map((imagePath) => imagePath.substring(publicDirectory.length))
.map(async (imagePath) => ({ id: imagePath, ...(await imageMetadata(imagePath)) }));
return Promise.all(metas);
},
schema: z.object({
src: z.string(),
width: z.union([z.string(), z.number()]),
height: z.union([z.string(), z.number()]),
blurDataURL: z.string(),
blurWidth: z.number(),
blurHeight: z.number(),
}),
});
// Categories Collection
const categoriesCollection = defineCollection({
loader: glob({ pattern: '**\/[^_]*.yml', base: './src/content/categories' }),
schema: z.object({
name: z.string().max(20),
slug: slug(),
cover: image(defaultCover),
description: z.string().max(999).optional().default(''),
}),
});
// Friends Collection
const friendsCollection = defineCollection({
loader: glob({ pattern: '**\/[^_]*.yml', base: './src/content/friends' }),
schema: z.array(
z
.object({
website: z.string().max(40),
description: z.string().optional(),
homepage: z.string().url(),
poster: z
.string()
.transform((poster) => (poster.startsWith('/') ? urlJoin(options.assetsPrefix(), poster) : poster)),
favicon: z.string().optional(),
})
.transform((data) => {
if (data.favicon === undefined) {
data.favicon = `${data.homepage}/favicon.ico`;
}
return data;
}),
),
});
// Posts Collection
const postsCollection = defineCollection({
loader: glob({ pattern: '**\/[^_]*.mdx', base: './src/content/posts' }),
schema: z.object({
title: z.string().max(99),
date: z.date(),
updated: z.date().optional(),
comments: z.boolean().optional().default(true),
alias: z.array(z.string()).optional().default([]),
tags: z.array(z.string()).optional().default([]),
category: z.string(),
summary: z.string().optional().default(''),
cover: image(defaultCover),
published: z.boolean().optional().default(true),
toc: toc(),
}),
});
// Pages Collection
const pagesCollection = defineCollection({
loader: glob({ pattern: '**\/[^_]*.mdx', base: './src/content/pages' }),
schema: z.object({
title: z.string().max(99),
date: z.date(),
updated: z.date().optional(),
comments: z.boolean().optional().default(true),
cover: image(defaultCover),
published: z.boolean().optional().default(true),
summary: z.string().optional(),
friend: z.boolean().optional().default(false),
toc: toc(),
}),
});
// Tags Collection
const tagsCollection = defineCollection({
loader: glob({ pattern: '**\/[^_]*.yml', base: './src/content/tags' }),
schema: z.array(
z.object({
name: z.string().max(20),
slug: slug(),
}),
),
});
export const collections = {
categories: categoriesCollection,
friends: friendsCollection,
pages: pagesCollection,
posts: postsCollection,
tags: tagsCollection,
images: imagesCollection,
};