Skip to content

Commit 7c087ab

Browse files
committed
create excerpts from post body without the need for addl. metadata. add formatted publication date.
1 parent 3997e16 commit 7c087ab

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/components/PostSummary.astro

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
---
2+
import type { RenderedContent } from 'astro:content';
23
interface Post {
34
id: string;
45
data: {
6+
date: Date;
57
title: string;
68
description?: string;
79
imagePath?: string;
810
imageAlt?: string;
911
};
12+
rendered?: RenderedContent;
1013
}
1114
1215
export interface Props {
@@ -15,7 +18,31 @@ export interface Props {
1518
1619
const { post } = Astro.props;
1720
const { id, data } = post;
18-
const { title, description, imageAlt, imagePath } = data;
21+
const { title, date } = data;
22+
const html: string = post.rendered?.html ?? '';
23+
24+
// retrieve and format publication date for the given post.
25+
const datePublished = new Date(date);
26+
let formattedDate = null;
27+
if (!Number.isNaN(datePublished.valueOf())) {
28+
formattedDate = datePublished.toLocaleDateString('en-US', {
29+
year: 'numeric',
30+
month: 'short',
31+
day: 'numeric',
32+
});
33+
}
34+
35+
// retrieve and truncate the post body after the first paragraph.
36+
// this makes it consistent with Jekyll's default behavior for
37+
// creating post excerpts.
38+
// see https://jekyllrb.com/docs/posts/#post-excerpts
39+
const closingParagraph = '</p>';
40+
const idxClosingParagraph = html.indexOf(closingParagraph);
41+
const idxCutOff = idxClosingParagraph + closingParagraph.length;
42+
let content = html;
43+
if (idxClosingParagraph !== -1 && idxCutOff < html.length) {
44+
content = html.slice(0, idxCutOff);
45+
}
1946
---
2047

2148
<style>
@@ -29,7 +56,9 @@ const { title, description, imageAlt, imagePath } = data;
2956
</style>
3057

3158
<li>
32-
<a href={`/posts/${id}`}>{title}</a>
33-
{imagePath && <img src={imagePath} alt={imageAlt} />}
34-
{description && <p>{description}</p>}
59+
{formattedDate && <span class="post-meta">{formattedDate}</span>}
60+
<h2>
61+
<a class="post-link" href={`/posts/${id}`}>{title}</a>
62+
</h2>
63+
<Fragment set:html={content} />
3564
</li>

0 commit comments

Comments
 (0)