docs: update reading-time post for astro-paper v3

This commit is contained in:
satnaing
2023-09-24 23:52:38 +06:30
committed by Sat Naing
parent 58c68ee055
commit a5feb4b3ab
@@ -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(),
const blog = defineCollection({
type: "content",
schema: ({ image }) =>
z.object({
// others...
canonicalURL: z.string().optional(),
readingTime: z.string().optional(), // 👈🏻 readingTime frontmatter
})
.strict();
}),
});
export type BlogFrontmatter = z.infer<typeof blogSchema>;
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<MarkdownInstance<BlogFrontmatter>>(
"../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();