diff --git a/src/content/config.ts b/src/content/config.ts index 7cbb222..cb4387f 100644 --- a/src/content/config.ts +++ b/src/content/config.ts @@ -1,3 +1,4 @@ +import { imageMetadata } from '@/helpers/images'; import { urlJoin } from '@/helpers/tools'; import options from '@/options'; import { defineCollection, z } from 'astro:content'; @@ -14,7 +15,12 @@ const slug = () => .max(200) .regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/i, 'Invalid slug'); -const image = (fallbackImage: string) => z.string().optional().default(fallbackImage); +const image = (fallbackImage: string) => + z + .string() + .optional() + .default(fallbackImage) + .transform((file) => imageMetadata(file)); // Categories Collection const categoriesCollection = defineCollection({ diff --git a/src/helpers/schema.ts b/src/helpers/schema.ts index 5a10051..2efb8fe 100644 --- a/src/helpers/schema.ts +++ b/src/helpers/schema.ts @@ -1,5 +1,4 @@ import { defaultCover } from '@/content/config.ts'; -import { imageMetadata, type Image } from '@/helpers/images'; import options from '@/options'; import { getCollection, getEntry, type Render } from 'astro:content'; @@ -11,22 +10,19 @@ const postsCollection = await getCollection('posts'); const tagsCollection = await getCollection('tags'); // Redefine the types from the astro content. -export type Category = Omit<(typeof categoriesCollection)[number]['data'], 'cover'> & { +export type Category = (typeof categoriesCollection)[number]['data'] & { counts: number; permalink: string; - cover: Image; }; export type Friend = (typeof friendsCollection)[number]['data'][number]; -export type Page = Omit<(typeof pagesCollection)[number]['data'], 'cover'> & { +export type Page = (typeof pagesCollection)[number]['data'] & { slug: string; permalink: string; - cover: Image; render: () => Render['.mdx']; }; -export type Post = Omit<(typeof postsCollection)[number]['data'], 'cover'> & { +export type Post = (typeof postsCollection)[number]['data'] & { slug: string; permalink: string; - cover: Image; render: () => Render['.mdx']; raw: () => Promise; }; @@ -35,52 +31,42 @@ export type Tag = (typeof tagsCollection)[number]['data'][number] & { counts: nu // Translate the Astro content into the original content for dealing with different configuration types. export const friends: Friend[] = friendsCollection[0].data; // Override the website for local debugging -export const pages: Page[] = await Promise.all( - pagesCollection - .filter((page) => page.data.published || !options.isProd()) - .map(async (page) => ({ - slug: page.slug, - permalink: `/${page.slug}`, - render: async () => { - const entry = await getEntry('pages', page.slug); - return entry.render(); - }, - ...page.data, - cover: await imageMetadata(page.data.cover), - })), -); -export const posts: Post[] = ( - await Promise.all( - postsCollection - .filter((post) => post.data.published || !options.isProd()) - .map(async (post) => ({ - slug: post.slug, - permalink: `/posts/${post.slug}`, - render: async () => { - const entry = await getEntry('posts', post.slug); - return entry.render(); - }, - raw: async () => { - const entry = await getEntry('posts', post.slug); - return entry.body; - }, - ...post.data, - cover: await imageMetadata(post.data.cover), - })), - ) -).sort((left: Post, right: Post) => { - const a = left.date.getTime(); - const b = right.date.getTime(); - return options.settings.post.sort === 'asc' ? a - b : b - a; -}); -export const categories: Category[] = await Promise.all( - categoriesCollection.map(async (cat) => ({ - counts: posts.filter((post) => post.category === cat.data.name).length, - permalink: `/cats/${cat.data.slug}`, - ...cat.data, - cover: await imageMetadata(cat.data.cover), - })), -); +export const pages: Page[] = pagesCollection + .filter((page) => page.data.published || !options.isProd()) + .map((page) => ({ + slug: page.slug, + permalink: `/${page.slug}`, + render: async () => { + const entry = await getEntry('pages', page.slug); + return entry.render(); + }, + ...page.data, + })); +export const posts: Post[] = postsCollection + .filter((post) => post.data.published || !options.isProd()) + .map((post) => ({ + slug: post.slug, + permalink: `/posts/${post.slug}`, + render: async () => { + const entry = await getEntry('posts', post.slug); + return entry.render(); + }, + raw: async () => { + const entry = await getEntry('posts', post.slug); + return entry.body; + }, + ...post.data, + })) + .sort((left: Post, right: Post) => { + const a = left.date.getTime(); + const b = right.date.getTime(); + return options.settings.post.sort === 'asc' ? a - b : b - a; + }); +export const categories: Category[] = categoriesCollection.map((cat) => ({ + counts: posts.filter((post) => post.category === cat.data.name).length, + permalink: `/cats/${cat.data.slug}`, + ...cat.data, +})); export const tags: Tag[] = tagsCollection[0].data.map((tag) => ({ counts: posts.filter((post) => post.tags.includes(tag.name)).length, permalink: `/tags/${tag.slug}`,