From e4c370ede5f8b872a066a1eed1b6a71da960b68f Mon Sep 17 00:00:00 2001 From: PrinOrange Date: Sat, 6 Jan 2024 16:18:34 +0800 Subject: [PATCH] Fixed the issue of not being able to recognize differences in upper and lower case, resulting in duplication of labels --- lib/post-process.ts | 5 +++-- lib/utils.ts | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/post-process.ts b/lib/post-process.ts index 19390f7..978a0ee 100644 --- a/lib/post-process.ts +++ b/lib/post-process.ts @@ -4,7 +4,7 @@ import { TPostListItem, TTagSubPostSet } from "@/types/post-list"; import fs from "fs"; import { serialize } from "next-mdx-remote/serialize"; import path from "path"; -import { nullifyEmptyString } from "./utils"; +import { capitalizeFirstLetter, nullifyEmptyString } from "./utils"; async function getFrontmatters(filepath: string): Promise { const source = fs.readFileSync(filepath, "utf-8"); @@ -45,6 +45,7 @@ const sortOutPostLists = async (): Promise<{ for (let i = 0; i < postFilePaths.length; i++) { const frontmatter = await getFrontmatters(postFilePaths[i]); const postId = path.parse(postFilePaths[i]).name; + const capitalizedTags = frontmatter.tags?.map((tagname) => capitalizeFirstLetter(tagname)); const postListItem: TPostListItem = { id: postId, @@ -52,7 +53,7 @@ const sortOutPostLists = async (): Promise<{ title: frontmatter.title, subtitle: nullifyEmptyString(frontmatter.subtitle), coverURL: nullifyEmptyString(frontmatter.coverURL), - tags: frontmatter.tags ?? [], + tags: capitalizedTags ?? [], summary: nullifyEmptyString(frontmatter.summary), time: frontmatter.time, pin: frontmatter.pin ?? false, diff --git a/lib/utils.ts b/lib/utils.ts index f50de2f..8d09b92 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -56,3 +56,17 @@ export function isEmptyString(value: string | null | undefined): boolean { } return false; } + +/** + * Capitalizes the first letter of each word in a string. + * + * @param str - The input string. + * @returns The string with the first letter of each word capitalized. + */ +export function capitalizeFirstLetter(str: string) { + return str + .toLowerCase() + .split(" ") + .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) + .join(" "); +}