fix: change the image metadata.
This commit is contained in:
parent
596040fef2
commit
71c941e9e3
@ -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({
|
||||||
|
@ -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,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.
|
// 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((page) => ({
|
||||||
.map(async (page) => ({
|
slug: page.slug,
|
||||||
slug: page.slug,
|
permalink: `/${page.slug}`,
|
||||||
permalink: `/${page.slug}`,
|
render: async () => {
|
||||||
render: async () => {
|
const entry = await getEntry('pages', page.slug);
|
||||||
const entry = await getEntry('pages', page.slug);
|
return entry.render();
|
||||||
return entry.render();
|
},
|
||||||
},
|
...page.data,
|
||||||
...page.data,
|
}));
|
||||||
cover: await imageMetadata(page.data.cover),
|
export const posts: Post[] = postsCollection
|
||||||
})),
|
.filter((post) => post.data.published || !options.isProd())
|
||||||
);
|
.map((post) => ({
|
||||||
export const posts: Post[] = (
|
slug: post.slug,
|
||||||
await Promise.all(
|
permalink: `/posts/${post.slug}`,
|
||||||
postsCollection
|
render: async () => {
|
||||||
.filter((post) => post.data.published || !options.isProd())
|
const entry = await getEntry('posts', post.slug);
|
||||||
.map(async (post) => ({
|
return entry.render();
|
||||||
slug: post.slug,
|
},
|
||||||
permalink: `/posts/${post.slug}`,
|
raw: async () => {
|
||||||
render: async () => {
|
const entry = await getEntry('posts', post.slug);
|
||||||
const entry = await getEntry('posts', post.slug);
|
return entry.body;
|
||||||
return entry.render();
|
},
|
||||||
},
|
...post.data,
|
||||||
raw: async () => {
|
}))
|
||||||
const entry = await getEntry('posts', post.slug);
|
.sort((left: Post, right: Post) => {
|
||||||
return entry.body;
|
const a = left.date.getTime();
|
||||||
},
|
const b = right.date.getTime();
|
||||||
...post.data,
|
return options.settings.post.sort === 'asc' ? a - b : b - a;
|
||||||
cover: await imageMetadata(post.data.cover),
|
});
|
||||||
})),
|
export const categories: Category[] = categoriesCollection.map((cat) => ({
|
||||||
)
|
counts: posts.filter((post) => post.category === cat.data.name).length,
|
||||||
).sort((left: Post, right: Post) => {
|
permalink: `/cats/${cat.data.slug}`,
|
||||||
const a = left.date.getTime();
|
...cat.data,
|
||||||
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 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}`,
|
||||||
|
Loading…
Reference in New Issue
Block a user