File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11---
22import { getCollection } from ' astro:content' ;
33import 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
66export 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 },
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { getCollection } from 'astro:content';
33import Layout from ' ../layouts/Layout.astro' ;
44import FormattedDate from ' ../components/FormattedDate.astro' ;
55import { SITE_TITLE } from ' ../consts' ;
6+ import calculateReadingTime from ' ../utils/calculateReadingTime'
67
78// Get all blog posts
89const 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments