-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontentlayer.config.ts
229 lines (202 loc) · 5.37 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
import {
ComputedFields,
defineDocumentType,
FieldDefs,
makeSource,
} from 'contentlayer/source-files';
import codeMeta from './plugins/code-meta.mjs';
import remarkMilestones from './plugins/remark-milestone.mjs';
//@ts-ignore
import remarkRemoveComments from 'remark-remove-comments';
// //@ts-ignore
// import remarkEmbedImages from 'remark-embed-images';
// import removeHTMLComments from './plugins/remove-html-comments.mjs';
import fm from 'gray-matter';
import readingTime from 'reading-time';
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkRehype from 'remark-rehype';
// import remarkMdxCodeMeta from 'remark-mdx-code-meta';
import rehypeStringify from 'rehype-stringify';
// import { visit } from 'unist-util-visit';
// // import pkg from 'unist-util-visit';
// // const { visit } = pkg;
// /** @type {import('unified').Plugin<Array<void>, import('hast').Root>} */
// function rehypeMetaAsAttributes() {
// return (tree) => {
// visit(tree, 'element', (node) => {
// if (node.tagName === 'code' && node.data && node.data.meta) {
// node.properties.meta = node.data.meta;
// }
// });
// };
// }
async function excerptToHTML(excerpt: string) {
const file = await unified()
// .use(remarkEmbedImages)
.use(remarkParse)
//@ts-ignore
.use(remarkRehype)
// .use(rehypeMetaAsAttributes)
//@ts-ignore
.use(rehypeStringify)
.process(excerpt);
return String(file);
}
const pageFields: FieldDefs = {
title: {
type: 'string',
description: 'The title of the post',
required: true,
},
layout: {
type: 'enum',
options: ['Releases', 'API'],
},
description: {
type: 'string',
description: 'The description of the post',
},
since: {
type: 'string',
description: 'The version the feature was introduced',
},
metaDescription: {
type: 'string',
description: 'The metadata description of the post',
},
metaTitle: {
type: 'string',
description: 'The metadata title of the post',
},
redirect_to: {
type: 'string',
},
};
const pageComputedFields: ComputedFields<string> = {
url: {
type: 'string',
resolve: (docsPage) => {
const url = `/${docsPage._raw.flattenedPath}`
.replace('.page', '')
.replace('/index', '');
return url;
},
},
};
export const DocsPage = defineDocumentType(() => ({
name: 'DocsPage',
filePathPattern: `./docs/**/*.page.md`,
contentType: 'mdx',
fields: pageFields,
computedFields: pageComputedFields,
}));
export const RootPage = defineDocumentType(() => ({
name: 'RootPage',
filePathPattern: `./*.page.md`,
contentType: 'mdx',
fields: pageFields,
computedFields: pageComputedFields,
}));
export const Post = defineDocumentType(() => ({
name: 'Post',
filePathPattern: `./blog/**/*.md`,
contentType: 'mdx',
fields: {
title: {
type: 'string',
description: 'The title of the post',
required: true,
},
description: {
type: 'string',
description: 'The description of the post',
},
hide_in_homepage: {
type: 'boolean',
description: 'Whether to hide the post in the homepage',
},
author: {
type: 'json',
description: 'The authors of the post',
},
draft: {
type: 'boolean',
description: 'Whether the post is a draft',
},
excerpt: {
type: 'string',
description: 'A short excerpt of the post',
},
},
computedFields: {
url: {
type: 'string',
resolve: (post) => `/${post._raw.flattenedPath}`.replace('.page', ''),
},
date: {
type: 'string',
resolve: (post) => {
const [year, month, day] = post._raw.flattenedPath
.replace('blog/', '')
.split('/');
return [year, month, day].join('-');
},
},
description: {
type: 'string',
resolve: (post) => post.description || post.title,
},
excerpt: {
type: 'string',
resolve: async (post) => {
// const excerpt = post.body.raw.split('\n').slice(0, 2).join(' ');
const rawStr = post.body.raw;
const { data, excerpt } = fm(rawStr, {
//@ts-ignore
excerpt: function firstLine(file, options) {
//@ts-ignore
file.excerpt = file.content.split('\n').slice(0, 2).join(' ');
},
});
const theExcerpt = data.excerpt ?? excerpt;
return await excerptToHTML(theExcerpt.trimLeft().trim() || '');
},
},
readingTime: {
type: 'string',
resolve: (post) => {
try {
const time = readingTime(post.body.raw);
return time.text;
} catch (ex) {
return 'a few minutes read';
}
},
},
},
}));
export default makeSource({
contentDirPath: 'content',
documentTypes: [Post, DocsPage, RootPage],
mdx: {
esbuildOptions: (options) => {
// options.platform = 'node';
// options.packages = 'external';
return options;
},
// rehypePlugins: [rehypeMetaAsAttributes],
remarkPlugins: [
codeMeta({
NEXT_PUBLIC_BASE_URL:
process.env.NEXT_PUBLIC_BASE_URL ||
'https://infinite-table.com/.netlify/functions/json-server',
}),
remarkMilestones,
// remarkMdxCodeMeta,
// codeImport,
// remarkMdxCodeMeta,
/*remarkMdxCodeMeta rehypeMetaAsAttributes*/
],
},
});