From 3f5ca8d9437bac7dea10f113ac5cf9163dbead48 Mon Sep 17 00:00:00 2001 From: L4Ph <4ranci0ne@gmail.com> Date: Mon, 26 May 2025 05:51:34 +0900 Subject: [PATCH] refactor: improve tag handling and encoding in Tags and [tag] components https://github.com/saicaca/fuwari/issues/462 --- src/components/widget/Tags.astro | 16 ++++++++----- src/pages/archive/tag/[tag].astro | 40 ++++++++++--------------------- src/utils/encoding-utils.ts | 11 +-------- 3 files changed, 24 insertions(+), 43 deletions(-) diff --git a/src/components/widget/Tags.astro b/src/components/widget/Tags.astro index 3ead19db..a52267fd 100644 --- a/src/components/widget/Tags.astro +++ b/src/components/widget/Tags.astro @@ -3,7 +3,8 @@ import I18nKey from "../../i18n/i18nKey"; import { i18n } from "../../i18n/translation"; import { getTagList } from "../../utils/content-utils"; -import { getTagUrl } from "../../utils/url-utils"; +import { encodePathSegment } from "../../utils/encoding-utils"; +import { url as url_util } from "../../utils/url-utils"; import ButtonTag from "../control/ButtonTag.astro"; import WidgetLayout from "./WidgetLayout.astro"; @@ -22,10 +23,13 @@ const style = Astro.props.style; ---
- {tags.map(t => ( - - {t.name.trim()} - - ))} + {tags.map(t => { + const encodedTag = encodePathSegment(t.name); + return ( + + {t.name} + + ); + })}
\ No newline at end of file diff --git a/src/pages/archive/tag/[tag].astro b/src/pages/archive/tag/[tag].astro index 34eedd13..8c9c9add 100644 --- a/src/pages/archive/tag/[tag].astro +++ b/src/pages/archive/tag/[tag].astro @@ -10,47 +10,33 @@ export async function getStaticPaths() { const posts = await getSortedPosts(); const allTags = posts.reduce>((acc, post) => { - if (Array.isArray(post.data.tags)) { - // biome-ignore lint/complexity/noForEach: - 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: - (post.data.tags as string) - .split(",") - .forEach((tag) => acc.add(tag.trim())); + for (const tag of post.data.tags) { + acc.add(tag); } return acc; }, new Set()); const allTagsArray = Array.from(allTags); - // 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: encodePathSegment(tag), }, props: { - decodedTag: tag, + decodedTag: tag.trim(), }, })); - const nonEncodedCJKPaths = allTagsArray.filter(isCJK).map((tag) => ({ - params: { - tag: tag, // keep CJK characters unencoded - }, - props: { - decodedTag: tag, - }, - })); + const nonEncodedCJKPaths = allTagsArray + .filter((tag) => /[\u3000-\u9fff\uac00-\ud7af\u4e00-\u9faf]/.test(tag)) + .map((tag) => ({ + params: { + tag: tag.trim(), // Do not encode CJK characters + }, + props: { + decodedTag: tag.trim(), + }, + })); return [...standardPaths, ...nonEncodedCJKPaths]; } diff --git a/src/utils/encoding-utils.ts b/src/utils/encoding-utils.ts index 1e3a1599..29f4fa7b 100644 --- a/src/utils/encoding-utils.ts +++ b/src/utils/encoding-utils.ts @@ -9,8 +9,6 @@ * @returns The encoded string */ export function encodePathSegment(value: string): string { - if (!value) return ""; - return encodeURIComponent(value.trim()); } @@ -21,12 +19,5 @@ export function encodePathSegment(value: string): string { * @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; - } + return decodeURIComponent(value); }