fix: change the image metadata.

This commit is contained in:
Yufan Sheng 2024-09-26 17:30:06 +08:00
parent d2d42e7c1d
commit 57070d3229
Signed by: syhily
GPG Key ID: 9D18A22A7DCD5A9B
2 changed files with 46 additions and 54 deletions

View File

@ -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({

View File

@ -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<string>;
};
@ -35,10 +31,9 @@ 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
export const pages: Page[] = pagesCollection
.filter((page) => page.data.published || !options.isProd())
.map(async (page) => ({
.map((page) => ({
slug: page.slug,
permalink: `/${page.slug}`,
render: async () => {
@ -46,14 +41,10 @@ export const pages: Page[] = await Promise.all(
return entry.render();
},
...page.data,
cover: await imageMetadata(page.data.cover),
})),
);
export const posts: Post[] = (
await Promise.all(
postsCollection
}));
export const posts: Post[] = postsCollection
.filter((post) => post.data.published || !options.isProd())
.map(async (post) => ({
.map((post) => ({
slug: post.slug,
permalink: `/posts/${post.slug}`,
render: async () => {
@ -65,22 +56,17 @@ export const posts: Post[] = (
return entry.body;
},
...post.data,
cover: await imageMetadata(post.data.cover),
})),
)
).sort((left: Post, right: Post) => {
}))
.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) => ({
export const categories: Category[] = 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 tags: Tag[] = tagsCollection[0].data.map((tag) => ({
counts: posts.filter((post) => post.tags.includes(tag.name)).length,
permalink: `/tags/${tag.slug}`,