(slug: string, limit = 3)
| 205 | } |
| 206 | |
| 207 | export async function getRelatedPosts(slug: string, limit = 3): Promise<BlogMeta[]> { |
| 208 | const posts = await getAllPostMeta() |
| 209 | const current = posts.find((p) => p.slug === slug) |
| 210 | if (!current) return [] |
| 211 | const others = posts.filter((p) => p.slug !== slug) |
| 212 | const scored = others |
| 213 | .map((p) => ({ |
| 214 | post: p, |
| 215 | score: p.tags.filter((t) => current.tags.includes(t)).length, |
| 216 | })) |
| 217 | .sort((a, b) => b.score - a.score || byDateDesc(a.post, b.post)) |
| 218 | .slice(0, limit) |
| 219 | .map((x) => x.post) |
| 220 | return scored |
| 221 | } |
no test coverage detected