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 { urlJoin } from '@/helpers/tools';
import options from '@/options'; import options from '@/options';
import { defineCollection, z } from 'astro:content'; import { defineCollection, z } from 'astro:content';
@ -14,7 +15,12 @@ const slug = () =>
.max(200) .max(200)
.regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/i, 'Invalid slug'); .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 // Categories Collection
const categoriesCollection = defineCollection({ const categoriesCollection = defineCollection({

View File

@ -1,5 +1,4 @@
import { defaultCover } from '@/content/config.ts'; import { defaultCover } from '@/content/config.ts';
import { imageMetadata, type Image } from '@/helpers/images';
import options from '@/options'; import options from '@/options';
import { getCollection, getEntry, type Render } from 'astro:content'; import { getCollection, getEntry, type Render } from 'astro:content';
@ -11,22 +10,19 @@ const postsCollection = await getCollection('posts');
const tagsCollection = await getCollection('tags'); const tagsCollection = await getCollection('tags');
// Redefine the types from the astro content. // 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; counts: number;
permalink: string; permalink: string;
cover: Image;
}; };
export type Friend = (typeof friendsCollection)[number]['data'][number]; 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; slug: string;
permalink: string; permalink: string;
cover: Image;
render: () => Render['.mdx']; render: () => Render['.mdx'];
}; };
export type Post = Omit<(typeof postsCollection)[number]['data'], 'cover'> & { export type Post = (typeof postsCollection)[number]['data'] & {
slug: string; slug: string;
permalink: string; permalink: string;
cover: Image;
render: () => Render['.mdx']; render: () => Render['.mdx'];
raw: () => Promise<string>; 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. // Translate the Astro content into the original content for dealing with different configuration types.
export const friends: Friend[] = friendsCollection[0].data; export const friends: Friend[] = friendsCollection[0].data;
// Override the website for local debugging // Override the website for local debugging
export const pages: Page[] = await Promise.all( export const pages: Page[] = pagesCollection
pagesCollection
.filter((page) => page.data.published || !options.isProd()) .filter((page) => page.data.published || !options.isProd())
.map(async (page) => ({ .map((page) => ({
slug: page.slug, slug: page.slug,
permalink: `/${page.slug}`, permalink: `/${page.slug}`,
render: async () => { render: async () => {
@ -46,14 +41,10 @@ export const pages: Page[] = await Promise.all(
return entry.render(); return entry.render();
}, },
...page.data, ...page.data,
cover: await imageMetadata(page.data.cover), }));
})), export const posts: Post[] = postsCollection
);
export const posts: Post[] = (
await Promise.all(
postsCollection
.filter((post) => post.data.published || !options.isProd()) .filter((post) => post.data.published || !options.isProd())
.map(async (post) => ({ .map((post) => ({
slug: post.slug, slug: post.slug,
permalink: `/posts/${post.slug}`, permalink: `/posts/${post.slug}`,
render: async () => { render: async () => {
@ -65,22 +56,17 @@ export const posts: Post[] = (
return entry.body; return entry.body;
}, },
...post.data, ...post.data,
cover: await imageMetadata(post.data.cover), }))
})), .sort((left: Post, right: Post) => {
)
).sort((left: Post, right: Post) => {
const a = left.date.getTime(); const a = left.date.getTime();
const b = right.date.getTime(); const b = right.date.getTime();
return options.settings.post.sort === 'asc' ? a - b : b - a; return options.settings.post.sort === 'asc' ? a - b : b - a;
}); });
export const categories: Category[] = await Promise.all( export const categories: Category[] = categoriesCollection.map(async (cat) => ({
categoriesCollection.map(async (cat) => ({
counts: posts.filter((post) => post.category === cat.data.name).length, counts: posts.filter((post) => post.category === cat.data.name).length,
permalink: `/cats/${cat.data.slug}`, permalink: `/cats/${cat.data.slug}`,
...cat.data, ...cat.data,
cover: await imageMetadata(cat.data.cover), }));
})),
);
export const tags: Tag[] = tagsCollection[0].data.map((tag) => ({ export const tags: Tag[] = tagsCollection[0].data.map((tag) => ({
counts: posts.filter((post) => post.tags.includes(tag.name)).length, counts: posts.filter((post) => post.tags.includes(tag.name)).length,
permalink: `/tags/${tag.slug}`, permalink: `/tags/${tag.slug}`,