-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentlayer.config.js
78 lines (73 loc) · 1.92 KB
/
contentlayer.config.js
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
import { defineDocumentType, makeSource } from 'contentlayer/source-files';
import highlight from 'rehype-highlight';
export const calculateReadingTime = (text) => {
// Step 2: Determine the average reading speed (words per minute)
const wordsPerMinute = 200;
// Step 3: Calculate the word count
const noOfWords = text.split(/\s/g).length;
// Step 4: Calculate the estimated reading time (in minutes)
const minutes = noOfWords / wordsPerMinute;
const readTime = Math.ceil(minutes);
// Step 5: Format the output
return `${minutes < 1 ? '<' : ''}${readTime} min read`;
};
export const Post = defineDocumentType(() => ({
name: 'Post',
filePathPattern: 'blog/**/*.mdx',
contentType: 'mdx',
fields: {
title: {
type: 'string',
description: 'The title of the post',
required: true,
},
date: {
type: 'date',
description: 'The date of the post',
required: true,
},
image: {
type: 'string',
description: 'The image of the post',
},
},
computedFields: {
url: {
type: 'string',
// eslint-disable-next-line no-underscore-dangle
resolve: (post) => `/${post._raw.flattenedPath}`,
},
readingTime: {
type: 'string',
// eslint-disable-next-line no-underscore-dangle
resolve: (post) => calculateReadingTime(post.body.raw),
},
},
}));
export const Project = defineDocumentType(() => ({
name: 'Project',
filePathPattern: 'projects/**/*.mdx',
contentType: 'mdx',
fields: {
name: {
type: 'string',
description: 'The name of the project',
required: true,
},
year: {
type: 'number',
description: 'The year the project was created',
},
url: {
type: 'string',
description: 'The URL to the project',
},
},
}));
export default makeSource({
contentDirPath: './contents',
documentTypes: [Post, Project],
mdx: {
rehypePlugins: [highlight],
},
});