feat: upgrade to astro 5

This commit is contained in:
Jayden 2024-12-24 06:20:40 +00:00
parent 47f21f8b3c
commit 0704481e0b
16 changed files with 3976 additions and 2671 deletions

View file

@ -9,7 +9,7 @@ import { type CollectionEntry, getCollection } from 'astro:content'
export async function getStaticPaths() {
const authors = await getCollection('authors')
return authors.map((author) => ({
params: { slug: author.slug },
params: { id: author.id },
props: { author },
}))
}
@ -23,7 +23,7 @@ const { author } = Astro.props
const allPosts = await getCollection('blog')
const authorPosts = allPosts
.filter((post) => {
return post.data.authors && post.data.authors.includes(author.slug)
return post.data.authors && post.data.authors.includes(author.id)
})
.sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf())
---

View file

@ -7,17 +7,18 @@ import { badgeVariants } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { Separator } from '@/components/ui/separator'
import Layout from '@/layouts/Layout.astro'
import { formatDate, parseAuthors, readingTime } from '@/lib/utils'
import { parseAuthors } from '@/lib/server-utils'
import { formatDate, readingTime } from '@/lib/utils'
import { Icon } from 'astro-icon/components'
import { Image } from 'astro:assets'
import { type CollectionEntry, getCollection } from 'astro:content'
import { type CollectionEntry, getCollection, render } from 'astro:content'
export async function getStaticPaths() {
const posts = (await getCollection('blog'))
.filter((post) => !post.data.draft)
.sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf())
return posts.map((post) => ({
params: { slug: post.slug },
params: { id: post.id },
props: post,
}))
}
@ -27,28 +28,28 @@ const posts = (await getCollection('blog'))
.filter((post) => !post.data.draft)
.sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf())
function getPostIndex(slug: string): number {
return posts.findIndex((post) => post.slug === slug)
function getPostIndex(id: string): number {
return posts.findIndex((post) => post.id === id)
}
function getPrevPost(slug: string): Props | null {
const postIndex = getPostIndex(slug)
function getPrevPost(id: string): Props | null {
const postIndex = getPostIndex(id)
return postIndex !== -1 && postIndex < posts.length - 1
? posts[postIndex + 1]
: null
}
function getNextPost(slug: string): Props | null {
const postIndex = getPostIndex(slug)
function getNextPost(id: string): Props | null {
const postIndex = getPostIndex(id)
return postIndex > 0 ? posts[postIndex - 1] : null
}
const currentPostSlug = Astro.params.slug
const nextPost = getNextPost(currentPostSlug)
const prevPost = getPrevPost(currentPostSlug)
const currentPostId = Astro.params.id
const nextPost = getNextPost(currentPostId)
const prevPost = getPrevPost(currentPostId)
const post = Astro.props
const { Content, headings } = await post.render()
const { Content, headings } = await render(post)
const authors = await parseAuthors(post.data.authors ?? [])
---
@ -103,7 +104,7 @@ const authors = await parseAuthors(post.data.authors ?? [])
/>
{author.isRegistered ? (
<Link
href={`/authors/${author.slug}`}
href={`/authors/${author.id}`}
underline
class="text-foreground"
>
@ -122,7 +123,7 @@ const authors = await parseAuthors(post.data.authors ?? [])
<div class="flex items-center gap-2">
<span>{formatDate(post.data.date)}</span>
<Separator orientation="vertical" className="h-4" />
<span>{readingTime(post.body)}</span>
<span>{readingTime(post.body!)}</span>
</div>
</div>
<div class="flex flex-wrap justify-center gap-2">

View file

@ -1,5 +1,5 @@
import rss from '@astrojs/rss'
import { SITE } from '@/consts'
import rss from '@astrojs/rss'
import type { APIContext } from 'astro'
import { getCollection } from 'astro:content'
@ -24,7 +24,7 @@ export async function GET(context: APIContext) {
title: item.data.title,
description: item.data.description,
pubDate: item.data.date,
link: `/${item.collection}/${item.slug}/`,
link: `/${item.collection}/${item.id}/`,
})),
})
} catch (error) {

View file

@ -23,7 +23,7 @@ export async function getStaticPaths() {
)
return uniqueTags.map((tag) => ({
params: { slug: tag },
params: { id: tag },
props: {
tag,
posts: posts.filter((post) => post.data.tags?.includes(tag)),