fix: Trim whitespace from category and tag names in URL generation (#437)

This commit is contained in:
Katsuyuki Karasawa
2025-05-05 09:57:34 +09:00
committed by GitHub
parent 7f0c109b17
commit 2b3d7cf304
9 changed files with 143 additions and 24 deletions
+20 -2
View File
@@ -1,5 +1,6 @@
import i18nKey from "@i18n/i18nKey";
import { i18n } from "@i18n/translation";
import { encodePathSegment } from "./encoding-utils";
export function pathsEqual(path1: string, path2: string) {
const normalizedPath1 = path1.replace(/^\/|\/$/g, "").toLowerCase();
@@ -16,10 +17,27 @@ export function getPostUrlBySlug(slug: string): string {
return url(`/posts/${slug}/`);
}
export function getTagUrl(tag: string): string {
if (!tag) return url("/archive/tag/");
// use common encoding function
const encodedTag = encodePathSegment(tag);
const tagUrl = `/archive/tag/${encodedTag}/`;
console.log(`Generating URL for tag "${tag.trim()}" => "${tagUrl}"`);
return url(tagUrl);
}
export function getCategoryUrl(category: string): string {
if (category === i18n(i18nKey.uncategorized))
console.log(`category: ${category}`);
if (!category) return url("/archive/category/");
const trimmedCategory = category.trim();
if (trimmedCategory === i18n(i18nKey.uncategorized))
return url("/archive/category/uncategorized/");
return url(`/archive/category/${encodeURIComponent(category)}/`);
return url(
`/archive/category/${encodeURIComponent(trimmedCategory).replace(/%20/g, "+")}/`,
);
}
export function getDir(path: string): string {