Skip to content

Commit 7f63707

Browse files
stopfstedtjrjohnson
authored andcommitted
create excerpts from post body without the need for addl. metadata. add formatted publication date.
1 parent c31aad5 commit 7f63707

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/components/PostSummary.astro

Lines changed: 29 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,27 @@ 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+
// format publication date for the given post.
25+
const formattedDate = date.toLocaleDateString('en-US', {
26+
year: 'numeric',
27+
month: 'short',
28+
day: 'numeric',
29+
});
30+
31+
// retrieve and truncate the post body after the first paragraph.
32+
// this makes it consistent with Jekyll's default behavior for
33+
// creating post excerpts.
34+
// see https://jekyllrb.com/docs/posts/#post-excerpts
35+
const closingParagraph = '</p>';
36+
const idxClosingParagraph = html.indexOf(closingParagraph);
37+
const idxCutOff = idxClosingParagraph + closingParagraph.length;
38+
let content = html;
39+
if (idxClosingParagraph !== -1 && idxCutOff < html.length) {
40+
content = html.slice(0, idxCutOff);
41+
}
1942
---
2043

2144
<style>
@@ -29,7 +52,9 @@ const { title, description, imageAlt, imagePath } = data;
2952
</style>
3053

3154
<li>
32-
<a href={`/posts/${id}`}>{title}</a>
33-
{imagePath && <img src={imagePath} alt={imageAlt} />}
34-
{description && <p>{description}</p>}
55+
{formattedDate && <span class="post-meta">{formattedDate}</span>}
56+
<h2>
57+
<a class="post-link" href={`/posts/${id}`}>{title}</a>
58+
</h2>
59+
<Fragment set:html={content} />
3560
</li>

0 commit comments

Comments
 (0)