diff --git a/src/content/blog/how-to-add-an-estimated-reading-time.md b/src/content/blog/how-to-add-an-estimated-reading-time.md index 75fe5f8..bc9a01c 100644 --- a/src/content/blog/how-to-add-an-estimated-reading-time.md +++ b/src/content/blog/how-to-add-an-estimated-reading-time.md @@ -65,43 +65,37 @@ export default defineConfig({ }); ``` -Step (4) Add `readingTime` to blog schema (`src/content/_schemas.ts`) +Step (4) Add `readingTime` to blog schema (`src/content/config.ts`) ```ts -import { z } from "astro:content"; +import { SITE } from "@config"; +import { defineCollection, z } from "astro:content"; -export const blogSchema = z - .object({ - author: z.string().optional(), - pubDatetime: z.date(), - title: z.string(), - postSlug: z.string().optional(), - featured: z.boolean().optional(), - draft: z.boolean().optional(), - tags: z.array(z.string()).default(["others"]), - ogImage: z.string().optional(), - description: z.string(), - canonicalURL: z.string().optional(), - readingTime: z.string().optional(), // 👈🏻 readingTime frontmatter - }) - .strict(); +const blog = defineCollection({ + type: "content", + schema: ({ image }) => + z.object({ + // others... + canonicalURL: z.string().optional(), + readingTime: z.string().optional(), // 👈🏻 readingTime frontmatter + }), +}); -export type BlogFrontmatter = z.infer; +export const collections = { blog }; ``` Step (5) Create a new file called `getPostsWithRT.ts` under `src/utils` directory. ```ts -import type { BlogFrontmatter } from "@content/_schemas"; import type { MarkdownInstance } from "astro"; import slugify from "./slugify"; import type { CollectionEntry } from "astro:content"; export const getReadingTime = async () => { // Get all posts using glob. This is to get the updated frontmatter - const globPosts = import.meta.glob>( - "../content/blog/*.md" - ); + const globPosts = import.meta.glob("../content/blog/*.md") as Promise< + CollectionEntry<"blog">["data"][] + >; // Then, set those frontmatter value in a JS Map with key value pair const mapFrontmatter = new Map();