-
Notifications
You must be signed in to change notification settings - Fork 1
/
contentlayer.config.ts
104 lines (98 loc) · 3 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
import { defineDocumentType, makeSource } from 'contentlayer/source-files'
import readingTime from 'reading-time'
import rehypeAutolinkHeadings, {
type Options as AutolinkHeadingsOptions,
} from 'rehype-autolink-headings'
import rehypeSlug from 'rehype-slug'
import remarkGfm from 'remark-gfm'
import { getSlug } from './lib/files'
import rehypeImgSizeWithFullWidth from './lib/rehypeImgSizeWithFullWidth'
import rehypeUrlInspector, { type Options as UrlInspectorOptions } from 'rehype-url-inspector'
import rehypePrettyCode, { type Options as PrettyCodeOptions } from 'rehype-pretty-code'
//@ts-ignore
import rehypeFigure from 'rehype-figure'
import { readFileSync } from 'fs'
const Post = defineDocumentType(() => ({
name: 'Post',
filePathPattern: `**/*.@(md|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,
},
summary: {
type: 'string',
description: 'The sammary of the post',
required: true,
},
tag: {
type: 'list',
of: {
type: 'string',
},
description: 'The tag of the post',
required: false,
},
},
computedFields: {
slug: {
type: 'string',
resolve: (doc) => getSlug(doc._raw.flattenedPath),
},
readtime: {
type: 'string',
resolve: (doc) => readingTime(doc.body.raw).text,
},
},
}))
/* 💡 A custom plugin to eliminate "../public" generated by the markdown editor for the next/image component. */
const imgSrcUrlConvertOption: UrlInspectorOptions = {
inspectEach(url) {
if (
url.node.properties &&
url.node.properties.src &&
typeof url.node.properties.src === 'string'
)
url.node.properties.src = url.node.properties.src.replace(/^.*\/public\//, '/')
},
selectors: [`img[src]`],
}
const autolinkHeadingOption: AutolinkHeadingsOptions = {
properties: {
className: ['anchor'],
},
}
const prettyCodeOptions: PrettyCodeOptions = {
theme: {
dark: JSON.parse(readFileSync('./code_theme/one-dark-pro-darker.json', 'utf-8')),
light: JSON.parse(readFileSync('./code_theme/atom-one-light.json', 'utf-8')),
},
tokensMap: {
fn: 'entity.name.function',
str: 'string',
var: 'variable.other.constant',
attr: 'variable.other.readwrite',
},
}
export default makeSource({
contentDirPath: process.env.CONTENTS_DIR || '__contents',
documentTypes: [Post],
mdx: {
remarkPlugins: [remarkGfm],
rehypePlugins: [
[rehypeUrlInspector, imgSrcUrlConvertOption], // 💡 You need to run the 'rehypeUrlInspector' plugin before the 'rehypeImgSizeWithFullWidth'
[rehypeFigure, { className: 'image-caption' }],
rehypeImgSizeWithFullWidth, // 💡 A custom plugin to enhance <img> tags by adding size (width/height) attributes.
rehypeSlug,
[rehypeAutolinkHeadings, autolinkHeadingOption],
[rehypePrettyCode, prettyCodeOptions],
],
},
})