Skip to content

Commit 37611db

Browse files
Fix/wrong reading time on main page (#15)
* refactor: moving the calculation of the reading time to a centralized place * fix: wrong estimated read time on main page
1 parent 792d747 commit 37611db

3 files changed

Lines changed: 28 additions & 14 deletions

File tree

src/pages/blog/[...slug].astro

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
import { getCollection } from 'astro:content';
33
import BlogPost from '../../layouts/BlogPost.astro';
4-
import readingTime from 'reading-time'; // Add this import if it doesn't exist yet
4+
import calculateReadingTime from '../../utils/calculateReadingTime'
55
66
export async function getStaticPaths() {
77
const blogEntries = await getCollection('blog');
@@ -12,18 +12,8 @@ export async function getStaticPaths() {
1212
);
1313
1414
return sortedEntries.map((entry, index) => {
15-
// Calculate readingTime from markdown content
16-
const readingTimeResult = readingTime(entry.body);
17-
18-
// Format the read time into a readable string
19-
let readingTimeStr;
20-
if (readingTimeResult.minutes < 1) {
21-
readingTimeStr = 'Under 1 min read';
22-
} else {
23-
// Round up and format to "X minutes read"
24-
const minutes = Math.ceil(readingTimeResult.minutes);
25-
readingTimeStr = `${minutes} min read`;
26-
}
15+
16+
const { readingTimeResult, readingTimeStr } = calculateReadingTime(entry.body)
2717
2818
return {
2919
params: { slug: entry.slug },

src/pages/index.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getCollection } from 'astro:content';
33
import Layout from '../layouts/Layout.astro';
44
import FormattedDate from '../components/FormattedDate.astro';
55
import { SITE_TITLE } from '../consts';
6+
import calculateReadingTime from '../utils/calculateReadingTime'
67
78
// Get all blog posts
89
const allPosts = await getCollection('blog');
@@ -94,7 +95,7 @@ const allTags = [...new Set(allPosts.flatMap(post => post.data.tags || []))].sli
9495
<FormattedDate date={post.data.pubDate} format="long" />
9596
</time>
9697
<span class="relative z-10 rounded-full bg-zinc-100 dark:bg-zinc-800 px-2 sm:px-3 py-1 sm:py-1.5 font-medium text-zinc-600 dark:text-zinc-300 theme-transition-all">
97-
{post.data.readingTime || `${Math.ceil(post.body.length / 2000)} min read`}
98+
{post.data.readingTime || calculateReadingTime(post.body).readingTimeStr}
9899
</span>
99100
</div>
100101

src/utils/calculateReadingTime.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import readingTime from 'reading-time';
2+
3+
const calculateReadingTime = (content: string) => {
4+
// Calculate readingTime from markdown content
5+
const readingTimeResult = readingTime(content);
6+
7+
// Format the read time into a readable string
8+
let readingTimeStr;
9+
if (readingTimeResult.minutes < 1) {
10+
readingTimeStr = 'Under 1 min read';
11+
} else {
12+
// Round up and format to "X minutes read"
13+
const minutes = Math.ceil(readingTimeResult.minutes);
14+
readingTimeStr = `${minutes} min read`;
15+
}
16+
17+
return {
18+
readingTimeStr,
19+
readingTimeResult
20+
}
21+
}
22+
23+
export default calculateReadingTime

0 commit comments

Comments
 (0)