From c6d2ac305dbd8d154593a30316e95eaf148e6b17 Mon Sep 17 00:00:00 2001 From: madcampos Date: Sat, 28 Sep 2024 18:20:52 -0400 Subject: [PATCH] fix: resolve authors rendering as `[object Object]` --- src/components/AuthorCard/index.astro | 11 ++++------- src/pages/authors/[author].astro | 9 ++++----- src/utils/authors.ts | 12 ++++++++++++ 3 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 src/utils/authors.ts diff --git a/src/components/AuthorCard/index.astro b/src/components/AuthorCard/index.astro index 7cbb611..5cb06ed 100644 --- a/src/components/AuthorCard/index.astro +++ b/src/components/AuthorCard/index.astro @@ -1,6 +1,5 @@ --- -import { getEntry } from 'astro:content'; - +import { getAuthor } from '../../utils/authors.js'; import Avatar from '../Avatar/index.astro'; import './styles.css'; @@ -13,9 +12,8 @@ const { author } = Astro.props; const BLOG_URL = Astro.site?.href; -// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -const { data: { avatar, avatarAlt, name, pronouns }, render } = (await getEntry('authors', author))!; -const description = await render(); +const { data: { avatar, avatarAlt, name, pronouns }, render } = await getAuthor(author); +const { Content } = await render(); ---
- {/* eslint-disable-next-line astro/no-set-html-directive */} - +
diff --git a/src/pages/authors/[author].astro b/src/pages/authors/[author].astro index 46837ba..984e559 100644 --- a/src/pages/authors/[author].astro +++ b/src/pages/authors/[author].astro @@ -1,6 +1,6 @@ --- import type { GetStaticPaths, InferGetStaticPropsType } from 'astro'; -import { getCollection } from 'astro:content'; +import { listAllAuthors } from '../../utils/authors.js'; import Avatar from '../../components/Avatar/index.astro'; import InternalPageLayout from '../../layouts/InternalPage/index.astro'; @@ -10,7 +10,7 @@ import '../../styles/authors.css'; // eslint-disable-next-line @typescript-eslint/no-use-before-define type Props = InferGetStaticPropsType; -export const getStaticPaths = (async () => (await getCollection('authors')).map(((author) => ({ +export const getStaticPaths = (async () => (await listAllAuthors()).map(((author) => ({ params: { author: author.slug }, props: { ...author.data, render: author.render, slug: author.slug } })))) satisfies GetStaticPaths; @@ -22,7 +22,7 @@ const { pronouns, socialMedia } = Astro.props; -const description = await render(); +const { Content } = await render(); ---
- {/* eslint-disable-next-line astro/no-set-html-directive */} - +
diff --git a/src/utils/authors.ts b/src/utils/authors.ts new file mode 100644 index 0000000..e08a7fc --- /dev/null +++ b/src/utils/authors.ts @@ -0,0 +1,12 @@ +import { type CollectionEntry, getCollection, getEntry } from 'astro:content'; + +export async function listAllAuthors() { + const authorEntries = await getCollection('authors'); + const authors = authorEntries.sort((first, second) => first.data.name.localeCompare(second.data.name, 'en-US', { usage: 'sort' })); + + return authors as unknown as CollectionEntry<'authors'>[]; +} + +export async function getAuthor(authorId: string) { + return getEntry('authors', authorId) as unknown as Promise | undefined>; +}