From 61c21cc16954a878a8c82f04af8b225a03417bfd Mon Sep 17 00:00:00 2001 From: Suradet Pratomsak Date: Thu, 1 Jan 2026 06:01:40 +0700 Subject: [PATCH 1/5] feat: add feature related post --- src/components/blog/RelatedPosts.astro | 68 ++++++++++++++++++++++++++ src/layouts/BlogPostLayout.astro | 12 ++++- src/utils/post-utils.ts | 65 ++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 src/components/blog/RelatedPosts.astro create mode 100644 src/utils/post-utils.ts diff --git a/src/components/blog/RelatedPosts.astro b/src/components/blog/RelatedPosts.astro new file mode 100644 index 0000000..018c040 --- /dev/null +++ b/src/components/blog/RelatedPosts.astro @@ -0,0 +1,68 @@ +--- +import type { CollectionEntry } from 'astro:content'; + +import BlogPostCard from './BlogPostCard.astro'; + +type Props = { + relatedPosts: CollectionEntry<'blog'>[]; +}; + +const { relatedPosts } = Astro.props; +--- + +{ + relatedPosts.length > 0 && ( + + ) +} + + diff --git a/src/layouts/BlogPostLayout.astro b/src/layouts/BlogPostLayout.astro index 94fdbde..0390a31 100644 --- a/src/layouts/BlogPostLayout.astro +++ b/src/layouts/BlogPostLayout.astro @@ -2,8 +2,12 @@ import type { MarkdownHeading } from 'astro'; import type { CollectionEntry } from 'astro:content'; +import { getCollection } from 'astro:content'; + +import RelatedPosts from '@/components/blog/RelatedPosts.astro'; import Toc from '@/components/blog/Toc.astro'; import ViewCounter from '@/components/blog/ViewCounter.astro'; +import { getRelatedPosts } from '@/utils/post-utils'; import { slugify } from '@/utils/slugify'; import BaseLayout from './BaseLayout.astro'; @@ -23,8 +27,12 @@ const formattedDate = new Date(pubDate).toLocaleDateString('en-US', { day: 'numeric', }); -// ใช้ slug ที่ส่งมา หรือสร้างจาก title เพื่อใช้เป็น key ในฐานข้อมูล +// Use the provided slug or generate from title as a database key const postSlug = slug || slugify(title); + +// Fetch all blog posts and compute related posts +const allPosts = await getCollection('blog'); +const relatedPosts = getRelatedPosts(allPosts, postSlug, tags, category, 3); --- @@ -72,6 +80,8 @@ const postSlug = slug || slugify(title); } + +