|
| 1 | +--- |
| 2 | +import { getCollection } from "astro:content"; |
| 3 | +import { formatDate, postUrl, slugify, withBase } from "../../lib/utils"; |
| 4 | +
|
| 5 | +const allPosts = await getCollection("posts"); |
| 6 | +const recentPosts = allPosts |
| 7 | + .filter((p) => p.data.published) |
| 8 | + .sort((a, b) => b.data.publishDate.getTime() - a.data.publishDate.getTime()) |
| 9 | + .slice(0, 3); |
| 10 | +
|
| 11 | +// Collect years from all posts for the mini sidebar |
| 12 | +const yearSet = new Set<number>(); |
| 13 | +for (const post of allPosts.filter((p) => p.data.published)) { |
| 14 | + yearSet.add(post.data.publishDate.getFullYear()); |
| 15 | +} |
| 16 | +const years = [...yearSet].sort((a, b) => b - a).slice(0, 8); |
| 17 | +
|
| 18 | +// Top tags for the mini sidebar |
| 19 | +const tagCounts = new Map<string, number>(); |
| 20 | +for (const post of allPosts.filter((p) => p.data.published)) { |
| 21 | + for (const tag of post.data.tags) { |
| 22 | + tagCounts.set(tag, (tagCounts.get(tag) || 0) + 1); |
| 23 | + } |
| 24 | +} |
| 25 | +const topTags = [...tagCounts.entries()].sort((a, b) => b[1] - a[1]).slice(0, 10); |
| 26 | +--- |
| 27 | + |
| 28 | +<div class="not-prose my-8 overflow-hidden rounded-xl border border-zinc-200 dark:border-zinc-800"> |
| 29 | + <div class="flex items-center justify-between border-b border-zinc-200 bg-zinc-50 px-5 py-3 dark:border-zinc-800 dark:bg-zinc-900/50"> |
| 30 | + <div class="flex items-center gap-2"> |
| 31 | + <svg class="h-4 w-4 text-zinc-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"> |
| 32 | + <path d="M2 3h6a4 4 0 014 4v14a3 3 0 00-3-3H2z" stroke-linecap="round" stroke-linejoin="round" /> |
| 33 | + <path d="M22 3h-6a4 4 0 00-4 4v14a3 3 0 013-3h7z" stroke-linecap="round" stroke-linejoin="round" /> |
| 34 | + </svg> |
| 35 | + <span class="text-sm font-semibold text-zinc-700 dark:text-zinc-300" style="font-family: var(--font-display);">Blog</span> |
| 36 | + <span class="text-xs text-zinc-400 dark:text-zinc-500">{allPosts.filter((p) => p.data.published).length} posts with filters & pagination</span> |
| 37 | + </div> |
| 38 | + <a href={withBase("/blog")} class="text-xs font-medium text-[#306998] hover:underline dark:text-[#ffd43b]"> |
| 39 | + Browse all → |
| 40 | + </a> |
| 41 | + </div> |
| 42 | + |
| 43 | + <div class="flex"> |
| 44 | + <!-- Posts column --> |
| 45 | + <div class="min-w-0 flex-1 divide-y divide-zinc-100 dark:divide-zinc-800/60"> |
| 46 | + {recentPosts.map((post) => ( |
| 47 | + <a href={withBase(postUrl(post.id, post.data.publishDate.toISOString()))} class="group block px-5 py-3.5 transition-colors hover:bg-zinc-50 dark:hover:bg-zinc-800/30"> |
| 48 | + <h4 class="text-sm font-semibold leading-snug text-zinc-800 transition-colors group-hover:text-[#306998] dark:text-zinc-200 dark:group-hover:text-[#ffd43b]" style="font-family: var(--font-display);"> |
| 49 | + {post.data.title} |
| 50 | + </h4> |
| 51 | + <div class="mt-1 flex items-center gap-2 text-xs text-zinc-400 dark:text-zinc-500"> |
| 52 | + <span class="font-medium text-zinc-500 dark:text-zinc-400">{post.data.author}</span> |
| 53 | + <span class="text-zinc-300 dark:text-zinc-700">·</span> |
| 54 | + <time datetime={post.data.publishDate.toISOString()}>{formatDate(post.data.publishDate.toISOString())}</time> |
| 55 | + </div> |
| 56 | + {post.data.tags.length > 0 && ( |
| 57 | + <div class="mt-2 flex flex-wrap gap-1"> |
| 58 | + {post.data.tags.slice(0, 3).map((tag) => ( |
| 59 | + <span class="rounded-md bg-zinc-100 px-1.5 py-0.5 text-[10px] font-medium text-zinc-500 dark:bg-zinc-800 dark:text-zinc-400"> |
| 60 | + {tag} |
| 61 | + </span> |
| 62 | + ))} |
| 63 | + </div> |
| 64 | + )} |
| 65 | + </a> |
| 66 | + ))} |
| 67 | + </div> |
| 68 | + |
| 69 | + <!-- Mini sidebar --> |
| 70 | + <div class="hidden w-44 flex-shrink-0 border-l border-zinc-100 p-4 dark:border-zinc-800/60 sm:block"> |
| 71 | + <div class="mb-4"> |
| 72 | + <h5 class="mb-2 text-[10px] font-semibold uppercase tracking-widest text-zinc-400 dark:text-zinc-600">Years</h5> |
| 73 | + <div class="flex flex-wrap gap-1"> |
| 74 | + {years.map((y) => ( |
| 75 | + <a |
| 76 | + href={withBase(`/blog/year/${y}`)} |
| 77 | + class="rounded bg-zinc-100 px-1.5 py-0.5 text-[10px] font-medium text-zinc-500 transition-colors hover:bg-zinc-200 dark:bg-zinc-800 dark:text-zinc-400 dark:hover:bg-zinc-700" |
| 78 | + > |
| 79 | + {y} |
| 80 | + </a> |
| 81 | + ))} |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + <div> |
| 85 | + <h5 class="mb-2 text-[10px] font-semibold uppercase tracking-widest text-zinc-400 dark:text-zinc-600">Tags</h5> |
| 86 | + <div class="flex flex-wrap gap-1"> |
| 87 | + {topTags.map(([tag, count]) => ( |
| 88 | + <a |
| 89 | + href={withBase(`/tags/${tag}`)} |
| 90 | + class="tag-pill inline-flex items-center gap-0.5 rounded bg-zinc-100 px-1.5 py-0.5 text-[10px] text-zinc-500 dark:bg-zinc-800 dark:text-zinc-400" |
| 91 | + > |
| 92 | + {tag} |
| 93 | + <span class="text-[9px] text-zinc-400 dark:text-zinc-600">{count}</span> |
| 94 | + </a> |
| 95 | + ))} |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | +</div> |
0 commit comments