-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent-collections.ts
69 lines (60 loc) · 1.88 KB
/
content-collections.ts
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { defineCollection, defineConfig } from "@content-collections/core";
import { compileMarkdown } from "@content-collections/markdown";
const markdownToPlainText = (markdown: string): string => {
// Remove headers (lines starting with #)
markdown = markdown.replace(/^#+\s.*$/gm, "");
// Remove images ()
markdown = markdown.replace(/!\[[^\]]*\]\([^\)]*\)/g, "");
// Replace links [text](url) with just "text"
markdown = markdown.replace(/\[([^\]]+)\]\([^\)]+\)/g, "$1");
// Remove any remaining formatting characters (*, _, etc.)
markdown = markdown.replace(/[*_~`]/g, "");
// Trim excess whitespace and normalize line breaks
return markdown.trim().replace(/\n{2,}/g, "\n");
};
const authorToID: Record<string, number> = {
andreoda: 1083,
riipah: 12,
shiroizu: 329,
pyther: 14922,
};
const authorToProfileImage: Record<string, string> = {
andreoda: "1083.jpg",
riipah: "12.png",
shiroizu: "329.jpg",
pyther: "14922.jpg",
};
const posts = defineCollection({
name: "posts",
directory: "posts",
include: "**/*.md",
schema: (z) => ({
title: z.string(),
slug: z.string(),
authors: z.array(z.string()),
date: z.string().date(),
cover: z.string().optional(),
}),
transform: async (doc, context) => {
const html = await compileMarkdown(context, doc);
const filteredMarkdown = doc.content
.split("\n")
.filter((line) => !line.startsWith("_"))
.join("\n");
const shortened = markdownToPlainText(filteredMarkdown)
.split(" ")
.slice(0, 50)
.join(" ");
const parsedAuthors = doc.authors
.filter((author) => author in authorToID)
.map((author) => ({
name: author,
id: authorToID[author],
avatarSrc: authorToProfileImage[author],
}));
return { ...doc, html, shortened, parsedAuthors };
},
});
export default defineConfig({
collections: [posts],
});