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 ```ts
import { z } from "astro:content"; import { SITE } from "@config";
import { defineCollection, z } from "astro:content";
export const blogSchema = z const blog = defineCollection({
.object({ type: "content",
author: z.string().optional(), schema: ({ image }) =>
pubDatetime: z.date(), z.object({
title: z.string(), // others...
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(), canonicalURL: z.string().optional(),
readingTime: z.string().optional(), // 👈🏻 readingTime frontmatter 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. Step (5) Create a new file called `getPostsWithRT.ts` under `src/utils` directory.
```ts ```ts
import type { BlogFrontmatter } from "@content/_schemas";
import type { MarkdownInstance } from "astro"; import type { MarkdownInstance } from "astro";
import slugify from "./slugify"; import slugify from "./slugify";
import type { CollectionEntry } from "astro:content"; import type { CollectionEntry } from "astro:content";
export const getReadingTime = async () => { export const getReadingTime = async () => {
// Get all posts using glob. This is to get the updated frontmatter // Get all posts using glob. This is to get the updated frontmatter
const globPosts = import.meta.glob<MarkdownInstance<BlogFrontmatter>>( const globPosts = import.meta.glob("../content/blog/*.md") as Promise<
"../content/blog/*.md" CollectionEntry<"blog">["data"][]
); >;
// Then, set those frontmatter value in a JS Map with key value pair // Then, set those frontmatter value in a JS Map with key value pair
const mapFrontmatter = new Map(); const mapFrontmatter = new Map();