-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent-collections.ts
More file actions
44 lines (39 loc) · 1.4 KB
/
content-collections.ts
File metadata and controls
44 lines (39 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { defineCollection, defineConfig } from "@content-collections/core";
import { compileMDX } from "@content-collections/mdx";
const posts = defineCollection({
name: "posts",
directory: "content/posts",
include: "**/*.mdx",
exclude: "**/*.draft.mdx",
schema: (z) => ({
title: z.string(),
description: z.string(),
time: z.string(),
isDraft: z.boolean().optional().default(false),
ogUrl: z.string().optional(),
}),
transform: async (document, { collection, cache }) => {
const mdx = await compileMDX({ cache }, document);
const docs = await collection.documents();
// Helper function to parse dates with ordinal suffixes
const parseDate = (dateString: string): Date => {
const cleanDate = dateString.replace(/(\d+)(st|nd|rd|th)/g, '$1');
return new Date(cleanDate);
};
// Sort docs by date descending (newest first)
const sortedDocs = [...docs].sort(
(a, b) => parseDate(b.time).getTime() - parseDate(a.time).getTime(),
);
const idx = sortedDocs.findIndex((d) => document._meta.filePath === d._meta.filePath);
return {
...document,
mdx,
// Use sortedDocs instead of docs for prev/next to maintain chronological order
prev: idx > 0 ? sortedDocs[idx - 1] : null,
next: idx < sortedDocs.length - 1 ? sortedDocs[idx + 1] : null,
};
},
});
export default defineConfig({
collections: [posts],
});