mirror of
https://github.com/10h30/blog-balodeplao.git
synced 2026-05-12 15:21:15 +09:00
feat: add getDescendantSlugs function and update category/destination filtering logic
This commit is contained in:
@@ -8,6 +8,7 @@ import Pagination from "@/components/ui/Pagination.astro";
|
||||
import {
|
||||
getTaxonomyMap,
|
||||
getTaxonomyPath,
|
||||
getDescendantSlugs,
|
||||
type TaxonomyItem,
|
||||
} from "@/utils/taxonomy";
|
||||
import { buildPaginatedPaths } from "@/utils/paginate";
|
||||
@@ -32,8 +33,11 @@ export async function getStaticPaths() {
|
||||
);
|
||||
|
||||
return Array.from(catSlugs).flatMap((slug) => {
|
||||
const descendants = getDescendantSlugs("category", slug);
|
||||
const allSlugs = [slug, ...descendants];
|
||||
|
||||
const catPosts = posts
|
||||
.filter((post) => post.data.categories?.includes(slug))
|
||||
.filter((post) => post.data.categories?.some((d) => allSlugs.includes(d)))
|
||||
.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf());
|
||||
|
||||
const fullPath = getTaxonomyPath("category", slug);
|
||||
|
||||
@@ -8,6 +8,7 @@ import Pagination from "@/components/ui/Pagination.astro";
|
||||
import {
|
||||
getTaxonomyMap,
|
||||
getTaxonomyPath,
|
||||
getDescendantSlugs,
|
||||
type TaxonomyItem,
|
||||
} from "@/utils/taxonomy";
|
||||
import { buildPaginatedPaths } from "@/utils/paginate";
|
||||
@@ -32,8 +33,13 @@ export async function getStaticPaths() {
|
||||
);
|
||||
|
||||
return Array.from(destSlugs).flatMap((slug) => {
|
||||
const descendants = getDescendantSlugs("destination", slug);
|
||||
const allSlugs = [slug, ...descendants];
|
||||
|
||||
const destPosts = posts
|
||||
.filter((post) => post.data.destination?.includes(slug))
|
||||
.filter((post) =>
|
||||
post.data.destination?.some((d) => allSlugs.includes(d)),
|
||||
)
|
||||
.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf());
|
||||
|
||||
const fullPath = getTaxonomyPath("destination", slug);
|
||||
@@ -88,11 +94,9 @@ const metadata = {
|
||||
subtitle: "mx-auto max-w-3xl text-xl text-muted-foreground",
|
||||
}}
|
||||
/>
|
||||
|
||||
<div class="mb-8 text-center">
|
||||
<a href="/blog" class="text-primary hover:underline">← Quay lại blog</a>
|
||||
</div>
|
||||
|
||||
<div class="grid gap-8 md:grid-cols-2 lg:grid-cols-3">
|
||||
{posts.map((post) => <PostItem post={post} />)}
|
||||
</div>
|
||||
|
||||
@@ -77,3 +77,26 @@ export function getTaxonomyItem(
|
||||
): TaxonomyItem | undefined {
|
||||
return getTaxonomyMap(type).get(slug);
|
||||
}
|
||||
|
||||
export function getDescendantSlugs(
|
||||
taxonomyType: string,
|
||||
slug: string,
|
||||
): string[] {
|
||||
const map = getTaxonomyMap(taxonomyType);
|
||||
const result: string[] = [];
|
||||
|
||||
for (const [key, item] of map.entries()) {
|
||||
if (key === slug) continue;
|
||||
// Đi ngược từ node này lên root, kiểm tra xem có qua slug không
|
||||
let current: TaxonomyItem | undefined = item;
|
||||
while (current?.parent) {
|
||||
if (current.parent === slug) {
|
||||
result.push(key);
|
||||
break;
|
||||
}
|
||||
current = map.get(current.parent);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user