feat: more modern way to use 'reference' to get authors

This commit is contained in:
grassblock 2025-06-06 22:50:36 +08:00
parent 9ff40d1e9e
commit 9ecf25f3ab
4 changed files with 21 additions and 12 deletions

View file

@ -3,14 +3,13 @@ import {Image} from "astro:assets";
import {getEntry} from "astro:content";
import {siteConfig} from "../../../config";
const { id } = Astro.props;
const { data } = Astro.props;
// Get author data
const authorData = await getEntry('authors', id || '');
const authorAvatar = authorData?.data.mcplayerid ? `/images/avatars/${id}.png` : null;
const authorName = authorData ? authorData.data.name : null;
const authorAvatar = data?.data.mcplayerid ? `/images/avatars/${data.id}.png` : null;
const authorName = data ? data.data.name : null;
---
{(siteConfig.displayAvatar && authorData) &&
{(siteConfig.displayAvatar && data) &&
<>
{authorAvatar && <Image src={authorAvatar} alt={`avatar of ${authorName}`} width=16 height=16 />}
<span>{authorName} @ </span>

View file

@ -1,4 +1,4 @@
import { z } from 'astro:content';
import { z, reference } from 'astro:content';
export const posts = ({ image }) => z.object({
title: z.string(),
@ -8,5 +8,5 @@ export const posts = ({ image }) => z.object({
categories: z.array(z.string()).default(['uncategorized']),
tags: z.array(z.string()).optional(),
cover: image().optional(),
author: z.string().optional(),
author: reference('authors').optional(),
});

View file

@ -19,10 +19,10 @@ const { entry } = Astro.props;
const { Content } = await entry.render();
const noscript = siteConfig.noClientJavaScript
const slug = Astro.params.slug;
const authorId = entry.data.author || siteConfig.defaultAuthor.id;
const author = entry.data.author || {collection: 'authors', id: siteConfig.defaultAuthor.id};
// Get author data
const authorData = await getEntry('authors', authorId);
const authorData = await getEntry(author);
const authorInfo = authorData ? authorData.data : siteConfig.defaultAuthor;
// get featured image and use it as og:image
@ -46,7 +46,7 @@ const cover = customFeaturedImage || matchedImage_src?.src || firstImageURL || `
author={authorInfo.name}
>
<h1 class="title">{entry.data.title}</h1>
<AuthorInfo id={authorId} />
<AuthorInfo data={authorData} />
<span class="date">{new Date(entry.data.pubDate).toISOString().split('T')[0]}</span>
<div class="content">
<Content />
@ -60,3 +60,4 @@ const cover = customFeaturedImage || matchedImage_src?.src || firstImageURL || `
</div>
</Layout>

View file

@ -52,7 +52,16 @@ export async function GET(context) {
},
sanitize({ dropElements: ["script", "style"] }),
]);
feedItems.push({ ...post.data, link: `/post/${post.slug}/`, content });
// Make sure each feed item has required properties with proper formatting
feedItems.push({
title: post.data.title,
description: post.data.description || '',
pubDate: post.data.pubDate,
link: `${baseUrl}/post/${post.slug}`,
content,
});
}
// Return our RSS feed XML response.