From 7a857f8198287f4d3d2cd49947c2b3a44c628f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hal=C3=AD=20V=2E?= Date: Thu, 2 Jan 2025 11:17:19 -0600 Subject: [PATCH 1/3] fix: post excerpt --- src/components/PostExcerpt.vue | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/src/components/PostExcerpt.vue b/src/components/PostExcerpt.vue index 60a49274..7b2a0542 100644 --- a/src/components/PostExcerpt.vue +++ b/src/components/PostExcerpt.vue @@ -1,31 +1,13 @@ From 75320ebcd5fc990614822abb305a849c50a563e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hal=C3=AD=20V=2E?= Date: Thu, 2 Jan 2025 11:24:57 -0600 Subject: [PATCH 3/3] chore: rename function --- src/components/PostExcerpt.vue | 4 ++-- src/components/PostTranslationLink.astro | 4 ++-- src/pages/blog/[...slug].astro | 15 ++------------- src/pages/en/blog/[...slug].astro | 4 ++-- src/ts/functions.ts | 2 +- 5 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/components/PostExcerpt.vue b/src/components/PostExcerpt.vue index 6bc0b820..ee6f0f5a 100644 --- a/src/components/PostExcerpt.vue +++ b/src/components/PostExcerpt.vue @@ -3,7 +3,7 @@ import { getRelativeLocaleUrl } from "astro:i18n" import type { CollectionEntry } from "astro:content" import { getLangFromUrl, useTranslations } from "@/i18n/utils" import { computed } from "vue" -import { getPostUrl, postReadableDate } from "@/ts/functions" +import { getPostUrlParts, postReadableDate } from "@/ts/functions" const props = defineProps<{ url: URL @@ -21,7 +21,7 @@ const hasExcerpt = props.post.rendered?.html.includes(KEEP_READING_FLAG) ?? false const href = computed(() => { - const [year, month, slug] = getPostUrl(props.post) + const [year, month, slug] = getPostUrlParts(props.post) return getRelativeLocaleUrl(lang, `/blog/${year}/${month}/${slug}`) }) diff --git a/src/components/PostTranslationLink.astro b/src/components/PostTranslationLink.astro index dea7d2a4..536e832d 100644 --- a/src/components/PostTranslationLink.astro +++ b/src/components/PostTranslationLink.astro @@ -2,7 +2,7 @@ import { getEntry } from "astro:content" import type { CollectionEntry } from "astro:content" import { getAbsoluteLocaleUrl } from "astro:i18n" -import { getPostUrl } from "@/ts/functions" +import { getPostUrlParts } from "@/ts/functions" import { displayLanguages } from "@/i18n/ui" type Translation = NonNullable< @@ -26,7 +26,7 @@ const translationLabel = displayLanguages[translation.short] class="capitalize" href={getAbsoluteLocaleUrl( translation.short, - "/blog/" + getPostUrl(post).join("/"), + "/blog/" + getPostUrlParts(post).join("/"), )} > {translationLabel} diff --git a/src/pages/blog/[...slug].astro b/src/pages/blog/[...slug].astro index 7c5e1620..882cf5c0 100644 --- a/src/pages/blog/[...slug].astro +++ b/src/pages/blog/[...slug].astro @@ -4,7 +4,7 @@ import { getCollection, getEntry, render } from "astro:content" import { Image } from "astro:assets" import HtmlLayout from "@/layouts/html.astro" import { getLangFromUrl, useTranslations } from "@/i18n/utils" -import { postReadableDate } from "@/ts/functions" +import { getPostUrlParts, postReadableDate } from "@/ts/functions" import PostTag from "@/components/PostTag.vue" import PostReactions from "@/components/PostReactions.vue" import PostTranslationLink from "@/components/PostTranslationLink.astro" @@ -12,19 +12,8 @@ import PostTranslationLink from "@/components/PostTranslationLink.astro" export const getStaticPaths = (async () => { const posts = await getCollection("posts", (post) => !post.id.includes("/")) - function getPostUrl(post: { - id: string - data: { date: Date } - }): [string, string, string] { - const year = post.data.date.getFullYear().toString() - const month = (post.data.date.getMonth() + 1).toString().padStart(2, "0") - const slug = post.id.replace(/\d{4}-\d{2}-\d{2}-/, "") - - return [year, month, slug] - } - return posts.map((post) => ({ - params: { slug: getPostUrl(post).join("/") }, + params: { slug: getPostUrlParts(post).join("/") }, props: { post }, })) }) satisfies GetStaticPaths diff --git a/src/pages/en/blog/[...slug].astro b/src/pages/en/blog/[...slug].astro index d64dc860..592eef14 100644 --- a/src/pages/en/blog/[...slug].astro +++ b/src/pages/en/blog/[...slug].astro @@ -2,7 +2,7 @@ import type { GetStaticPaths } from "astro" import { getCollection } from "astro:content" import BlogPost from "@/pages/blog/[...slug].astro" -import { getPostUrl } from "@/ts/functions" +import { getPostUrlParts } from "@/ts/functions" export const getStaticPaths = (async () => { const posts = await getCollection( @@ -11,7 +11,7 @@ export const getStaticPaths = (async () => { ) return posts.map((post) => ({ - params: { slug: getPostUrl(post).join("/") }, + params: { slug: getPostUrlParts(post).join("/") }, props: { post }, })) }) satisfies GetStaticPaths diff --git a/src/ts/functions.ts b/src/ts/functions.ts index ce8a0a5f..71a7de74 100644 --- a/src/ts/functions.ts +++ b/src/ts/functions.ts @@ -36,7 +36,7 @@ export function postReadableDate(lang: typeof languages[number], date: Date) { return `${month} ${day}, ${year}` } -export function getPostUrl(post: { +export function getPostUrlParts(post: { id: string data: { date: Date } }): [string, string, string] {