feat: 优化重定向的写法

This commit is contained in:
KazooTTT
2025-02-21 15:44:50 +08:00
parent 2fd907f3e8
commit 4288d472c7
11 changed files with 113 additions and 300 deletions

View File

@ -1,31 +0,0 @@
---
import type { CollectionEntry } from "astro:content";
import { getAllPosts, getUniqueCategories, getUniqueTags } from "@/data/post";
import { collectionDateSort } from "@/utils/date";
import type { GetStaticPaths, Page } from "astro";
export const getStaticPaths = (async ({ paginate }) => {
const MAX_POSTS_PER_PAGE = 20;
const MAX_TAGS = 7;
const MAX_CATEGORIES = 7;
const allPosts = await getAllPosts();
const uniqueTags = getUniqueTags(allPosts).slice(0, MAX_TAGS);
const uniqueCategories = getUniqueCategories(allPosts).slice(0, MAX_CATEGORIES);
const postsCount = allPosts.length;
return paginate(allPosts.sort(collectionDateSort), {
pageSize: MAX_POSTS_PER_PAGE,
props: { uniqueTags, uniqueCategories, postsCount },
});
}) satisfies GetStaticPaths;
interface Props {
page: Page<CollectionEntry<"post">>;
uniqueTags: string[];
uniqueCategories: string[];
postsCount: number;
}
const currentPath = Astro.url.pathname;
const newPath = currentPath.replace("/blog/", "/posts/");
return Astro.redirect(newPath);
---

View File

@ -1,17 +0,0 @@
---
import { getAllPosts } from "@/data/post";
import type { GetStaticPaths } from "astro";
// if you're using an adaptor in SSR mode, getStaticPaths wont work -> https://docs.astro.build/en/guides/routing/#modifying-the-slug-example-for-ssr
export const getStaticPaths = (async () => {
const blogEntries = await getAllPosts();
return blogEntries.map((post) => ({
params: { slug: post.id },
props: { post },
}));
}) satisfies GetStaticPaths;
const currentPath = Astro.url.pathname;
const newPath = currentPath.replace("/blog/", "/posts/");
return Astro.redirect(newPath);
---

View File

@ -1,74 +0,0 @@
---
import type { CollectionEntry } from "astro:content";
import Pagination from "@/components/Paginator.astro";
import PostPreview from "@/components/blog/PostPreview.astro";
import { getAllPosts, getUniqueCategories } from "@/data/post";
import PageLayout from "@/layouts/Base.astro";
import { collectionDateSort } from "@/utils/date";
import type { GetStaticPaths, Page } from "astro";
export const getStaticPaths: GetStaticPaths = async ({ paginate }) => {
const allPosts = await getAllPosts();
const sortedPosts = allPosts.sort(collectionDateSort);
const uniqueCategories = getUniqueCategories(sortedPosts);
return uniqueCategories.flatMap((category) => {
const filterPosts = sortedPosts.filter((post) => post.data.category === category);
return paginate(filterPosts, {
pageSize: 20,
params: { category },
});
});
};
interface Props {
page: Page<CollectionEntry<"post">>;
}
const { page } = Astro.props;
const { category } = Astro.params;
const meta = {
description: `View all posts with the category - ${category}`,
title: `Category: ${category}`,
};
const paginationProps = {
...(page.url.prev && {
prevUrl: {
text: "← Previous Categories",
url: page.url.prev,
},
}),
...(page.url.next && {
nextUrl: {
text: "Next Categories →",
url: page.url.next,
},
}),
};
---
<PageLayout meta={meta}>
<div class="mb-6 flex items-center">
<h1 class="sr-only">Posts with the category {category}</h1>
<a class="title text-accent" href="/categories/"
><span class="sr-only">All {" "}</span>Categories</a
>
<span aria-hidden="true" class="ms-2 me-3 text-xl">→</span>
<span aria-hidden="true" class="text-xl">#{category}</span>
</div>
<section aria-labelledby={`categories-${category}`}>
<h2 id={`categories-${category}`} class="sr-only">Post List</h2>
<ul class="space-y-4">
{
page.data.map((p) => (
<li class="grid gap-2 sm:grid-cols-[auto_1fr]">
<PostPreview as="h2" post={p} />
</li>
))
}
</ul>
<Pagination {...paginationProps} />
</section>
</PageLayout>

View File

@ -1,35 +0,0 @@
---
import { getAllPosts, getUniqueCategoriesWithCount } from "@/data/post";
import PageLayout from "@/layouts/Base.astro";
const allPosts = await getAllPosts();
const allCategories = getUniqueCategoriesWithCount(allPosts);
const meta = {
description: "A list of all the categories I've written about in my posts",
title: "All Categories",
};
---
<PageLayout meta={meta}>
<h1 class="title mb-6">Categories</h1>
<ul class="space-y-4">
{
allCategories.map(([item, val]) => (
<li class="flex items-center gap-x-2">
<a
class="cactus-link inline-block"
data-astro-prefetch
href={`/categories/${item}/`}
title={`View posts with the category: ${item}`}
>
&#35;{item}
</a>
<span class="inline-block">
- {val} Post{val > 1 && "s"}
</span>
</li>
))
}
</ul>
</PageLayout>

View File

@ -1,25 +0,0 @@
---
import { type CollectionEntry, getCollection } from "astro:content";
import { collectionModifiedDateSort } from "@/utils/date";
import type { GetStaticPaths, Page } from "astro";
export const getStaticPaths = (async ({ paginate }) => {
const MAX_NOTES_PER_PAGE = 10;
const allNotes = await getCollection("note");
const notesCount = allNotes.length;
return paginate(allNotes.sort(collectionModifiedDateSort), {
pageSize: MAX_NOTES_PER_PAGE,
props: { notesCount },
});
}) satisfies GetStaticPaths;
interface Props {
page: Page<CollectionEntry<"note">>;
uniqueTags: string[];
notesCount: number;
}
const currentPath = Astro.url.pathname;
const newPath = currentPath.replace("/diary/", "/notes/");
return Astro.redirect(newPath);
---

View File

@ -1,18 +0,0 @@
---
import { getCollection } from "astro:content";
import type { GetStaticPaths } from "astro";
// if you're using an adaptor in SSR mode, getStaticPaths wont work -> https://docs.astro.build/en/guides/routing/#modifying-the-slug-example-for-ssr
export const getStaticPaths = (async () => {
const allNotes = await getCollection("note");
return allNotes.map((note) => ({
params: { slug: note.id },
props: { note },
}));
}) satisfies GetStaticPaths;
const currentPath = Astro.url.pathname;
const newPath = currentPath.replace("/diary/", "/notes/");
return Astro.redirect(newPath);
---

View File

@ -1,23 +0,0 @@
---
import { getAllPosts, getUniqueTags } from "@/data/post";
import { collectionDateSort } from "@/utils/date";
import type { GetStaticPaths } from "astro";
export const getStaticPaths: GetStaticPaths = async ({ paginate }) => {
const allPosts = await getAllPosts();
const sortedPosts = allPosts.sort(collectionDateSort);
const uniqueTags = getUniqueTags(sortedPosts);
return uniqueTags.flatMap((tag) => {
const filterPosts = sortedPosts.filter((post) => post.data.tags.includes(tag));
return paginate(filterPosts, {
pageSize: 20,
params: { tag },
});
});
};
const currentPath = Astro.url.pathname;
const newPath = currentPath.replace("/tag/", "/tags/");
return Astro.redirect(newPath);
---

View File

@ -1,3 +0,0 @@
---
return Astro.redirect("/tags");
---