mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 11:01:42 +08:00
docs: update reading-time post for astro-paper v3
This commit is contained in:
@@ -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<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();
|
||||
|
||||
Reference in New Issue
Block a user