-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentlayer.config.js
101 lines (93 loc) · 2.95 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import remarkGfm from 'remark-gfm'
import rehypeSlug from 'rehype-slug'
import remarkMath from 'remark-math'
import GithubSlugger from 'github-slugger'
import rehypeHighlight from 'rehype-highlight'
import { defineDocumentType, makeSource} from 'contentlayer/source-files'
export const Doc = defineDocumentType(() => ({
name: 'Doc',
filePathPattern: 'docs/**/*.mdx',
contentType: 'mdx',
fields: {
title: { type: 'string', required: true },
section: { type: 'string', required: true },
},
computedFields: {
headings: {
type: 'json',
resolve: async (doc) => {
const headingsRegex = /\n(?<flag>#{1,6})\s+(?<content>.+)/g
const slugger = new GithubSlugger
const headings = Array.from(doc.body.raw.matchAll(headingsRegex)).map(
({ groups }) => {
const flag = groups?.flag
const content = groups?.content
return {
level: flag.length == 1 ? 'one'
: flag?.length == 2 ? 'two'
: 'three',
text: content,
slug: content ? slugger.slug(content) : undefined,
}
}
)
return headings
}
},
slug: { type: 'string', resolve: (doc) => doc._raw.sourceFileName.replace('.mdx', '') },
url: { type: 'string', resolve: (doc) => `/docs/${doc.slug}`}
}
}))
export const Navigation = defineDocumentType(() => ({
name: 'Navigation',
contentType: 'data',
filePathPattern: 'navigation.json',
fields: {
links: { type: 'json', required: true },
},
isSingleton: true
}))
export const Post = defineDocumentType(() => ({
name: 'Post',
contentType: 'mdx',
filePathPattern: 'posts/**/*.mdx',
fields: {
title: { type: 'string', required: true },
date: { type: 'date', required: true },
summary: { type: 'mdx', required: true },
version: { type: 'string', required: false },
},
computedFields: {
headings: {
type: 'json',
resolve: async (post) => {
const headingsRegex = /\n(?<flag>#{1,6})\s+(?<content>.+)/g
const slugger = new GithubSlugger
const headings = Array.from(post.body.raw.matchAll(headingsRegex)).map(
({ groups }) => {
const flag = groups?.flag
const content = groups?.content
return {
level: flag.length == 1 ? 'one'
: flag?.length == 2 ? 'two'
: 'three',
text: content,
slug: content ? slugger.slug(content) : undefined,
}
}
)
return headings
}
},
url: { type: 'string', resolve: (post) => `/blog/${post._raw.flattenedPath.split('/')[1]}` },
slug: { type: 'string', resolve: (post) => post._raw.flattenedPath.split('/')[1] },
}
}))
export default makeSource({
contentDirPath: 'content',
documentTypes: [Doc, Navigation, Post],
mdx: {
remarkPlugins: [remarkGfm, remarkMath],
rehypePlugins: [rehypeHighlight, rehypeSlug],
}
})