Skip to content

Commit 435f8ee

Browse files
author
Eva Decker
authored
content: Make authors optional (#240)
1 parent 01eac2b commit 435f8ee

5 files changed

Lines changed: 83 additions & 75 deletions

File tree

src/components/ArticleLink.astro

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
import { Image } from "astro:assets";
3+
import { getEntries, type ReferenceDataEntry } from "astro:content";
34
import dayjs from "dayjs";
45
import utc from "dayjs/plugin/utc";
56
@@ -10,14 +11,20 @@ interface Props {
1011
date: Date;
1112
url: string;
1213
subhead?: string;
13-
authors?: string[];
14+
authors?: ReferenceDataEntry<"authors", string>[];
1415
description?: string;
1516
image?: ImageMetadata;
1617
imageAlt?: string;
1718
}
1819
1920
const { title, date, url, subhead, authors, description, image, imageAlt } =
2021
Astro.props;
22+
23+
const authorsNames = authors
24+
? await getEntries(authors).then((authors) =>
25+
authors.map((author) => author.data.name),
26+
)
27+
: undefined;
2128
---
2229

2330
<article class="article">
@@ -28,7 +35,11 @@ const { title, date, url, subhead, authors, description, image, imageAlt } =
2835
<h2 class="title">
2936
<a href={url} class="title-text">{title}</a>
3037
</h2>
31-
{authors && <strong class="authors">By {authors.join(", ")}</strong>}
38+
{
39+
authorsNames && (
40+
<strong class="authors">By {authorsNames.join(", ")}</strong>
41+
)
42+
}
3243
{description && <p class="description">{description}</p>}
3344
{
3445
image && imageAlt && (

src/content.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const collections = {
6161
.or(z.date())
6262
.transform((v) => new Date(v)),
6363
annotation: z.custom<RoughAnnotationType>().optional(),
64-
authors: z.array(reference("authors")),
64+
authors: z.array(reference("authors")).optional(),
6565
image: image().optional(),
6666
imageAlt: z.string().optional(),
6767
}),

src/content/posts/becoming-sloane.mdx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ title: Becoming Sloane
33
description: >-
44
In his own words, Sloane Patridge shares the experience of changing their name in Massachusetts with the help of Namesake and MTPC.
55
publishDate: 2025-08-26
6-
authors:
7-
- eva-decker
86
image: ../../assets/images/posts/becoming-sloane/becoming-sloane.png
97
imageAlt: "The text 'Becoming Sloane' is rendered in a variety of fonts as if clipped from different newspaper and magazine pages."
108
---

src/pages/blog/[id].astro

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ if (!post) return Astro.redirect("/404");
2828
const { data } = post;
2929
const { Content, headings } = await render(post);
3030
31-
const authors = await getEntries(data.authors);
31+
const authors = data.authors ? await getEntries(data.authors) : [];
3232
---
3333

3434
<ProseLayout
@@ -39,61 +39,65 @@ const authors = await getEntries(data.authors);
3939
annotation={data.annotation ?? "highlight"}
4040
headings={headings}
4141
>
42-
<div class="authors" slot="after-header">
43-
{
44-
authors.map((author) => (
45-
<span class="author">
46-
{author.data.avatar && (
47-
<Image
48-
class="avatar"
49-
src={author.data.avatar}
50-
alt={`${author.data.name} photo`}
51-
width={32}
52-
densities={[1, 2]}
53-
/>
54-
)}
55-
<div class="author-info">
56-
<strong>{author.data.name}</strong>
57-
<span class="role">{author.data.role}</span>
58-
</div>
59-
</span>
60-
))
61-
}
62-
</div>
63-
<Content />
64-
<div class="bios" slot="after-content">
65-
{
66-
authors.map(({ data: { avatar, name, bio, socialLinks } }) => (
67-
<div class="bio">
68-
<div class="bio-avatar">
69-
<Image
70-
class="avatar"
71-
src={avatar}
72-
alt={`${name} photo`}
73-
width={80}
74-
height={80}
75-
densities={[1, 2]}
76-
/>
77-
</div>
78-
<div class="content">
79-
<strong>{name}</strong>
80-
<p>{bio}</p>
81-
{socialLinks && socialLinks.length > 0 && (
82-
<ul class="social">
83-
{socialLinks.map((socialLink) => (
84-
<li>
85-
<a href={socialLink.url} target="_blank">
86-
{socialLink.name}
87-
</a>
88-
</li>
89-
))}
90-
</ul>
42+
{
43+
authors.length > 0 && (
44+
<div class="authors" slot="after-header">
45+
{authors.map((author) => (
46+
<span class="author">
47+
{author.data.avatar && (
48+
<Image
49+
class="avatar"
50+
src={author.data.avatar}
51+
alt={`${author.data.name} photo`}
52+
width={32}
53+
densities={[1, 2]}
54+
/>
9155
)}
56+
<div class="author-info">
57+
<strong>{author.data.name}</strong>
58+
<span class="role">{author.data.role}</span>
59+
</div>
60+
</span>
61+
))}
62+
</div>
63+
)
64+
}
65+
<Content />
66+
{
67+
authors.length > 0 && (
68+
<div class="bios" slot="after-content">
69+
{authors.map(({ data: { avatar, name, bio, socialLinks } }) => (
70+
<div class="bio">
71+
<div class="bio-avatar">
72+
<Image
73+
class="avatar"
74+
src={avatar}
75+
alt={`${name} photo`}
76+
width={80}
77+
height={80}
78+
densities={[1, 2]}
79+
/>
80+
</div>
81+
<div class="content">
82+
<strong>{name}</strong>
83+
<p>{bio}</p>
84+
{socialLinks && socialLinks.length > 0 && (
85+
<ul class="social">
86+
{socialLinks.map((socialLink) => (
87+
<li>
88+
<a href={socialLink.url} target="_blank">
89+
{socialLink.name}
90+
</a>
91+
</li>
92+
))}
93+
</ul>
94+
)}
95+
</div>
9296
</div>
93-
</div>
94-
))
95-
}
96-
</div>
97+
))}
98+
</div>
99+
)
100+
}
97101
</ProseLayout>
98102

99103
<style lang="scss">

src/pages/blog/index.astro

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,17 @@ const sortedPosts = posts.sort(
1919
<div class="posts">
2020
{
2121
sortedPosts.length ? (
22-
sortedPosts.map(async (post) => {
23-
const authors = await getEntries(post.data.authors);
24-
const authorsNames = authors.map((author) => author.data.name);
25-
26-
return (
27-
<ArticleLink
28-
authors={authorsNames}
29-
title={post.data.title}
30-
date={post.data.publishDate}
31-
url={`/blog/${post.id}`}
32-
description={post.data.description}
33-
image={post.data.image}
34-
imageAlt={post.data.imageAlt}
35-
/>
36-
);
37-
})
22+
sortedPosts.map((post) => (
23+
<ArticleLink
24+
authors={post.data.authors}
25+
title={post.data.title}
26+
date={post.data.publishDate}
27+
url={`/blog/${post.id}`}
28+
description={post.data.description}
29+
image={post.data.image}
30+
imageAlt={post.data.imageAlt}
31+
/>
32+
))
3833
) : (
3934
<p>No posts found.</p>
4035
)

0 commit comments

Comments
 (0)