-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontentlayer.config.ts
287 lines (271 loc) · 6.45 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import {
defineDocumentType,
makeSource,
FieldDefs,
} from "contentlayer/source-files";
const slugRegex = new RegExp(/^(.*)(.md|.mdx)/gi);
function createSlug(slug: string) {
const splitter: string | string[] = slug.toLowerCase().split("/");
return splitter[splitter.length - 1].split(".md")[0].replace(/\s+/g, "-");
}
/**
* Standard post fields, for ease of reuse
*/
const postFields: FieldDefs = {
title: {
type: "string",
description: "The primary title of the post",
required: true,
},
slug: {
type: "string",
description: "URL slug for the post",
required: false,
},
date: {
type: "date",
description: "The public date of the post",
required: false,
},
updatedAt: {
type: "date",
description: "The date this content was updated at",
required: false,
},
draft: {
type: "boolean",
description: "Draft status of the post",
required: false,
default: false,
},
featured: {
type: "boolean",
description: "Whether or not this content is featured",
required: false,
},
homepage: {
type: "boolean",
description: "Whether or not to display this content on the homepage",
required: false,
},
description: {
type: "string",
description:
"Brief description of the content (also used in the SEO metadata)",
required: false,
},
blurb: {
type: "string",
description: "One-liner description of the content",
required: false,
},
tags: {
type: "string",
// type: "list",
// of: { type: "string" },
description: "Comma separated listing of tags",
required: false,
},
image: {
type: "string",
description:
"The primary image of the post (also used in the SEO metadata)",
required: false,
},
imageFocus: {
type: "enum",
options: ["center", "left", "right"],
description: "Focus position of the posts image",
required: false,
},
nextPage: {
type: "string",
description: "",
required: false,
},
prevPage: {
type: "string",
description: "",
required: false,
},
};
/**
* Blog post schema
*/
export const Blog = defineDocumentType(() => ({
name: "Blog",
filePathPattern: `blog/**/*.md`,
fields: {
// use the standard post fields
...postFields,
// define custom fields now...
category: {
type: "enum",
options: ["blog", "newsletter", "devlog"],
default: "blog",
description: "The category this post belongs too",
required: true,
},
},
computedFields: {
draft: {
description: "Draft status of the post",
type: "boolean",
resolve: (post) =>
post.draft == true || post._raw.sourceFileName.startsWith("_"),
},
slug: {
description: "Computed slug of the post",
type: "string",
resolve: (post) => post?.slug ?? createSlug(post._id),
},
href: {
description: "Local url path of the content",
type: "string",
resolve: (post) =>
post.href ?? `/blog/${post?.slug ?? createSlug(post._id)}`,
},
},
}));
/**
* Article post schema
*/
export const Article = defineDocumentType(() => ({
name: "Article",
filePathPattern: `articles/**/*.md`,
fields: {
// use the standard post fields
...postFields,
// define custom fields now...
category: {
type: "string",
description: "",
required: false,
},
},
computedFields: {
draft: {
description: "Draft status of the post",
type: "boolean",
resolve: (post) =>
post.draft == true || post._raw.sourceFileName.startsWith("_"),
},
slug: {
description: "Computed slug of the post",
type: "string",
resolve: (post) => post?.slug ?? createSlug(post._id),
},
href: {
description: "Local url path of the content",
type: "string",
resolve: (post) =>
post.href ?? `/articles/${post?.slug ?? createSlug(post._id)}`,
},
tags: {
description: "Array listing of tags",
type: "list",
// of: { type: "string" },
resolve: (item) =>
item?.tags?.split(",")?.map((tag) => tag.trim()) ?? undefined,
},
},
}));
/**
* ArticleTag schema
*/
export const ArticleTag = defineDocumentType(() => ({
name: "ArticleTag",
filePathPattern: `tags/**/*.md`,
fields: {
// use the standard post fields
...postFields,
// define custom fields now...
// category: {
// type: "string",
// description: "",
// required: false,
// },
},
computedFields: {
slug: {
description: "Computed slug of the post",
type: "string",
resolve: (post) => post?.slug ?? createSlug(post._id),
},
href: {
description: "Local url path of the content",
type: "string",
resolve: (post) => `/tags/${post?.slug ?? createSlug(post._id)}`,
},
},
}));
/**
* Project schema
*/
export const Project = defineDocumentType(() => ({
name: "Project",
filePathPattern: `projects/**/*.md`,
fields: {
// use the standard post fields
...postFields,
// define custom fields now...
status: {
type: "enum",
options: ["active"],
description: "",
required: true,
},
url: {
type: "string",
description: "",
required: false,
},
logo: {
type: "string",
description: "",
required: false,
},
dateRange: {
type: "string",
description: "",
required: false,
},
heroImage: {
type: "string",
description: "",
required: false,
},
},
computedFields: {
draft: {
description: "Draft status of the post",
type: "boolean",
resolve: (post) =>
post.draft == true || post._raw.sourceFileName.startsWith("_"),
},
slug: {
description: "Computed slug of the project",
type: "string",
resolve: (post) => post._raw.sourceFileName.split(".")[0],
},
href: {
description: "Url path of the project (either local or absolute)",
type: "string",
resolve: (post) =>
post.url?.startsWith("http")
? new URL(post.url).toString()
: `/projects/${post._raw.sourceFileName.split(".")[0]}`,
},
tags: {
description: "Array listing of tags",
type: "list",
// of: { type: "string" },
resolve: (item) =>
item?.tags?.split(",")?.map((tag) => tag.trim()) ?? undefined,
},
},
}));
export default makeSource({
contentDirPath: "content",
documentTypes: [Project, Blog, Article, ArticleTag],
});