|
| 1 | +--- |
| 2 | +import BaseLayout from '~/layouts/BaseLayout.astro'; |
| 3 | +import BlogList from '~/components/BlogList.astro'; |
| 4 | +import PrevNext from '~/components/PrevNext.astro'; |
| 5 | +import { getCollection } from 'astro:content'; |
| 6 | +import type { GetStaticPathsOptions } from 'astro'; |
| 7 | +
|
| 8 | +const TAG_CHIPS_LIMIT = 12; |
| 9 | +
|
| 10 | +export async function getStaticPaths({ paginate }: GetStaticPathsOptions) { |
| 11 | + const allPosts = (await getCollection('blog', ({ data }) => !data.draft || import.meta.env.DEV)) |
| 12 | + .sort((a, b) => b.data.publishedAt.getTime() - a.data.publishedAt.getTime()); |
| 13 | +
|
| 14 | + // Collect aggregate filter data once and pass via props to every page. |
| 15 | + const categoryCounts = new Map<string, number>(); |
| 16 | + const tagCounts = new Map<string, number>(); |
| 17 | + for (const p of allPosts) { |
| 18 | + categoryCounts.set(p.data.category, (categoryCounts.get(p.data.category) ?? 0) + 1); |
| 19 | + for (const t of p.data.tags) tagCounts.set(t, (tagCounts.get(t) ?? 0) + 1); |
| 20 | + } |
| 21 | + const categories = Array.from(categoryCounts.entries()).sort((a, b) => b[1] - a[1]); |
| 22 | + const tags = Array.from(tagCounts.entries()).sort((a, b) => b[1] - a[1]); |
| 23 | +
|
| 24 | + return paginate(allPosts, { |
| 25 | + pageSize: 10, |
| 26 | + props: { categories, tags, totalPosts: allPosts.length }, |
| 27 | + }); |
| 28 | +} |
| 29 | +
|
| 30 | +const { page, categories, tags, totalPosts } = Astro.props as { |
| 31 | + page: import('astro').Page<import('astro:content').CollectionEntry<'blog'>>; |
| 32 | + categories: [string, number][]; |
| 33 | + tags: [string, number][]; |
| 34 | + totalPosts: number; |
| 35 | +}; |
| 36 | +const visibleTags = tags.slice(0, TAG_CHIPS_LIMIT); |
| 37 | +const hiddenTagsCount = Math.max(0, tags.length - TAG_CHIPS_LIMIT); |
| 38 | +--- |
| 39 | +<BaseLayout |
| 40 | + title={page.currentPage === 1 ? 'Blog — nSealr' : `Blog · page ${page.currentPage} — nSealr`} |
| 41 | + description="Release notes, research, tutorials, and news from the nSealr program." |
| 42 | +> |
| 43 | + <section class="container"> |
| 44 | + <p class="eyebrow">// blog</p> |
| 45 | + <h1>Notes on the program.</h1> |
| 46 | + <p class="lead">Release notes, research, tutorials, and news from nSealr signers, companion, and specs.</p> |
| 47 | + |
| 48 | + {page.currentPage === 1 && ( |
| 49 | + <> |
| 50 | + <div class="filter-block"> |
| 51 | + <p class="filter-label">Filter by category</p> |
| 52 | + <div class="filters"> |
| 53 | + <a href="/blog/" class="active">all <span class="n">{totalPosts}</span></a> |
| 54 | + {categories.map(([c, n]) => ( |
| 55 | + <a href={`/blog/categories/${c}/`}>{c} <span class="n">{n}</span></a> |
| 56 | + ))} |
| 57 | + </div> |
| 58 | + </div> |
| 59 | + |
| 60 | + {tags.length > 0 && ( |
| 61 | + <div class="filter-block"> |
| 62 | + <p class="filter-label">Filter by tag</p> |
| 63 | + <div class="filters"> |
| 64 | + {visibleTags.map(([t, n]) => ( |
| 65 | + <a href={`/blog/tags/${t}/`}>#{t} <span class="n">{n}</span></a> |
| 66 | + ))} |
| 67 | + {hiddenTagsCount > 0 && ( |
| 68 | + <a class="see-more" href="/blog/tags/">+{hiddenTagsCount} more →</a> |
| 69 | + )} |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + )} |
| 73 | + </> |
| 74 | + )} |
| 75 | + |
| 76 | + <BlogList posts={page.data} /> |
| 77 | + |
| 78 | + <PrevNext |
| 79 | + prev={page.url.prev ? { href: page.url.prev, title: `Page ${page.currentPage - 1}`, label: 'Newer posts' } : undefined} |
| 80 | + next={page.url.next ? { href: page.url.next, title: `Page ${page.currentPage + 1}`, label: 'Older posts' } : undefined} |
| 81 | + /> |
| 82 | + |
| 83 | + <p class="rss-row"><a href="/blog/rss.xml">RSS feed →</a></p> |
| 84 | + </section> |
| 85 | +</BaseLayout> |
| 86 | + |
| 87 | +<style> |
| 88 | + .container { padding-block: 60px; max-width: 880px; } |
| 89 | + h1 { font-family: var(--font-mono); color: var(--fg-strong); font-size: clamp(28px, 4vw, 44px); margin: 8px 0 12px; } |
| 90 | + .lead { color: var(--fg-muted); font-size: 17px; max-width: 64ch; margin: 0 0 22px; } |
| 91 | + .filter-block { margin-bottom: 16px; } |
| 92 | + .filter-label { |
| 93 | + font-family: var(--font-mono); font-size: 11px; |
| 94 | + color: var(--fg-muted); |
| 95 | + text-transform: uppercase; letter-spacing: 0.06em; |
| 96 | + margin: 0 0 6px; |
| 97 | + } |
| 98 | + .filters { display: flex; gap: 8px; flex-wrap: wrap; font-family: var(--font-mono); font-size: 12px; } |
| 99 | + .filters a { |
| 100 | + display: inline-flex; align-items: center; gap: 6px; |
| 101 | + color: var(--fg-muted); border: 1px solid var(--border); |
| 102 | + padding: 4px 10px; border-radius: var(--radius-pill); |
| 103 | + text-decoration: none; |
| 104 | + transition: color 120ms, border-color 120ms; |
| 105 | + } |
| 106 | + .filters a:hover { color: var(--accent-soft); border-color: var(--accent); } |
| 107 | + .filters a.active { color: var(--accent-soft); border-color: var(--accent); background: var(--accent-bg); } |
| 108 | + .filters a .n { color: var(--fg-muted); font-size: 10px; } |
| 109 | + .filters a:hover .n, .filters a.active .n { color: var(--accent-soft); } |
| 110 | + .filters a.see-more { color: var(--accent-soft); border-style: dashed; } |
| 111 | + .rss-row { margin-top: 28px; font-family: var(--font-mono); font-size: 12px; } |
| 112 | + .rss-row a { color: var(--accent-soft); } |
| 113 | +</style> |
0 commit comments