feat: more modern way to use 'reference' to get authors
This commit is contained in:
parent
9ff40d1e9e
commit
9ecf25f3ab
4 changed files with 21 additions and 12 deletions
|
@ -3,14 +3,13 @@ import {Image} from "astro:assets";
|
||||||
import {getEntry} from "astro:content";
|
import {getEntry} from "astro:content";
|
||||||
import {siteConfig} from "../../../config";
|
import {siteConfig} from "../../../config";
|
||||||
|
|
||||||
const { id } = Astro.props;
|
const { data } = Astro.props;
|
||||||
|
|
||||||
// Get author data
|
// Get author data
|
||||||
const authorData = await getEntry('authors', id || '');
|
const authorAvatar = data?.data.mcplayerid ? `/images/avatars/${data.id}.png` : null;
|
||||||
const authorAvatar = authorData?.data.mcplayerid ? `/images/avatars/${id}.png` : null;
|
const authorName = data ? data.data.name : null;
|
||||||
const authorName = authorData ? authorData.data.name : null;
|
|
||||||
---
|
---
|
||||||
{(siteConfig.displayAvatar && authorData) &&
|
{(siteConfig.displayAvatar && data) &&
|
||||||
<>
|
<>
|
||||||
{authorAvatar && <Image src={authorAvatar} alt={`avatar of ${authorName}`} width=16 height=16 />}
|
{authorAvatar && <Image src={authorAvatar} alt={`avatar of ${authorName}`} width=16 height=16 />}
|
||||||
<span>{authorName} @ </span>
|
<span>{authorName} @ </span>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { z } from 'astro:content';
|
import { z, reference } from 'astro:content';
|
||||||
|
|
||||||
export const posts = ({ image }) => z.object({
|
export const posts = ({ image }) => z.object({
|
||||||
title: z.string(),
|
title: z.string(),
|
||||||
|
@ -8,5 +8,5 @@ export const posts = ({ image }) => z.object({
|
||||||
categories: z.array(z.string()).default(['uncategorized']),
|
categories: z.array(z.string()).default(['uncategorized']),
|
||||||
tags: z.array(z.string()).optional(),
|
tags: z.array(z.string()).optional(),
|
||||||
cover: image().optional(),
|
cover: image().optional(),
|
||||||
author: z.string().optional(),
|
author: reference('authors').optional(),
|
||||||
});
|
});
|
|
@ -19,10 +19,10 @@ const { entry } = Astro.props;
|
||||||
const { Content } = await entry.render();
|
const { Content } = await entry.render();
|
||||||
const noscript = siteConfig.noClientJavaScript
|
const noscript = siteConfig.noClientJavaScript
|
||||||
const slug = Astro.params.slug;
|
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
|
// Get author data
|
||||||
const authorData = await getEntry('authors', authorId);
|
const authorData = await getEntry(author);
|
||||||
const authorInfo = authorData ? authorData.data : siteConfig.defaultAuthor;
|
const authorInfo = authorData ? authorData.data : siteConfig.defaultAuthor;
|
||||||
|
|
||||||
// get featured image and use it as og:image
|
// get featured image and use it as og:image
|
||||||
|
@ -46,7 +46,7 @@ const cover = customFeaturedImage || matchedImage_src?.src || firstImageURL || `
|
||||||
author={authorInfo.name}
|
author={authorInfo.name}
|
||||||
>
|
>
|
||||||
<h1 class="title">{entry.data.title}</h1>
|
<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>
|
<span class="date">{new Date(entry.data.pubDate).toISOString().split('T')[0]}</span>
|
||||||
<div class="content">
|
<div class="content">
|
||||||
<Content />
|
<Content />
|
||||||
|
@ -59,4 +59,5 @@ const cover = customFeaturedImage || matchedImage_src?.src || firstImageURL || `
|
||||||
{noscript && <h2>Comments</h2> <Comments path={`post/${slug}`} />}
|
{noscript && <h2>Comments</h2> <Comments path={`post/${slug}`} />}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</Layout>
|
</Layout>
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,16 @@ export async function GET(context) {
|
||||||
},
|
},
|
||||||
sanitize({ dropElements: ["script", "style"] }),
|
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.
|
// Return our RSS feed XML response.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue