fix: Trim whitespace from category and tag names in URL generation (#437)
Some checks failed
Code quality / quality (push) Failing after 4s
Build and Check / Astro Check for Node.js 22 (push) Failing after 4s
Build and Check / Astro Check for Node.js 23 (push) Failing after 4s
Build and Check / Astro Build for Node.js 22 (push) Failing after 4s
Build and Check / Astro Build for Node.js 23 (push) Failing after 3s

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

View File

@@ -67,9 +67,13 @@ export async function getCategoryList(): Promise<Category[]> {
count[ucKey] = count[ucKey] ? count[ucKey] + 1 : 1;
return;
}
count[post.data.category] = count[post.data.category]
? count[post.data.category] + 1
: 1;
const categoryName =
typeof post.data.category === "string"
? post.data.category.trim()
: String(post.data.category).trim();
count[categoryName] = count[categoryName] ? count[categoryName] + 1 : 1;
});
const lst = Object.keys(count).sort((a, b) => {

View File

@@ -0,0 +1,32 @@
/**
* Utility functions for ensuring consistent URL encoding
*/
/**
* Ensure consistent URL encoding across all tags and categories
*
* @param value The string to encode
* @returns The encoded string
*/
export function encodePathSegment(value: string): string {
if (!value) return "";
return encodeURIComponent(value.trim());
}
/**
* Decode from the URL path
*
* @param value String to decode
* @returns Decoded string
*/
export function decodePathSegment(value: string): string {
if (!value) return "";
try {
return decodeURIComponent(value);
} catch (e) {
console.error(`Failed to decode path segment: ${value}`, e);
return value;
}
}

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 {