forked from tszhong0411/honghong.me
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentlayer.config.ts
250 lines (235 loc) · 6.98 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
import {
defineDocumentType,
defineNestedType,
makeSource
} from 'contentlayer/source-files'
import { type Element, type Root } from 'hast'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import rehypePrettyCode from 'rehype-pretty-code'
import rehypeSlug from 'rehype-slug'
import remarkGfm from 'remark-gfm'
import { visit } from 'unist-util-visit'
import cn from './src/utils/cn'
import getLanguageIconByExtension from './src/utils/get-language-icon-by-extension'
const Techstack = defineNestedType(() => ({
name: 'Techstack',
fields: {
label: {
type: 'string',
description: 'The label of the techstack',
required: true
}
}
}))
const Project = defineDocumentType(() => ({
name: 'Project',
filePathPattern: 'projects/**/*.mdx',
contentType: 'mdx',
fields: {
name: {
type: 'string',
description: 'The name of the project',
required: true
},
description: {
type: 'string',
description: 'The description of the project',
required: true
},
homepage: {
type: 'string',
description: "The link to the project's homepage",
required: false
},
github: {
type: 'string',
description: "The url to the project's github page",
required: true
},
icon: {
type: 'string',
description: 'The name of the icon to use',
required: true
},
image: {
type: 'string',
description: 'Image for the project',
required: true
},
repo: {
type: 'string',
description: 'The name of the repo of the project',
required: true
},
techstack: {
type: 'list',
of: Techstack,
required: true
}
},
computedFields: {
slug: {
type: 'string',
resolve: (doc) => doc._raw.sourceFileName.replace(/\.mdx$/, '')
}
}
}))
const BlogPost = defineDocumentType(() => ({
name: 'BlogPost',
filePathPattern: 'blog/**/*.mdx',
contentType: 'mdx',
fields: {
title: {
type: 'string',
description: 'The title of the blog post',
required: true
},
date: {
type: 'string',
description: 'The date of the blog post',
required: true
},
modifiedTime: {
type: 'string',
description: 'The modified time of the blog post',
required: true
},
summary: {
type: 'string',
description: 'The summary of the blog post',
required: true
}
},
computedFields: {
slug: {
type: 'string',
resolve: (doc) => doc._raw.sourceFileName.replace(/\.mdx$/, '')
}
}
}))
const Pages = defineDocumentType(() => ({
name: 'Pages',
filePathPattern: 'pages/**/*.mdx',
contentType: 'mdx',
computedFields: {
slug: {
type: 'string',
resolve: (doc) => doc._raw.sourceFileName.replace(/\.mdx$/, '')
}
}
}))
const rehypeAddClassesToCodeBlocks = (className: string) => {
return (tree: Root) => {
visit(tree, 'element', (node, index, parent) => {
if (node.properties?.['data-rehype-pretty-code-fragment'] === undefined)
return
if (parent?.children === undefined) return
if (typeof index !== 'number') return
Object.assign(node.properties, {
className: cn(node.properties?.className as string, className)
})
})
}
}
const rehypeAddLanguageIconToCodeBlocks = () => {
return (tree: Root) => {
visit(tree, 'element', (node) => {
if (node.properties?.['data-rehype-pretty-code-title'] === undefined)
return
node.children.unshift(
getLanguageIconByExtension(node.properties['data-language'] as string)
)
})
}
}
const rehypeCodeBlocksDarkTheme = () => {
return (tree: Root) => {
visit(tree, 'element', (node, _, parent) => {
if (node.tagName !== 'pre' && node.tagName !== 'code') return
if (
node.tagName === 'code' &&
parent !== null &&
parent !== undefined &&
parent.type === 'element' &&
parent.tagName === 'pre'
)
return
if (!node.properties) return
const theme = node.properties['data-theme']
if (theme === undefined) return
Object.assign(node.properties, {
className: cn(
node.properties?.className,
theme === 'light'
? 'dark:hidden'
: ['hidden', node.tagName === 'pre' ? 'dark:block' : 'dark:inline']
)
})
})
}
}
export default makeSource({
contentDirPath: 'src/content',
documentTypes: [Project, BlogPost, Pages],
mdx: {
remarkPlugins: [remarkGfm],
rehypePlugins: [
rehypeSlug,
[
rehypeAutolinkHeadings,
{
properties: {
className: ['absolute left-0 top-0 bottom-0 w-full group'],
ariaLabel: 'Link to this section'
},
content: [
{
type: 'element',
tagName: 'svg',
properties: {
xmlns: 'http://www.w3.org/2000/svg',
width: 16,
height: 16,
fill: 'currentColor',
className:
'invisible absolute top-1/2 right-full h-4 w-4 -translate-y-1/2 text-secondary-foreground group-hover:visible mr-2',
viewBox: '0 0 24 24'
},
children: [
{
type: 'element',
tagName: 'path',
properties: {
d: 'M9.199 13.599a5.99 5.99 0 0 0 3.949 2.345 5.987 5.987 0 0 0 5.105-1.702l2.995-2.994a5.992 5.992 0 0 0 1.695-4.285 5.976 5.976 0 0 0-1.831-4.211 5.99 5.99 0 0 0-6.431-1.242 6.003 6.003 0 0 0-1.905 1.24l-1.731 1.721a.999.999 0 1 0 1.41 1.418l1.709-1.699a3.985 3.985 0 0 1 2.761-1.123 3.975 3.975 0 0 1 2.799 1.122 3.997 3.997 0 0 1 .111 5.644l-3.005 3.006a3.982 3.982 0 0 1-3.395 1.126 3.987 3.987 0 0 1-2.632-1.563A1 1 0 0 0 9.201 13.6zm5.602-3.198a5.99 5.99 0 0 0-3.949-2.345 5.987 5.987 0 0 0-5.105 1.702l-2.995 2.994a5.992 5.992 0 0 0-1.695 4.285 5.976 5.976 0 0 0 1.831 4.211 5.99 5.99 0 0 0 6.431 1.242 6.003 6.003 0 0 0 1.905-1.24l1.723-1.723a.999.999 0 1 0-1.414-1.414L9.836 19.81a3.985 3.985 0 0 1-2.761 1.123 3.975 3.975 0 0 1-2.799-1.122 3.997 3.997 0 0 1-.111-5.644l3.005-3.006a3.982 3.982 0 0 1 3.395-1.126 3.987 3.987 0 0 1 2.632 1.563 1 1 0 0 0 1.602-1.198z'
}
}
]
}
]
}
],
[
rehypePrettyCode,
{
theme: {
light: 'github-light',
dark: 'github-dark'
},
keepBackground: false,
onVisitTitle: (element: Element) => {
const theme = element.properties?.['data-theme']
Object.assign(element.properties, {
className: cn(
element.properties?.className,
theme === 'light' ? 'dark:hidden' : ['hidden dark:flex']
)
})
}
}
],
[rehypeAddClassesToCodeBlocks, 'relative'],
rehypeAddLanguageIconToCodeBlocks,
rehypeCodeBlocksDarkTheme
]
}
})