diff --git a/components/right-sidebar.tsx b/components/right-sidebar.tsx index 5239efb..a2648af 100644 --- a/components/right-sidebar.tsx +++ b/components/right-sidebar.tsx @@ -1,9 +1,10 @@ import Link from 'next/link'; import { siteConfig } from '@/lib/config'; -import { getAllPostsSorted } from '@/lib/posts'; +import { getAllPostsSorted, getAllTagsWithCount } from '@/lib/posts'; export function RightSidebar() { const latest = getAllPostsSorted().slice(0, 5); + const tags = getAllTagsWithCount().slice(0, 30); return ( ); } - diff --git a/lib/posts.ts b/lib/posts.ts index 58de38a..1c76a05 100644 --- a/lib/posts.ts +++ b/lib/posts.ts @@ -26,3 +26,20 @@ export function getPageBySlug(slug: string): Page | undefined { ); } +export function getAllTagsWithCount(): { tag: string; count: number }[] { + const map = new Map(); + + for (const post of allPosts) { + if (!post.tags) continue; + for (const tag of post.tags) { + map.set(tag, (map.get(tag) ?? 0) + 1); + } + } + + return Array.from(map.entries()) + .map(([tag, count]) => ({ tag, count })) + .sort((a, b) => { + if (b.count === a.count) return a.tag.localeCompare(b.tag); + return b.count - a.count; + }); +}