|
| 1 | +<script setup lang="ts"> |
| 2 | +import { data as allPosts } from "../../posts.data"; |
| 3 | +import { computed } from "vue"; |
| 4 | +import { useRoute } from "vitepress"; |
| 5 | +
|
| 6 | +const route = useRoute(); |
| 7 | +
|
| 8 | +// 从路由 /archive/2018/ 中提取年份 |
| 9 | +const yearMatch = route.path.match(/\/archive\/(\d{4})\//); |
| 10 | +const year = yearMatch ? parseInt(yearMatch[1]) : null; |
| 11 | +
|
| 12 | +const yearPosts = computed(() => { |
| 13 | + if (!year) return []; |
| 14 | + return (allPosts as typeof allPosts).filter( |
| 15 | + (p) => new Date(p.date).getFullYear() === year, |
| 16 | + ); |
| 17 | +}); |
| 18 | +
|
| 19 | +const postsGroupedByMonth = computed(() => { |
| 20 | + const groups: Record<string, typeof yearPosts.value> = {}; |
| 21 | +
|
| 22 | + yearPosts.value.forEach((post) => { |
| 23 | + const date = new Date(post.date); |
| 24 | + const month = String(date.getMonth() + 1).padStart(2, "0"); |
| 25 | + const key = month; |
| 26 | +
|
| 27 | + if (!groups[key]) { |
| 28 | + groups[key] = []; |
| 29 | + } |
| 30 | + groups[key].push(post); |
| 31 | + }); |
| 32 | +
|
| 33 | + return Object.entries(groups) |
| 34 | + .sort(([a], [b]) => b.localeCompare(a)) |
| 35 | + .map(([month, posts]) => ({ month, posts })); |
| 36 | +}); |
| 37 | +
|
| 38 | +function formatDate(d: string) { |
| 39 | + if (!d) return ""; |
| 40 | + const dt = new Date(d.length === 10 ? d + "T00:00:00" : d); |
| 41 | + if (isNaN(dt.getTime())) return ""; |
| 42 | + return `${dt.getFullYear()}-${String(dt.getMonth() + 1).padStart(2, "0")}-${String(dt.getDate()).padStart(2, "0")}`; |
| 43 | +} |
| 44 | +</script> |
| 45 | + |
| 46 | +<template> |
| 47 | + <div class="archive-year-wrap" v-if="year && yearPosts.length > 0"> |
| 48 | + <p class="year-summary">{{ year }} 年共 {{ yearPosts.length }} 篇文章</p> |
| 49 | + |
| 50 | + <div class="months-list"> |
| 51 | + <div |
| 52 | + v-for="group in postsGroupedByMonth" |
| 53 | + :key="group.month" |
| 54 | + class="month-group" |
| 55 | + > |
| 56 | + <h3 class="month-title"> |
| 57 | + {{ group.month }} 月 ({{ group.posts.length }} 篇) |
| 58 | + </h3> |
| 59 | + <ul class="posts-in-month"> |
| 60 | + <li v-for="post in group.posts" :key="post.url" class="post-item"> |
| 61 | + <time class="post-date">{{ formatDate(post.date) }}</time> |
| 62 | + <a :href="post.url" class="post-title">{{ post.title }}</a> |
| 63 | + </li> |
| 64 | + </ul> |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + |
| 68 | + <div class="archive-nav"> |
| 69 | + <a href="/archive/" class="nav-link">← 返回归档</a> |
| 70 | + <a href="/posts/" class="nav-link">查看所有文章 →</a> |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + <div v-else class="no-posts"> |
| 74 | + <p>{{ year ? `${year} 年没有文章` : "获取年份失败" }}</p> |
| 75 | + <a href="/archive/">返回归档 →</a> |
| 76 | + </div> |
| 77 | +</template> |
| 78 | + |
| 79 | +<style scoped> |
| 80 | +.archive-year-wrap { |
| 81 | + max-width: 740px; |
| 82 | + padding: 1rem 0 4rem; |
| 83 | +} |
| 84 | +
|
| 85 | +.year-summary { |
| 86 | + font-size: 1.1rem; |
| 87 | + color: var(--vp-c-text-1); |
| 88 | + margin-bottom: 1.5rem; |
| 89 | + font-weight: 600; |
| 90 | +} |
| 91 | +
|
| 92 | +.months-list { |
| 93 | + display: flex; |
| 94 | + flex-direction: column; |
| 95 | + gap: 2rem; |
| 96 | + margin-bottom: 2rem; |
| 97 | +} |
| 98 | +
|
| 99 | +.month-group { |
| 100 | + border-left: 3px solid var(--vp-c-brand-1); |
| 101 | + padding-left: 1rem; |
| 102 | +} |
| 103 | +
|
| 104 | +.month-title { |
| 105 | + font-size: 1rem; |
| 106 | + color: var(--vp-c-brand-1); |
| 107 | + margin: 0 0 0.8rem 0; |
| 108 | + font-weight: 600; |
| 109 | +} |
| 110 | +
|
| 111 | +.posts-in-month { |
| 112 | + list-style: none; |
| 113 | + padding: 0; |
| 114 | + margin: 0; |
| 115 | + display: flex; |
| 116 | + flex-direction: column; |
| 117 | + gap: 0.6rem; |
| 118 | +} |
| 119 | +
|
| 120 | +.post-item { |
| 121 | + display: flex; |
| 122 | + gap: 1rem; |
| 123 | + align-items: center; |
| 124 | +} |
| 125 | +
|
| 126 | +.post-date { |
| 127 | + font-size: 0.9rem; |
| 128 | + color: var(--vp-c-text-3); |
| 129 | + min-width: 110px; |
| 130 | + flex-shrink: 0; |
| 131 | +} |
| 132 | +
|
| 133 | +.post-title { |
| 134 | + color: var(--vp-c-brand-1); |
| 135 | + text-decoration: none; |
| 136 | + flex: 1; |
| 137 | + transition: opacity 0.2s; |
| 138 | +} |
| 139 | +
|
| 140 | +.post-title:hover { |
| 141 | + opacity: 0.7; |
| 142 | +} |
| 143 | +
|
| 144 | +.archive-nav { |
| 145 | + display: flex; |
| 146 | + gap: 1rem; |
| 147 | + justify-content: center; |
| 148 | + padding-top: 1rem; |
| 149 | + border-top: 1px solid var(--vp-c-divider); |
| 150 | +} |
| 151 | +
|
| 152 | +.nav-link { |
| 153 | + color: var(--vp-c-brand-1); |
| 154 | + text-decoration: none; |
| 155 | + transition: opacity 0.2s; |
| 156 | +} |
| 157 | +
|
| 158 | +.nav-link:hover { |
| 159 | + opacity: 0.7; |
| 160 | +} |
| 161 | +
|
| 162 | +.no-posts { |
| 163 | + text-align: center; |
| 164 | + padding: 2rem; |
| 165 | + color: var(--vp-c-text-2); |
| 166 | +} |
| 167 | +
|
| 168 | +.no-posts a { |
| 169 | + color: var(--vp-c-brand-1); |
| 170 | +} |
| 171 | +</style> |
0 commit comments