-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentlayer.config.ts
179 lines (174 loc) · 4.8 KB
/
contentlayer.config.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import rehypeAutolinkHeadings from "rehype-autolink-headings";
import rehypeSlug from "rehype-slug";
import remarkGfm from "remark-gfm";
import { defineDocumentType, makeSource } from "contentlayer/source-files";
import rehypePrettyCode, { type Options } from "rehype-pretty-code";
import { promises as fs } from "fs";
import path from "path";
import { type RepoData } from "@/app/(main)/(narrow)/projects/[...slug]/github-widget";
export const Misc = defineDocumentType(() => ({
name: "Misc",
filePathPattern: "misc/**/*.mdx",
contentType: "mdx",
}));
export const Post = defineDocumentType(() => ({
name: "Post",
filePathPattern: "posts/**/*.mdx",
contentType: "mdx",
fields: {
published: { type: "boolean", required: false, default: true },
title: { type: "string", required: true },
date: { type: "date", required: true },
},
computedFields: {
url: {
type: "string",
resolve: (post) => `/${post._raw.flattenedPath}`,
},
},
}));
export const UI = defineDocumentType(() => ({
name: "UIComponent",
filePathPattern: "experiments/ui/*/component.mdx",
contentType: "mdx",
fields: {
title: { type: "string", required: true },
description: { type: "string", required: true },
dependencies: { type: "list", of: { type: "string" }, required: false },
registryDependencies: {
type: "list",
of: { type: "string" },
required: false,
},
colSpan: { type: "number", required: false },
rowSpan: { type: "number", required: false },
},
computedFields: {
componentId: {
type: "string",
resolve: (component): string =>
// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
component._raw.flattenedPath.match(
/(?<=^experiments\/ui\/).*(?=\/component)/g,
)?.[0]!,
},
componentCode: {
type: "string",
resolve: async (component): Promise<string> => {
const code = await fs.readFile(
path.join(
process.cwd(),
"src/components/ui",
`${
component._raw.flattenedPath.match(
/(?<=^experiments\/ui\/).*(?=\/component)/g,
)?.[0]
}.tsx`,
),
);
return code.toString();
},
},
examples: {
type: "list",
resolve: async (component): Promise<string[]> => {
const data = await fs
.readdir(
path.join(
process.cwd(),
"src/content/experiments/ui",
// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
component._raw.flattenedPath.match(
/(?<=^experiments\/ui\/).*(?=\/component)/g,
)?.[0]!,
),
)
.then((files) =>
files
.filter((file) => file.endsWith(".tsx") && file !== "example.tsx")
.map((file) => file.replace(".tsx", ""))
.filter((file) => file !== "mini-example"),
);
return data;
},
},
url: {
type: "string",
resolve: (component) => `/${component._raw.flattenedPath}`,
},
},
}));
export const Project = defineDocumentType(() => ({
name: "Project",
filePathPattern: "projects/**/*.mdx",
contentType: "mdx",
fields: {
published: { type: "boolean", required: false, default: true },
title: { type: "string", required: true },
repo: { type: "string", required: true },
tech: {
type: "list",
required: true,
of: {
type: "enum",
options: [
"react",
"next",
"cloudflare",
"drizzle",
"tailwind",
"vercel",
"shadcn",
"turso",
],
},
},
},
computedFields: {
url: {
type: "string",
resolve: (project) => `/${project._raw.flattenedPath}`,
},
repoData: {
type: "json",
resolve: async (project) => {
const projectData = await fetch(
`https://api.github.com/repos/${project.repo}`,
).then(async (res) => {
return (await res.json()) as RepoData;
});
return projectData;
},
},
},
}));
export default makeSource({
contentDirPath: "src/content",
documentTypes: [Post, Project, Misc, UI],
mdx: {
remarkPlugins: [remarkGfm],
rehypePlugins: [
rehypeSlug,
[
rehypeAutolinkHeadings,
{
properties: {
className: ["subheading-anchor"],
ariaLabel: "Link to section",
},
},
],
[
rehypePrettyCode as (options: Options) => undefined,
{
theme: {
light: "github-light",
dark: "github-dark",
},
keepBackground: false,
defaultLang: "plaintext",
},
],
],
},
});