Migrate to astro v2

This commit is contained in:
prototypa
2023-01-21 18:19:18 -05:00
parent b575a89329
commit 09e827c035
9 changed files with 28 additions and 29 deletions

View File

@ -1,7 +1,7 @@
import { z, defineCollection } from 'astro:content';
const blog = defineCollection({
schema: {
schema: z.object({
title: z.string(),
description: z.string().optional(),
image: z.string().optional(),
@ -16,7 +16,7 @@ const blog = defineCollection({
category: z.string().optional(),
tags: z.array(z.string()).optional(),
author: z.string().optional(),
},
}),
slug: ({ defaultSlug, data }) => {
return data.permalink || defaultSlug;
},

1
src/env.d.ts vendored
View File

@ -1 +1,2 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference types="@astrojs/image/client" />

View File

@ -1,11 +1,11 @@
import { getCollection, getEntry } from 'astro:content';
import { getCollection } from 'astro:content';
import type { CollectionEntry } from 'astro:content';
import type { Post } from '~/types';
import { cleanSlug } from './permalinks';
const getNormalizedPost = async (post: CollectionEntry<'blog'>): Promise<Post> => {
const { id, slug = '', data } = post;
const { Content, injectedFrontmatter } = await post.render();
const { Content, remarkPluginFrontmatter } = await post.render();
const { tags = [], category = 'default', author = 'Anonymous', publishDate = new Date(), ...rest } = data;
@ -23,7 +23,7 @@ const getNormalizedPost = async (post: CollectionEntry<'blog'>): Promise<Post> =
Content: Content,
// or 'body' in case you consume from API
readingTime: injectedFrontmatter.readingTime,
readingTime: remarkPluginFrontmatter?.readingTime,
};
};
@ -67,12 +67,14 @@ export const findPostsBySlugs = async (slugs: Array<string>): Promise<Array<Post
export const findPostsByIds = async (ids: Array<string>): Promise<Array<Post>> => {
if (!Array.isArray(ids)) return [];
return await Promise.all(
ids.map(async (id: never) => {
const post = await getEntry('blog', id);
return await getNormalizedPost(post);
})
);
const posts = await fetchPosts();
return ids.reduce(function (r: Array<Post>, id: string) {
posts.some(function (post: Post) {
return id === post.id && r.push(post);
});
return r;
}, []);
};
/** */

View File

@ -2,10 +2,11 @@ import getReadingTime from 'reading-time';
import { toString } from 'mdast-util-to-string';
export function remarkReadingTime() {
return function (tree, { data }) {
return function (tree, file) {
const text = toString(tree);
const readingTime = Math.ceil(getReadingTime(text).minutes);
data.astro.frontmatter.readingTime = readingTime;
const { frontmatter } = file.data.astro;
frontmatter.readingTime = readingTime;
};
}