feat: multi-authors within one article support

This commit is contained in:
草师傅 2025-07-22 18:03:12 +08:00
parent 0eef3d8d05
commit 98d23e7c94
Signed by: gb
GPG key ID: 43330A030E2D6478
4 changed files with 14 additions and 12 deletions

View file

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

View file

@ -10,5 +10,5 @@ export const posts = ({ image }) => z.object({
categories: z.array(z.string()).default(['uncategorized']),
tags: z.array(z.string()).optional(),
cover: image().optional(),
author: reference('authors').optional(),
author: z.union([z.array(reference('authors')), reference('authors')]).optional(),
});

View file

@ -2,6 +2,9 @@
title: 'My Terminal Setup'
description: 'A walkthrough of my current terminal configuration'
pubDate: '2025-06-08'
author:
- 'Glados'
- 'Wheatley'
---
![cover from mohammad-rahmani-oXlXu2qukGE-unsplash](./demo.png)
Here's my current terminal setup:

View file

@ -23,11 +23,11 @@ const { Content, headings } = await entry.render();
const noscript = siteConfig.noClientJavaScript
const slug = Astro.params.slug;
const author = entry.data.author || {collection: 'authors', id: siteConfig.defaultAuthor.id};
const author = Array.isArray(entry.data.author) ? entry.data.author : (entry.data.author !== undefined ? [entry.data.author] : [{collection: 'authors', id: siteConfig.defaultAuthor.id}]);
// Get author data
const authorData = await getEntry(author);
const authorInfo = authorData ? authorData.data : siteConfig.defaultAuthor;
const authorData = await Promise.all((author).map((singleAuthor) => getEntry(singleAuthor).then(authorEntry => authorEntry.data)))
const authorInfo = authorData.includes(undefined) ? [{data: siteConfig.defaultAuthor}] : authorData;
// get featured image and use it as og:image
// use the custom cover image if it exists, otherwise use the featured image file in the same directory
@ -36,7 +36,7 @@ const customFeaturedImage = entry.data.cover?.src
const matchedImage = Object.keys(featuredImages).find(path => path.includes(slug));
let matchedImage_src;
if (matchedImage && !customFeaturedImage) {
matchedImage_src = await getImage({src: featuredImages[matchedImage], format: 'webp'}) || null;
matchedImage_src = await getImage({src: featuredImages[matchedImage] as ImageMetadata, format: 'webp'}) || null;
}
const firstImageURL = await ExtractFirstImage(Content)
@ -47,10 +47,10 @@ const cover = customFeaturedImage || matchedImage_src?.src || firstImageURL || `
title={entry.data.title}
description={entry.data.description}
ogImage={cover}
author={authorInfo.name}
author={authorInfo.map((a: any) => a.name).join(', ')}
>
<h1 class="title">{entry.data.title}</h1>
<AuthorInfo data={authorData} />
{authorInfo.map((a: any) => <AuthorInfo data={a} />)}
<span class="date">{new Date(entry.data.pubDate).toISOString().split('T')[0]}</span>
{headings.length !== 0 && <TableOfContents headings={headings} />}
{entry.data.summary && <p class="summary">{entry.data.summary}</p> }
@ -59,7 +59,7 @@ const cover = customFeaturedImage || matchedImage_src?.src || firstImageURL || `
</div>
<div class="extra-post" style="margin-top: 2rem; border-top: 1px solid var(--border-color); padding-top: 1rem;">
<ReplyViaEmail title={entry.data.title} email={authorInfo.email} />
<ReplyViaEmail title={entry.data.title} email={authorInfo[0].email} />
<br>
<a href="/blog">&larr; Back to posts</a>
{!noscript && <h2>Comments</h2> <Comments />}