-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcontent-collections.ts
More file actions
41 lines (36 loc) · 1.11 KB
/
content-collections.ts
File metadata and controls
41 lines (36 loc) · 1.11 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
import { defineCollection, defineConfig } from "@content-collections/core";
import matter from "gray-matter";
import { z } from "zod";
function extractFrontMatter(content: string) {
const { data, content: body, excerpt } = matter(content, { excerpt: true });
return { data, body, excerpt };
}
const posts = defineCollection({
name: "posts",
directory: "./src/blogs",
include: "*.md",
schema: z.object({
title: z.string(),
published: z.string(),
description: z.string().optional(),
authors: z.string().array(),
content: z.string(),
}),
transform: ({ content, ...post }) => {
const frontMatter = extractFrontMatter(content);
// Extract header image (first image in the document)
const headerImageMatch = content.match(/!\[([^\]]*)\]\(([^)]+)\)/);
const headerImage = headerImageMatch ? headerImageMatch[2] : undefined;
return {
...post,
blogId: post._meta.path,
excerpt: frontMatter.excerpt,
description: frontMatter.data.description,
headerImage,
content: frontMatter.body,
};
},
});
export default defineConfig({
content: [posts],
});