mirror of
https://github.com/10h30/blog-balodeplao.git
synced 2026-05-12 15:21:15 +09:00
feat: refactor taxonomy data handling for categories, destinations, and tags
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
---
|
||||
import { categoryMap, getCategoryPath } from "@/utils/taxonomy";
|
||||
import { getTaxonomyMap, getTaxonomyPath } from "@/utils/taxonomy";
|
||||
|
||||
export interface Props {
|
||||
categories: string[];
|
||||
@@ -15,10 +15,10 @@ const { categories, class: className = "text-sm" } = Astro.props;
|
||||
{categories.map((category) => (
|
||||
<li class="inline-block relative">
|
||||
<a
|
||||
href={`/category/${getCategoryPath(category)}`}
|
||||
href={`/category/${getTaxonomyPath("category", category)}`}
|
||||
class="inline-block bg-primary/10 hover:bg-primary/20 text-primary hover:text-primary px-3 py-1 rounded-full border border-primary/20 transition-colors duration-200"
|
||||
>
|
||||
{categoryMap.get(category)?.name ?? category}
|
||||
{getTaxonomyMap("category").get(category)?.name ?? category}
|
||||
</a>
|
||||
</li>
|
||||
))}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
---
|
||||
import { destinationMap, getDestinationPath } from "@/utils/taxonomy";
|
||||
import { getTaxonomyMap, getTaxonomyPath } from "@/utils/taxonomy";
|
||||
|
||||
export interface Props {
|
||||
destinations: string[];
|
||||
@@ -15,7 +15,7 @@ const { destinations, class: className = "text-sm" } = Astro.props;
|
||||
{destinations.map((destination) => (
|
||||
<li class="inline-block relative">
|
||||
<a
|
||||
href={`/destination/${getDestinationPath(destination)}`}
|
||||
href={`/destination/${getTaxonomyPath("destination", destination)}`}
|
||||
class="inline-flex items-center gap-1 bg-emerald-500/10 hover:bg-emerald-500/20 text-emerald-700 dark:text-emerald-400 hover:text-emerald-800 dark:hover:text-emerald-300 px-3 py-1 rounded-full border border-emerald-500/20 transition-colors duration-200"
|
||||
>
|
||||
<svg
|
||||
@@ -32,7 +32,8 @@ const { destinations, class: className = "text-sm" } = Astro.props;
|
||||
<path d="M20 10c0 6-8 12-8 12s-8-6-8-12a8 8 0 0 1 16 0Z" />
|
||||
<circle cx="12" cy="10" r="3" />
|
||||
</svg>
|
||||
{destinationMap.get(destination)?.name ?? destination}
|
||||
{getTaxonomyMap("destination").get(destination)?.name ??
|
||||
destination}
|
||||
</a>
|
||||
</li>
|
||||
))}
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
---
|
||||
import type { Page } from "astro";
|
||||
import type { CollectionEntry } from "astro:content";
|
||||
import { Icon } from "astro-icon/components";
|
||||
|
||||
export interface SimplePage {
|
||||
currentPage: number;
|
||||
lastPage: number;
|
||||
url: { prev?: string; next?: string };
|
||||
}
|
||||
|
||||
export interface Props {
|
||||
page: Page<CollectionEntry<"blog">>;
|
||||
page: SimplePage;
|
||||
class?: string;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
---
|
||||
import { tagMap } from "@/utils/taxonomy";
|
||||
import { getTaxonomyMap } from "@/utils/taxonomy";
|
||||
|
||||
export interface Props {
|
||||
tags: string[];
|
||||
@@ -18,7 +18,7 @@ const { tags, class: className = "text-sm" } = Astro.props;
|
||||
href={`/tag/${tag}`}
|
||||
class="inline-block bg-muted/50 hover:bg-muted text-muted-foreground hover:text-foreground px-3 py-1 rounded-full border border-border transition-colors duration-200"
|
||||
>
|
||||
#{tagMap.get(tag)?.name ?? tag}
|
||||
#{getTaxonomyMap("tag").get(tag)?.name ?? tag}
|
||||
</a>
|
||||
</li>
|
||||
))}
|
||||
|
||||
@@ -7,8 +7,8 @@ import Pagination from "@/components/ui/Pagination.astro";
|
||||
import type { Page } from "astro";
|
||||
import type { CollectionEntry } from "astro:content";
|
||||
import {
|
||||
categoryMap,
|
||||
getCategoryPath,
|
||||
getTaxonomyMap,
|
||||
getTaxonomyPath,
|
||||
type TaxonomyItem,
|
||||
} from "@/utils/taxonomy";
|
||||
|
||||
@@ -27,8 +27,10 @@ export async function getStaticPaths() {
|
||||
.filter((post) => post.data.categories?.includes(slug))
|
||||
.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf());
|
||||
|
||||
const fullPath = getCategoryPath(slug);
|
||||
const cat = categoryMap.get(slug) ?? ({ slug, name: slug } as TaxonomyItem);
|
||||
const fullPath = getTaxonomyPath("category", slug);
|
||||
const cat =
|
||||
getTaxonomyMap("category").get(slug) ??
|
||||
({ slug, name: slug } as TaxonomyItem);
|
||||
const totalPages = Math.max(1, Math.ceil(catPosts.length / PAGE_SIZE));
|
||||
|
||||
return Array.from({ length: totalPages }, (_, i) => {
|
||||
@@ -65,7 +67,7 @@ const page = {
|
||||
currentPage,
|
||||
lastPage,
|
||||
url: { prev: prevUrl, next: nextUrl },
|
||||
} as unknown as Page<CollectionEntry<"blog">>;
|
||||
};
|
||||
|
||||
const metadata = {
|
||||
title: cat.name,
|
||||
|
||||
@@ -5,7 +5,7 @@ import Headline from "@/components/ui/Headline.astro";
|
||||
import PostItem from "@/components/blog/PostItem.astro";
|
||||
import Pagination from "@/components/ui/Pagination.astro";
|
||||
import type { GetStaticPathsOptions } from "astro";
|
||||
import { categoryMap } from "@/utils/taxonomy";
|
||||
import { getTaxonomyMap } from "@/utils/taxonomy";
|
||||
|
||||
export async function getStaticPaths({ paginate }: GetStaticPathsOptions) {
|
||||
const allPosts = await getCollection("blog");
|
||||
@@ -31,7 +31,7 @@ export async function getStaticPaths({ paginate }: GetStaticPathsOptions) {
|
||||
const { page } = Astro.props;
|
||||
const { category } = Astro.params;
|
||||
|
||||
const categoryInfo = categoryMap.get(category!);
|
||||
const categoryInfo = getTaxonomyMap("category").get(category!);
|
||||
const displayName = categoryInfo?.name ?? category!;
|
||||
const description = categoryInfo?.description;
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@ import Pagination from "@/components/ui/Pagination.astro";
|
||||
import type { Page } from "astro";
|
||||
import type { CollectionEntry } from "astro:content";
|
||||
import {
|
||||
destinationMap,
|
||||
getDestinationPath,
|
||||
getTaxonomyMap,
|
||||
getTaxonomyPath,
|
||||
type TaxonomyItem,
|
||||
} from "@/utils/taxonomy";
|
||||
|
||||
@@ -27,9 +27,10 @@ export async function getStaticPaths() {
|
||||
.filter((post) => post.data.destination?.includes(slug))
|
||||
.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf());
|
||||
|
||||
const fullPath = getDestinationPath(slug);
|
||||
const fullPath = getTaxonomyPath("destination", slug);
|
||||
const dest =
|
||||
destinationMap.get(slug) ?? ({ slug, name: slug } as TaxonomyItem);
|
||||
getTaxonomyMap("destination").get(slug) ??
|
||||
({ slug, name: slug } as TaxonomyItem);
|
||||
const totalPages = Math.max(1, Math.ceil(destPosts.length / PAGE_SIZE));
|
||||
|
||||
return Array.from({ length: totalPages }, (_, i) => {
|
||||
@@ -66,7 +67,7 @@ const page = {
|
||||
currentPage,
|
||||
lastPage,
|
||||
url: { prev: prevUrl, next: nextUrl },
|
||||
} as unknown as Page<CollectionEntry<"blog">>;
|
||||
};
|
||||
|
||||
const metadata = {
|
||||
title: dest.name,
|
||||
|
||||
@@ -5,7 +5,7 @@ import Headline from "@/components/ui/Headline.astro";
|
||||
import PostItem from "@/components/blog/PostItem.astro";
|
||||
import Pagination from "@/components/ui/Pagination.astro";
|
||||
import type { GetStaticPathsOptions } from "astro";
|
||||
import { tagMap } from "@/utils/taxonomy";
|
||||
import { getTaxonomyMap } from "@/utils/taxonomy";
|
||||
|
||||
export async function getStaticPaths({ paginate }: GetStaticPathsOptions) {
|
||||
const allPosts = await getCollection("blog");
|
||||
@@ -31,7 +31,7 @@ export async function getStaticPaths({ paginate }: GetStaticPathsOptions) {
|
||||
const { page } = Astro.props;
|
||||
const { tag } = Astro.params;
|
||||
|
||||
const tagInfo = tagMap.get(tag!);
|
||||
const tagInfo = getTaxonomyMap("tag").get(tag!);
|
||||
const displayName = tagInfo?.name ?? tag!;
|
||||
const description = tagInfo?.description;
|
||||
|
||||
|
||||
@@ -51,14 +51,3 @@ export function getTaxonomyItem(
|
||||
): TaxonomyItem | undefined {
|
||||
return getTaxonomyMap(type).get(slug);
|
||||
}
|
||||
|
||||
// Convenience aliases kept for backward compatibility
|
||||
export const categoryMap = getTaxonomyMap("category");
|
||||
export const destinationMap = getTaxonomyMap("destination");
|
||||
export const tagMap = getTaxonomyMap("tag");
|
||||
|
||||
export const getCategoryPath = (slug: string) =>
|
||||
getTaxonomyPath("category", slug);
|
||||
export const getDestinationPath = (slug: string) =>
|
||||
getTaxonomyPath("destination", slug);
|
||||
export const getTagPath = (slug: string) => getTaxonomyPath("tag", slug);
|
||||
|
||||
Reference in New Issue
Block a user