Commit 0f72910

HPCesia <me@hpcesia.com>
2025-02-09 11:56:23
refactor: category and tag page URL generation
1 parent 8e141e9
Changed files (6)
src/components/misc/CategoryBar.astro
@@ -1,6 +1,7 @@
 ---
 import I18nKey from '@i18n/I18nKey';
 import { i18n } from '@i18n/translation';
+import { getCategoryUrl } from '@utils/content-utils';
 
 interface Props {
   categories: string[];
@@ -21,7 +22,7 @@ const { categories, currentCategory } = Astro.props;
     {
       categories.map((category) => (
         <a
-          href={`/archives/categories/${category}/1/`}
+          href={getCategoryUrl(category)}
           class:list={[
             'btn btn-ghost h-8 min-h-8 px-3 py-0',
             currentCategory === category ? 'btn-active' : '',
src/components/misc/PostInfo.astro
@@ -3,6 +3,7 @@ import { articleConfig } from '@/config';
 import MetaIcon from '@components/widgets/MetaIcon.astro';
 import I18nKey from '@i18n/I18nKey';
 import { i18n } from '@i18n/translation';
+import { getCategoryUrl, getTagUrl } from '@utils/content-utils';
 
 interface Props {
   title: string;
@@ -45,14 +46,14 @@ const metas: ({ icon: string; text: string; link?: string } | undefined)[] = [
     ? {
         icon: 'material-symbols:category-outline-rounded',
         text: category,
-        link: `/archives/categories/${category.replaceAll(/[\\/]/g, '-')}/1/`,
+        link: getCategoryUrl(category),
       }
     : undefined,
   ...tags.map((tag) => {
     return {
       icon: 'material-symbols:tag-rounded',
       text: tag,
-      link: `/archives/tags/${tag.replaceAll(/[\\/]/g, '-')}/1/`,
+      link: getTagUrl(tag),
     };
   }),
 ];
src/components/widgets/PostCard.astro
@@ -1,4 +1,5 @@
 ---
+import { getCategoryUrl, getTagUrl } from '@utils/content-utils';
 import MetaIcon from './MetaIcon.astro';
 import PostCardCover from './PostCardCover.astro';
 import ReadMoreButton from './ReadMoreButton.astro';
@@ -33,14 +34,14 @@ const metas: ({ icon: string; text: string; link?: string } | undefined)[] = [
     ? {
         icon: 'material-symbols:category-outline-rounded',
         text: category,
-        link: `/archives/categories/${category.replaceAll(/[\\/]/g, '-')}/1/`,
+        link: getCategoryUrl(category),
       }
     : undefined,
   ...tags.map((tag) => {
     return {
       icon: 'material-symbols:tag-rounded',
       text: tag,
-      link: `/archives/tags/${tag.replaceAll(/[\\/]/g, '-')}/1/`,
+      link: getTagUrl(tag),
     };
   }),
 ];
src/pages/archives/categories/index.astro
@@ -1,6 +1,6 @@
 ---
 import type { BlogPostData } from '@/types/data';
-import { getCategories, getSortedPosts } from '@/utils/content-utils';
+import { getCategories, getCategoryUrl, getSortedPosts } from '@/utils/content-utils';
 import Timeline from '@components/Timeline.astro';
 import Button from '@components/widgets/Button.astro';
 import PostCard from '@components/widgets/PostCard.astro';
@@ -31,11 +31,7 @@ if (uncategorizedPosts.length > 0)
           (category: string) => (
             <div class="mb-6 flex items-center justify-between">
               <h2 class="text-2xl font-bold">{category}</h2>
-              <Button
-                href={`/archives/categories/${category === i18n(I18nKey.uncategorized) ? I18nKey.uncategorized : category.replaceAll(/[\\/]/g, '-')}/1/`}
-                title={category}
-                class="pl-3!"
-              >
+              <Button href={getCategoryUrl(category)} title={category} class="pl-3">
                 {i18n(I18nKey.more)}
                 <Icon name="material-symbols:chevron-right-rounded" class="text-2xl" />
               </Button>
src/pages/archives/tags/index.astro
@@ -1,6 +1,6 @@
 ---
 import type { BlogPostData } from '@/types/data';
-import { getSortedPosts, getTags } from '@/utils/content-utils';
+import { getSortedPosts, getTags, getTagUrl } from '@/utils/content-utils';
 import Timeline from '@components/Timeline.astro';
 import Button from '@components/widgets/Button.astro';
 import PostCard from '@components/widgets/PostCard.astro';
@@ -32,11 +32,7 @@ if (untaggedPosts.length > 0) tagPosts.set(i18n(I18nKey.untagged) as string, unt
           (tag: string) => (
             <div class="mb-6 flex items-center justify-between">
               <h2 class="text-2xl font-bold">{tag}</h2>
-              <Button
-                href={`/archives/tags/${tag === i18n(I18nKey.untagged) ? I18nKey.untagged : tag.replaceAll(/[\\/]/g, '-')}/1/`}
-                title={tag}
-                class="pl-3!"
-              >
+              <Button href={getTagUrl(tag)} title={tag} class="pl-3">
                 {i18n(I18nKey.more)}
                 <Icon name="material-symbols:chevron-right-rounded" class="text-2xl" />
               </Button>
src/utils/content-utils.ts
@@ -64,3 +64,15 @@ export async function getTimeArchives() {
     }))
     .sort((a, b) => b.year - a.year);
 }
+
+export function getCategoryUrl(category: string | undefined) {
+  return category
+    ? `/archives//categories/${category.replaceAll(/[\\/]/g, '-')}/1/`
+    : `/archives//categories/${I18nKey.uncategorized}/1/`;
+}
+
+export function getTagUrl(tag: string) {
+  return tag === i18n(I18nKey.untagged)
+    ? `/archives/tags/${I18nKey.untagged}/1`
+    : `/archives/tags/${tag.replaceAll(/[\\/]/g, '-')}/1`;
+}