From 0ab29677b7d8b37929fa1517b6dcaa502fddf9fe Mon Sep 17 00:00:00 2001 From: miyaji255 <84168445+miyaji255@users.noreply.github.com> Date: Mon, 16 Dec 2024 10:22:43 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=83=A1=E3=83=83?= =?UTF-8?q?=E3=82=BB=E3=83=BC=E3=82=B8=E3=82=92=E8=A1=A8=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/content/_blog-statistics.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/content/_blog-statistics.ts b/src/content/_blog-statistics.ts index abd78f8..40767b9 100644 --- a/src/content/_blog-statistics.ts +++ b/src/content/_blog-statistics.ts @@ -57,9 +57,11 @@ const statistics = lazy(async () => { for (const { id, time } of categoryStatistics[category]) (result[id] ??= { score: 0, time }).score += 2 - for (const { id: slug, time } of tags.flatMap( - ({ id }) => tagStatistics[id]!, - )) + for (const { id: slug, time } of tags.flatMap(({ id }) => { + const tag = tagStatistics[id] + if (!tag) throw new Error(`Tag ${id} not found`) + return tag + })) (result[slug] ??= { score: 0, time }).score += 1 delete result[id] @@ -85,12 +87,18 @@ const statistics = lazy(async () => { export async function getBlogStatistics(slug: BlogId) { const { blogStatistics } = await statistics() - return blogStatistics[slug]! + const blog = blogStatistics[slug] + if (!blog) throw new Error(`Blog ${slug} not found`) + return blog } export async function getTagStatistics(): Promise export async function getTagStatistics(id: TagId): Promise export async function getTagStatistics(id?: TagId) { const { tagStatistics } = await statistics() - return id === undefined ? tagStatistics : tagStatistics[id]! + if (id === undefined) return tagStatistics + + const tag = tagStatistics[id] + if (!tag) throw new Error(`Tag ${id} not found`) + return tag }