Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update est readingTime blog post #409

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions src/content/blog/how-to-add-an-estimated-reading-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ import { SITE } from "@config";
import { defineCollection, z } from "astro:content";

const blog = defineCollection({
type: "content",
type: "content_layer",
loader: glob({ pattern: "**/*.md", base: "./src/content/blog" }),
schema: ({ image }) =>
z.object({
// others...
canonicalURL: z.string().optional(),
readingTime: z.string().optional(), // 👈🏻 readingTime frontmatter
// others...
}),
});

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

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

```ts
---
Expand Down Expand Up @@ -217,7 +219,7 @@ Files that use `getSortedPosts` function are as follow
- src/pages/index.astro
- src/pages/search.astro
- src/pages/rss.xml.ts
- src/pages/posts/index.astro
- src/pages/posts/[...page].astro
- src/pages/posts/[slug]/index.astro
- src/utils/getPostsByTag.ts

Expand All @@ -241,21 +243,20 @@ const postsByTag = await getPostsByTag(posts, tag); // new code ✅
Moreover, update the `getStaticPaths` of `src/pages/tags/[tag]/[page].astro` like this:

```ts
export async function getStaticPaths() {
export async function getStaticPaths({ paginate }: GetStaticPathsOptions) {
const posts = await getCollection("blog");

const tags = getUniqueTags(posts);

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

return totalPages.map(page => ({
params: { tag, page: String(page) },
props: { tag, tagName },
}));
return paginate(tagPosts, {
params: { tag },
props: { tagName },
pageSize: SITE.postPerPage,
});
})
);

Expand All @@ -274,26 +275,29 @@ But in this section, I'm gonna show you how I would display `readingTime` in my
Step (1) Update `Datetime` component to display `readingTime`

```tsx
import { LOCALE } from "@config";
// other codes

export interface Props {
datetime: string | Date;
interface Props extends DatetimesProps, EditPostProps {
size?: "sm" | "lg";
className?: string;
readingTime?: string; // new type
readingTime: string | undefined; // new type
}

export default function Datetime({
datetime,
pubDatetime,
modDatetime,
size = "sm",
className,
className = "",
editPost,
postId,
readingTime, // new prop
}: Props) {
return (
// other codes
<span className={`italic ${size === "sm" ? "text-sm" : "text-base"}`}>
<FormattedDatetime pubDatetime={pubDatetime} modDatetime={modDatetime} />
<span> ({readingTime})</span> {/* display reading time */}
{size === "lg" && <EditPost editPost={editPost} postId={postId} />}
</span>
// other codes
);
Expand All @@ -302,11 +306,11 @@ export default function Datetime({

Step (2) Then, pass `readingTime` props from its parent component.

file: Card.tsx
file: `Card.tsx`

```ts
export default function Card({ href, frontmatter, secHeading = true }: Props) {
const { title, pubDatetime, modDatetime description, readingTime } = frontmatter;
const { title, pubDatetime, modDatetime description, readingTime } = frontmatter; // don't forget to add readingTime here too
return (
...
<Datetime
Expand All @@ -319,7 +323,7 @@ export default function Card({ href, frontmatter, secHeading = true }: Props) {
}
```

file: PostDetails.tsx
file: `PostDetails.astro`

```jsx
// Other Codes
Expand Down