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 {
|
import {
|
||||||
getTaxonomyMap,
|
getTaxonomyMap,
|
||||||
getTaxonomyPath,
|
getTaxonomyPath,
|
||||||
|
getDescendantSlugs,
|
||||||
type TaxonomyItem,
|
type TaxonomyItem,
|
||||||
} from "@/utils/taxonomy";
|
} from "@/utils/taxonomy";
|
||||||
import { buildPaginatedPaths } from "@/utils/paginate";
|
import { buildPaginatedPaths } from "@/utils/paginate";
|
||||||
@@ -32,8 +33,11 @@ export async function getStaticPaths() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return Array.from(catSlugs).flatMap((slug) => {
|
return Array.from(catSlugs).flatMap((slug) => {
|
||||||
|
const descendants = getDescendantSlugs("category", slug);
|
||||||
|
const allSlugs = [slug, ...descendants];
|
||||||
|
|
||||||
const catPosts = posts
|
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());
|
.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf());
|
||||||
|
|
||||||
const fullPath = getTaxonomyPath("category", slug);
|
const fullPath = getTaxonomyPath("category", slug);
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import Pagination from "@/components/ui/Pagination.astro";
|
|||||||
import {
|
import {
|
||||||
getTaxonomyMap,
|
getTaxonomyMap,
|
||||||
getTaxonomyPath,
|
getTaxonomyPath,
|
||||||
|
getDescendantSlugs,
|
||||||
type TaxonomyItem,
|
type TaxonomyItem,
|
||||||
} from "@/utils/taxonomy";
|
} from "@/utils/taxonomy";
|
||||||
import { buildPaginatedPaths } from "@/utils/paginate";
|
import { buildPaginatedPaths } from "@/utils/paginate";
|
||||||
@@ -32,8 +33,13 @@ export async function getStaticPaths() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return Array.from(destSlugs).flatMap((slug) => {
|
return Array.from(destSlugs).flatMap((slug) => {
|
||||||
|
const descendants = getDescendantSlugs("destination", slug);
|
||||||
|
const allSlugs = [slug, ...descendants];
|
||||||
|
|
||||||
const destPosts = posts
|
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());
|
.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf());
|
||||||
|
|
||||||
const fullPath = getTaxonomyPath("destination", slug);
|
const fullPath = getTaxonomyPath("destination", slug);
|
||||||
@@ -88,11 +94,9 @@ const metadata = {
|
|||||||
subtitle: "mx-auto max-w-3xl text-xl text-muted-foreground",
|
subtitle: "mx-auto max-w-3xl text-xl text-muted-foreground",
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div class="mb-8 text-center">
|
<div class="mb-8 text-center">
|
||||||
<a href="/blog" class="text-primary hover:underline">← Quay lại blog</a>
|
<a href="/blog" class="text-primary hover:underline">← Quay lại blog</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="grid gap-8 md:grid-cols-2 lg:grid-cols-3">
|
<div class="grid gap-8 md:grid-cols-2 lg:grid-cols-3">
|
||||||
{posts.map((post) => <PostItem post={post} />)}
|
{posts.map((post) => <PostItem post={post} />)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -77,3 +77,26 @@ export function getTaxonomyItem(
|
|||||||
): TaxonomyItem | undefined {
|
): TaxonomyItem | undefined {
|
||||||
return getTaxonomyMap(type).get(slug);
|
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