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
+24 -3
View File
@@ -7,16 +7,37 @@ import { getCategoryList } from "@utils/content-utils";
export async function getStaticPaths() {
const categories = await getCategoryList();
return categories.map((category) => {
const standardPaths = categories.map((category) => {
return {
params: {
category: encodeURIComponent(category.name),
category: encodeURIComponent(category.name.trim()),
},
props: {
decodedCategory: category.name.trim(),
},
};
});
const nonEncodedCJKPaths = categories
.filter((category) =>
/[\u3000-\u9fff\uac00-\ud7af\u4e00-\u9faf]/.test(category.name),
)
.map((category) => ({
params: {
category: category.name.trim(), // Do not encode CJK characters
},
props: {
decodedCategory: category.name.trim(),
},
}));
return [...standardPaths, ...nonEncodedCJKPaths];
}
const category = decodeURIComponent(Astro.params.category as string);
const { decodedCategory } = Astro.props;
const category =
decodedCategory || decodeURIComponent(Astro.params.category as string);
---
<MainGridLayout title={i18n(I18nKey.archive)} description={i18n(I18nKey.archive)}>
+39 -7
View File
@@ -4,29 +4,61 @@ import I18nKey from "@i18n/i18nKey";
import { i18n } from "@i18n/translation";
import MainGridLayout from "@layouts/MainGridLayout.astro";
import { getSortedPosts } from "@utils/content-utils";
import { decodePathSegment, encodePathSegment } from "@utils/encoding-utils";
export async function getStaticPaths() {
const posts = await getSortedPosts();
// タグを集めるための Set の型を指定
const allTags = posts.reduce<Set<string>>((acc, post) => {
// biome-ignore lint/complexity/noForEach: <explanation>
post.data.tags.forEach((tag) => acc.add(tag));
if (Array.isArray(post.data.tags)) {
// biome-ignore lint/complexity/noForEach: <explanation>
post.data.tags.forEach((tag) => {
if (typeof tag === "string") {
acc.add(tag.trim());
} else {
acc.add(String(tag).trim());
}
});
} else if (post.data.tags && typeof post.data.tags === "string") {
// biome-ignore lint/complexity/noForEach: <explanation>
(post.data.tags as string)
.split(",")
.forEach((tag) => acc.add(tag.trim()));
}
return acc;
}, new Set());
const allTagsArray = Array.from(allTags);
return allTagsArray.map((tag) => ({
// judge if the string is CJK
const isCJK = (str: string) =>
/[\u3000-\u9fff\uac00-\ud7af\u4e00-\u9faf]/.test(str);
const standardPaths = allTagsArray.map((tag) => ({
params: {
tag: encodeURIComponent(tag),
tag: encodePathSegment(tag),
},
props: {
decodedTag: tag,
},
}));
const nonEncodedCJKPaths = allTagsArray.filter(isCJK).map((tag) => ({
params: {
tag: tag, // keep CJK characters unencoded
},
props: {
decodedTag: tag,
},
}));
return [...standardPaths, ...nonEncodedCJKPaths];
}
const tag = decodeURIComponent(Astro.params.tag as string);
const { decodedTag } = Astro.props;
const tag = decodedTag || decodePathSegment(Astro.params.tag as string);
---
<MainGridLayout title={i18n(I18nKey.archive)} description={i18n(I18nKey.archive)}>
<ArchivePanel tags={[tag]}></ArchivePanel>
</MainGridLayout>
</MainGridLayout>