Skip to content

Commit f4862d8

Browse files
committed
Added reading time to blog posts
1 parent 3c71af5 commit f4862d8

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

src/components/BlogPostCard.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
</a>
1919
<p class="text-gray-600 dark:text-gray-400">{post.frontmatter.description}</p>
2020
<ul class="flex flex-wrap">
21+
<li class="mr-3 text-gray-400 dark:text-gray-600 text-sm">{post.readingTime} min read</li>
2122
{#each post.frontmatter.tags as tag}
2223
<li class="mr-3 text-gray-400 dark:text-gray-600 text-sm font-medium hover:underline"><a href={`/tags/${tag}`}>{`#${tag}`}</a></li>
2324
{/each}

src/pages/blog/index.astro

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@ const orderedPosts = nonDraftPosts.sort((a, b) => {
99
const bDate = new Date(b.frontmatter.pubDate);
1010
return bDate.valueOf() - aDate.valueOf();
1111
});
12+
const postsWithReadingTime = orderedPosts.map((post) => {
13+
const wordCount = post.rawContent().split(" ").length;
14+
const readingTime = Math.ceil(wordCount / 200);
15+
return {
16+
...post,
17+
readingTime,
18+
};
19+
});
1220
---
1321

1422
<BaseLayout title="Milán Herke - Blog">
1523
<Navbar />
16-
{orderedPosts.map((post) => <BlogPostCard post={post} />)}
24+
{postsWithReadingTime.map((post) => <BlogPostCard post={post} />)}
1725
</BaseLayout>

0 commit comments

Comments
 (0)