From 7b9c1c9eea67c0a5a9fd4a25718b42b69c896c89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=B1=E7=94=B0=E3=83=8F=E3=83=A4=E3=82=AA?= Date: Sat, 24 Feb 2024 07:14:57 +0900 Subject: [PATCH] Update: Use new category info --- src/app/(hayao)/blog/category/[cat]/page.tsx | 2 +- src/app/(hayao)/blog/category/page.tsx | 7 +++---- src/app/(hayao)/blog/layout.tsx | 3 +-- src/lib/blog/categories.ts | 18 +++++++++++++++--- src/lib/blog/config.ts | 6 ++---- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/app/(hayao)/blog/category/[cat]/page.tsx b/src/app/(hayao)/blog/category/[cat]/page.tsx index 6c9f429..85f11e5 100644 --- a/src/app/(hayao)/blog/category/[cat]/page.tsx +++ b/src/app/(hayao)/blog/category/[cat]/page.tsx @@ -11,7 +11,7 @@ import { PostData } from "@/lib/markdown/post"; export default async function Categories({ params }: { params: { cat: string } }) { const postpost = getPostList(params.cat); - //console.log(params.cat); + //console.log(decodeURI(params.cat)); return ( diff --git a/src/app/(hayao)/blog/category/page.tsx b/src/app/(hayao)/blog/category/page.tsx index 5349513..3ec3d16 100644 --- a/src/app/(hayao)/blog/category/page.tsx +++ b/src/app/(hayao)/blog/category/page.tsx @@ -1,8 +1,7 @@ import Link from "next/link"; import CommonSpacer from "@/components/layouts/CommonSpacer"; -import { getAllCategories } from "@/lib/blog/categories"; -import { CATEGORY_DESC } from "@/lib/blog/config"; +import { findCategoryInfo, getAllCategories } from "@/lib/blog/categories"; export default function CategoryTop() { const categories = getAllCategories(); @@ -20,9 +19,9 @@ export default function CategoryTop() { } const Category = ({ category }: { category: string }) => { - const descList: { [key: string]: string } = CATEGORY_DESC; + const catInfo = findCategoryInfo(category); - const desc = descList[category] ? descList[category] : ""; + const desc = catInfo ? catInfo.desc : ""; const link = `/blog/category/${category}`; return ( diff --git a/src/app/(hayao)/blog/layout.tsx b/src/app/(hayao)/blog/layout.tsx index 52690f3..943f4a8 100644 --- a/src/app/(hayao)/blog/layout.tsx +++ b/src/app/(hayao)/blog/layout.tsx @@ -3,10 +3,9 @@ import Link from "next/link"; import { Heading } from "@/components/elements/Heading"; import CommonSpacer from "@/components/layouts/CommonSpacer"; -import { fetchedBlogPostList } from "@/lib/blog/post"; +import { fetchedBlogPostList as postlist } from "@/lib/blog/post"; export default function BlogLayout({ children }: { children: React.ReactNode }) { - const postlist = fetchedBlogPostList; const categories = postlist.getAllCategories(); const tags = postlist.getAllTags(); const posts = postlist.getPosts().slice(undefined, 10); diff --git a/src/lib/blog/categories.ts b/src/lib/blog/categories.ts index 85fc03c..826b066 100644 --- a/src/lib/blog/categories.ts +++ b/src/lib/blog/categories.ts @@ -1,11 +1,23 @@ import { CATEGORY_INFO } from "./config"; import { fetchedBlogPostList } from "./post"; +export type Category = { + jp: string; + url: string; + desc: string; +}; + export const getAllCategories = () => fetchedBlogPostList.getAllCategories(); -export const findCategoryInfo = (category: string) => { - const catingo: { jp: string; url: string; desc: string }[] = getCategoryInfo(); - return catingo.filter((c) => c.url === category || c.jp === category)[0]; +export const findCategoryInfo = (category: string): Category | null => { + const catingo = getCategoryInfo(); + const matched = catingo.filter((c) => c.url === category || c.jp === category); + + if (matched.length === 0) { + return null; + } + + return matched[0]; }; export const getCategoryInfo = () => { diff --git a/src/lib/blog/config.ts b/src/lib/blog/config.ts index a4ee4d4..70271d8 100644 --- a/src/lib/blog/config.ts +++ b/src/lib/blog/config.ts @@ -1,19 +1,17 @@ import path from "path"; import { URLFormat } from "../markdown/url"; +import { Category } from "./categories"; export const POSTLIST_ONEPAGE = 9; export const MDFILE_DIR = path.join(process.cwd(), "posts"); export const SUMMARY_LENGTH = 200; -export const CATEGORY_DESC = { - プライベート: "プライベートなこと", -}; export const BLOG_URL_FORMAT: URLFormat = { cutHead: path.join(process.cwd()).replaceAll(path.sep, "/").split("/").length + 1, }; -export const CATEGORY_INFO: { jp: string; url: string; desc: string }[] = [ +export const CATEGORY_INFO: Category[] = [ { jp: "プライベート", url: "private",