Skip to content

Commit 397e321

Browse files
authored
docs: update est readingTime blog post (#409)
Resolves #406
1 parent cac8ba0 commit 397e321

1 file changed

Lines changed: 23 additions & 19 deletions

File tree

src/content/blog/how-to-add-an-estimated-reading-time.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,14 @@ import { SITE } from "@config";
7575
import { defineCollection, z } from "astro:content";
7676

7777
const blog = defineCollection({
78-
type: "content",
78+
type: "content_layer",
79+
loader: glob({ pattern: "**/*.md", base: "./src/content/blog" }),
7980
schema: ({ image }) =>
8081
z.object({
8182
// others...
8283
canonicalURL: z.string().optional(),
8384
readingTime: z.string().optional(), // 👈🏻 readingTime frontmatter
85+
// others...
8486
}),
8587
});
8688

@@ -131,7 +133,7 @@ const getPostsWithRT = async (posts: CollectionEntry<"blog">[]) => {
131133
export default getPostsWithRT;
132134
```
133135

134-
Step (6) Refactor `getStaticPaths` of `/src/pages/posts/[slug]/index.astro` as the following
136+
Step (6) Refactor `getStaticPaths` of `src/pages/posts/[slug]/index.astro` as the following
135137

136138
```ts
137139
---
@@ -217,7 +219,7 @@ Files that use `getSortedPosts` function are as follow
217219
- src/pages/index.astro
218220
- src/pages/search.astro
219221
- src/pages/rss.xml.ts
220-
- src/pages/posts/index.astro
222+
- src/pages/posts/[...page].astro
221223
- src/pages/posts/[slug]/index.astro
222224
- src/utils/getPostsByTag.ts
223225
@@ -241,21 +243,20 @@ const postsByTag = await getPostsByTag(posts, tag); // new code ✅
241243
Moreover, update the `getStaticPaths` of `src/pages/tags/[tag]/[page].astro` like this:
242244
243245
```ts
244-
export async function getStaticPaths() {
246+
export async function getStaticPaths({ paginate }: GetStaticPathsOptions) {
245247
const posts = await getCollection("blog");
246-
247248
const tags = getUniqueTags(posts);
248249

249250
// Make sure to await the promises
250251
const paths = await Promise.all(
251252
tags.map(async ({ tag, tagName }) => {
252253
const tagPosts = await getPostsByTag(posts, tag);
253-
const totalPages = getPageNumbers(tagPosts.length);
254254

255-
return totalPages.map(page => ({
256-
params: { tag, page: String(page) },
257-
props: { tag, tagName },
258-
}));
255+
return paginate(tagPosts, {
256+
params: { tag },
257+
props: { tagName },
258+
pageSize: SITE.postPerPage,
259+
});
259260
})
260261
);
261262

@@ -274,26 +275,29 @@ But in this section, I'm gonna show you how I would display `readingTime` in my
274275
Step (1) Update `Datetime` component to display `readingTime`
275276
276277
```tsx
277-
import { LOCALE } from "@config";
278+
// other codes
278279

279-
export interface Props {
280-
datetime: string | Date;
280+
interface Props extends DatetimesProps, EditPostProps {
281281
size?: "sm" | "lg";
282282
className?: string;
283-
readingTime?: string; // new type
283+
readingTime: string | undefined; // new type
284284
}
285285

286286
export default function Datetime({
287-
datetime,
287+
pubDatetime,
288+
modDatetime,
288289
size = "sm",
289-
className,
290+
className = "",
291+
editPost,
292+
postId,
290293
readingTime, // new prop
291294
}: Props) {
292295
return (
293296
// other codes
294297
<span className={`italic ${size === "sm" ? "text-sm" : "text-base"}`}>
295298
<FormattedDatetime pubDatetime={pubDatetime} modDatetime={modDatetime} />
296299
<span> ({readingTime})</span> {/* display reading time */}
300+
{size === "lg" && <EditPost editPost={editPost} postId={postId} />}
297301
</span>
298302
// other codes
299303
);
@@ -302,11 +306,11 @@ export default function Datetime({
302306
303307
Step (2) Then, pass `readingTime` props from its parent component.
304308
305-
file: Card.tsx
309+
file: `Card.tsx`
306310
307311
```ts
308312
export default function Card({ href, frontmatter, secHeading = true }: Props) {
309-
const { title, pubDatetime, modDatetime description, readingTime } = frontmatter;
313+
const { title, pubDatetime, modDatetime description, readingTime } = frontmatter; // don't forget to add readingTime here too
310314
return (
311315
...
312316
<Datetime
@@ -319,7 +323,7 @@ export default function Card({ href, frontmatter, secHeading = true }: Props) {
319323
}
320324
```
321325
322-
file: PostDetails.tsx
326+
file: `PostDetails.astro`
323327
324328
```jsx
325329
// Other Codes

0 commit comments

Comments
 (0)