-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentlayer.config.ts
107 lines (99 loc) · 2.79 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
import { defineDocumentType, defineNestedType, makeSource } from 'contentlayer/source-files'
import { format } from 'date-fns'
import readingTime from 'reading-time'
import rehypeAutoLinkHeadings from 'rehype-autolink-headings'
import rehypeCodeTitles from 'rehype-code-titles'
import rehypeImgSize from 'rehype-img-size'
import rehypePrismPlus from 'rehype-prism-plus'
import rehypeSlug from 'rehype-slug'
import remarkBreaks from 'remark-breaks'
import remarkGfm from 'remark-gfm'
import remarkUnwrapImages from 'remark-unwrap-images'
import type { MDXOptions } from 'contentlayer/core'
const ReadTime = defineNestedType(() => ({
name: 'ReadTime',
fields: {
text: { type: 'string', required: true },
minutes: { type: 'number', required: true },
time: { type: 'number', required: true },
words: { type: 'number', required: true },
},
}))
export const Post = defineDocumentType(() => ({
name: 'Post',
filePathPattern: `posts/**/*.mdx`,
contentType: 'mdx',
fields: {
date: { type: 'date', required: true },
title: { type: 'string', required: true },
description: { type: 'string', required: true },
thumbnail: { type: 'string', required: true },
tags: { type: 'list', of: { type: 'string' } },
},
computedFields: {
dateFormatted: {
type: 'string',
resolve: (post) => format(new Date(post.date), 'yy.MM.dd'),
},
slug: {
type: 'string',
resolve: (post) => post._raw.sourceFileName.replace(/\.mdx$/, '').replace(/ /g, '-'),
},
readTime: {
type: 'nested',
of: ReadTime,
resolve: (post) => readingTime(post.body.raw),
},
},
}))
export const Log = defineDocumentType(() => ({
name: 'Log',
filePathPattern: `log/**/*.mdx`,
contentType: 'mdx',
fields: {
description: { type: 'string' },
tags: { type: 'list', of: { type: 'string' } },
},
computedFields: {
dateFormatted: {
type: 'string',
resolve: (post) => {
const splitPath = post._raw.flattenedPath.match(/(\d{2}\.\d{2})\/(\d{2}\.\d{2})/)
if (!splitPath) {
return undefined
}
const dateString = '20' + splitPath[1].split('.')[0] + '.' + splitPath[2]
return dateString.replace(/\./g, '-')
},
},
readTime: {
type: 'nested',
of: ReadTime,
resolve: (post) => readingTime(post.body.raw),
},
},
}))
export default makeSource({
contentDirPath: 'contents',
documentTypes: [Post, Log],
mdx: {
remarkPlugins: [remarkGfm, remarkUnwrapImages, remarkBreaks],
rehypePlugins: [
rehypeSlug,
rehypeCodeTitles,
rehypePrismPlus,
[
rehypeAutoLinkHeadings,
{
behavior: 'wrap',
},
],
[
rehypeImgSize,
{
dir: 'public',
},
],
],
} as MDXOptions,
})