fix: change the image metadata.

This commit is contained in:
Yufan Sheng 2024-09-26 17:32:30 +08:00
parent 596040fef2
commit 71c941e9e3
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,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}`,