常用配置、脚本和工程片段合集,适合复制后快速改造成项目内工具。
import { getCollection } from "astro:content"; export async function getSortedItems() { const items = await getCollection("items"); return items.sort((a, b) => { if (a.data.featured !== b.data.featured) { return a.data.featured ? -1 : 1; } const dateA = a.data.updated ? new Date(a.data.updated) : new Date(0); const dateB = b.data.updated ? new Date(b.data.updated) : new Date(0); return dateA > dateB ? -1 : 1; }); }
按 featured 和日期排序 Astro content collection 的常用辅助函数。