| 1 |
--- |
| 2 |
import { Image } from 'astro:assets'; |
| 3 |
import CopyPageButton from './CopyPageButton.astro'; |
| 4 |
import { getFile } from '../lib/content'; |
| 5 |
import { formatDate } from '../lib/utils'; |
| 6 |
|
| 7 |
const { title, description, file, collection } = Astro.props; |
| 8 |
|
| 9 |
const isEssay = !!file && collection === 'essays'; |
| 10 |
|
| 11 |
let byline = null; |
| 12 |
|
| 13 |
const teamImages = import.meta.glob('/src/assets/img/team/*.{png,jpg,jpeg,webp}', {eager: true}); |
| 14 |
|
| 15 |
if (isEssay) { |
| 16 |
const fm = file.frontmatter; |
| 17 |
const teamFile = await getFile('team.yaml'); |
| 18 |
const team = teamFile?.frontmatter || []; |
| 19 |
|
| 20 |
const authors = (fm.authors || []).map((name) => { |
| 21 |
const member = team.find((m) => m.name === name); |
| 22 |
const image = member?.image ? (teamImages[member.image]?.default ?? member.image) : undefined; |
| 23 |
return { name, url: member?.url, image }; |
| 24 |
}); |
| 25 |
|
| 26 |
const created = fm.created ? (fm.created instanceof Date ? fm.created : new Date(fm.created)) : undefined; |
| 27 |
const modified = fm.modified ? (fm.modified instanceof Date ? fm.modified : new Date(fm.modified)) : undefined; |
| 28 |
|
| 29 |
const essayRaw = import.meta.glob('/src/content/essays/*.{md,mdx}', { query: '?raw', import: 'default', eager: true }); |
| 30 |
let readingMinutes; |
| 31 |
const raw = essayRaw[`/src/content/${file.path}`]; |
| 32 |
if (raw) { |
| 33 |
const body = raw.replace(/^---[\s\S]*?---/, ''); |
| 34 |
readingMinutes = Math.max(1, Math.round(body.trim().split(/\s+/).length / 250)); |
| 35 |
} |
| 36 |
|
| 37 |
byline = { authors, created, modified, readingMinutes }; |
| 38 |
} |
| 39 |
|
| 40 |
const showModified = byline?.modified && byline?.created && |
| 41 |
byline.modified.toDateString() !== byline.created.toDateString(); |
| 42 |
|
| 43 |
const primaryAuthor = byline?.authors[0]; |
| 44 |
--- |
| 45 |
|
| 46 |
<header class="not-prose page-header"> |
| 47 |
<div class="title-row"> |
| 48 |
<h1 set:html={title} /> |
| 49 |
{file && ( |
| 50 |
<div class="actions"> |
| 51 |
<CopyPageButton file={file} /> |
| 52 |
</div> |
| 53 |
)} |
| 54 |
</div> |
| 55 |
|
| 56 |
{byline ? ( |
| 57 |
<div class="byline"> |
| 58 |
{primaryAuthor?.image ? ( |
| 59 |
<div class="avatar"> |
| 60 |
<Image |
| 61 |
src={primaryAuthor.image} |
| 62 |
alt="" |
| 63 |
width={80} |
| 64 |
height={80} |
| 65 |
loading="eager" |
| 66 |
/> |
| 67 |
</div> |
| 68 |
) : byline.authors.length > 0 && ( |
| 69 |
<div class="avatar placeholder"> |
| 70 |
<i class="icon-[mdi--account-outline]" aria-hidden="true"></i> |
| 71 |
</div> |
| 72 |
)} |
| 73 |
|
| 74 |
<div class:list={["meta", byline.authors.length > 0 && "has-avatar"]}> |
| 75 |
{byline.authors.length > 0 && ( |
| 76 |
<span class="authors"> |
| 77 |
{byline.authors.map((author, i) => ( |
| 78 |
<> |
| 79 |
{i > 0 && ", "} |
| 80 |
{author.url ? ( |
| 81 |
<a href={author.url} |
| 82 |
target="_blank" |
| 83 |
rel="author noopener noreferrer"> |
| 84 |
{author.name} |
| 85 |
</a> |
| 86 |
) : ( |
| 87 |
<span>{author.name}</span> |
| 88 |
)} |
| 89 |
</> |
| 90 |
))} |
| 91 |
</span> |
| 92 |
)} |
| 93 |
|
| 94 |
{byline.created && ( |
| 95 |
<span class="dates"> |
| 96 |
{byline.readingMinutes && ( |
| 97 |
<span>{byline.readingMinutes} min read · </span> |
| 98 |
)} |
| 99 |
<time datetime={byline.created.toISOString().slice(0, 10)}> |
| 100 |
{formatDate(byline.created, 'long')} |
| 101 |
</time> |
| 102 |
{showModified && ( |
| 103 |
<span> |
| 104 |
{" · updated "} |
| 105 |
<time datetime={byline.modified.toISOString().slice(0, 10)}> |
| 106 |
{formatDate(byline.modified, 'long')} |
| 107 |
</time> |
| 108 |
</span> |
| 109 |
)} |
| 110 |
</span> |
| 111 |
)} |
| 112 |
</div> |
| 113 |
</div> |
| 114 |
) : description && ( |
| 115 |
<p class="description">{description}</p> |
| 116 |
)} |
| 117 |
</header> |
| 118 |
|
| 119 |
<style> |
| 120 |
.page-header { |
| 121 |
margin-top: 1rem; |
| 122 |
margin-bottom: 1.5rem; |
| 123 |
display: flex; |
| 124 |
flex-direction: column; |
| 125 |
gap: 0.875rem; |
| 126 |
|
| 127 |
@media (min-width: 640px) { |
| 128 |
margin-top: 1.5rem; |
| 129 |
margin-bottom: 2rem; |
| 130 |
gap: 1rem; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
.title-row { |
| 135 |
display: flex; |
| 136 |
align-items: center; |
| 137 |
justify-content: space-between; |
| 138 |
gap: 0.5rem; |
| 139 |
flex-wrap: wrap; |
| 140 |
|
| 141 |
@media (min-width: 640px) { gap: 0.75rem; } |
| 142 |
} |
| 143 |
|
| 144 |
h1 { |
| 145 |
flex: 1; |
| 146 |
min-width: 0; |
| 147 |
font-family: var(--font-chicago); |
| 148 |
font-size: 1.5rem; |
| 149 |
font-weight: 700; |
| 150 |
color: light-dark(var(--color-neutral-900), var(--color-neutral-100)); |
| 151 |
|
| 152 |
@media (min-width: 640px) { font-size: 2em; line-height: 3rem; } |
| 153 |
} |
| 154 |
|
| 155 |
.actions { |
| 156 |
display: flex; |
| 157 |
align-items: center; |
| 158 |
gap: 0.375rem; |
| 159 |
flex-shrink: 0; |
| 160 |
|
| 161 |
@media (min-width: 640px) { gap: 0.5rem; } |
| 162 |
} |
| 163 |
|
| 164 |
.byline { |
| 165 |
display: flex; |
| 166 |
align-items: center; |
| 167 |
} |
| 168 |
|
| 169 |
.avatar { |
| 170 |
width: 2.5rem; |
| 171 |
height: 2.5rem; |
| 172 |
border-radius: 9999px; |
| 173 |
overflow: hidden; |
| 174 |
flex-shrink: 0; |
| 175 |
background: light-dark(var(--color-neutral-200), var(--color-neutral-800)); |
| 176 |
|
| 177 |
:global(img) { |
| 178 |
width: 100%; |
| 179 |
height: 100%; |
| 180 |
object-fit: cover; |
| 181 |
} |
| 182 |
|
| 183 |
&.placeholder { |
| 184 |
display: flex; |
| 185 |
align-items: center; |
| 186 |
justify-content: center; |
| 187 |
|
| 188 |
i { |
| 189 |
width: 1.25rem; |
| 190 |
height: 1.25rem; |
| 191 |
color: light-dark(var(--color-neutral-400), var(--color-neutral-600)); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
.meta { |
| 197 |
display: flex; |
| 198 |
flex-direction: column; |
| 199 |
gap: 0.375rem; |
| 200 |
|
| 201 |
&.has-avatar { margin-inline-start: 1rem; } |
| 202 |
} |
| 203 |
|
| 204 |
.authors { |
| 205 |
font-family: var(--font-chicago); |
| 206 |
font-size: 0.875rem; |
| 207 |
color: light-dark(var(--color-neutral-900), var(--color-neutral-100)); |
| 208 |
|
| 209 |
a { |
| 210 |
text-underline-offset: 0.25em; |
| 211 |
|
| 212 |
@media (hover: hover) { |
| 213 |
&:hover { |
| 214 |
color: light-dark(var(--color-blue-600), var(--color-blue-300)); |
| 215 |
text-decoration: underline; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
&:is(:focus-visible, :active) { |
| 220 |
color: light-dark(var(--color-blue-600), var(--color-blue-300)); |
| 221 |
text-decoration: underline; |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
.dates { |
| 227 |
font-size: 0.75rem; |
| 228 |
font-family: var(--font-mono); |
| 229 |
font-variant-numeric: tabular-nums; |
| 230 |
color: light-dark(var(--color-neutral-600), var(--color-neutral-400)); |
| 231 |
} |
| 232 |
|
| 233 |
.description { |
| 234 |
font-weight: 300; |
| 235 |
font-size: 1rem; |
| 236 |
color: light-dark(var(--color-neutral-650), var(--color-neutral-350)); |
| 237 |
|
| 238 |
@media (min-width: 640px) { font-size: 1.125rem; } |
| 239 |
@media (min-width: 1024px) { font-size: 1.25rem; } |
| 240 |
} |
| 241 |
</style> |