Drive archive posts from content

This commit is contained in:
Loyyd 2026-06-11 14:19:57 +02:00
parent d74495ddef
commit b4025aecaf
4 changed files with 276 additions and 2 deletions

42
src/lib/content-routes.ts Normal file
View file

@ -0,0 +1,42 @@
import { getCollection } from "astro:content";
const blogSourcePattern = /^\/blog\/(?<year>\d{4})\/(?<month>\d{2})\/(?<day>\d{2})\/(?<slug>[^/]+)\.html$/;
export type BlogSourceRoute = {
year: string;
month: string;
day: string;
slug: string;
route: string;
};
export function parseBlogSource(source?: string): BlogSourceRoute | undefined {
if (!source) {
return undefined;
}
const match = blogSourcePattern.exec(source);
if (!match?.groups) {
return undefined;
}
const { year, month, day, slug } = match.groups;
return {
year,
month,
day,
slug,
route: `blog/${year}/${month}/${day}/${slug}.html`,
};
}
export async function contentSourceRoutes(): Promise<Set<string>> {
const entries = await getCollection("archive");
return new Set(
entries
.map((entry) => parseBlogSource(entry.data.source)?.route)
.filter((route): route is string => Boolean(route)),
);
}