Fixed the issue of not being able to recognize differences in upper and lower case, resulting in duplication of labels
This commit is contained in:
@@ -4,7 +4,7 @@ import { TPostListItem, TTagSubPostSet } from "@/types/post-list";
|
|||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import { serialize } from "next-mdx-remote/serialize";
|
import { serialize } from "next-mdx-remote/serialize";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { nullifyEmptyString } from "./utils";
|
import { capitalizeFirstLetter, nullifyEmptyString } from "./utils";
|
||||||
|
|
||||||
async function getFrontmatters(filepath: string): Promise<TFrontmatter> {
|
async function getFrontmatters(filepath: string): Promise<TFrontmatter> {
|
||||||
const source = fs.readFileSync(filepath, "utf-8");
|
const source = fs.readFileSync(filepath, "utf-8");
|
||||||
@@ -45,6 +45,7 @@ const sortOutPostLists = async (): Promise<{
|
|||||||
for (let i = 0; i < postFilePaths.length; i++) {
|
for (let i = 0; i < postFilePaths.length; i++) {
|
||||||
const frontmatter = await getFrontmatters(postFilePaths[i]);
|
const frontmatter = await getFrontmatters(postFilePaths[i]);
|
||||||
const postId = path.parse(postFilePaths[i]).name;
|
const postId = path.parse(postFilePaths[i]).name;
|
||||||
|
const capitalizedTags = frontmatter.tags?.map((tagname) => capitalizeFirstLetter(tagname));
|
||||||
|
|
||||||
const postListItem: TPostListItem = {
|
const postListItem: TPostListItem = {
|
||||||
id: postId,
|
id: postId,
|
||||||
@@ -52,7 +53,7 @@ const sortOutPostLists = async (): Promise<{
|
|||||||
title: frontmatter.title,
|
title: frontmatter.title,
|
||||||
subtitle: nullifyEmptyString(frontmatter.subtitle),
|
subtitle: nullifyEmptyString(frontmatter.subtitle),
|
||||||
coverURL: nullifyEmptyString(frontmatter.coverURL),
|
coverURL: nullifyEmptyString(frontmatter.coverURL),
|
||||||
tags: frontmatter.tags ?? [],
|
tags: capitalizedTags ?? [],
|
||||||
summary: nullifyEmptyString(frontmatter.summary),
|
summary: nullifyEmptyString(frontmatter.summary),
|
||||||
time: frontmatter.time,
|
time: frontmatter.time,
|
||||||
pin: frontmatter.pin ?? false,
|
pin: frontmatter.pin ?? false,
|
||||||
|
|||||||
14
lib/utils.ts
14
lib/utils.ts
@@ -56,3 +56,17 @@ export function isEmptyString(value: string | null | undefined): boolean {
|
|||||||
}
|
}
|
||||||
return false;
|
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(" ");
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user