Skip to content

Commit f4e8281

Browse files
authored
Merge pull request #5 from milan-codes/develop
Added reading time to posts
2 parents 4afee1d + f4862d8 commit f4e8281

3 files changed

Lines changed: 26 additions & 17 deletions

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.astro

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/pages/blog/index.astro

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
import BlogPostCard from "../../components/BlogPostCard.svelte";
3+
import Navbar from "../../components/Navbar.svelte";
4+
import BaseLayout from "../../layouts/BaseLayout.astro";
5+
const allPosts = await Astro.glob("./*.md");
6+
const nonDraftPosts = allPosts.filter((post) => !post.frontmatter.draft);
7+
const orderedPosts = nonDraftPosts.sort((a, b) => {
8+
const aDate = new Date(a.frontmatter.pubDate);
9+
const bDate = new Date(b.frontmatter.pubDate);
10+
return bDate.valueOf() - aDate.valueOf();
11+
});
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+
});
20+
---
21+
22+
<BaseLayout title="Milán Herke - Blog">
23+
<Navbar />
24+
{postsWithReadingTime.map((post) => <BlogPostCard post={post} />)}
25+
</BaseLayout>

0 commit comments

Comments
 (0)