-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
contentlayer.config.ts
148 lines (139 loc) · 3.37 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
import {
ComputedFields,
defineDocumentType,
defineNestedType,
FieldDefs,
makeSource,
} from 'contentlayer2/source-files'
import { extractTocHeadings } from './src/mdx/remark-toc-headings.mjs'
import path from 'path'
import fs from 'fs'
const contentDirPath = 'docs'
const branch = process.env.NEXT_PUBLIC_GITHUB_BRANCH || 'main'
const baseFields: FieldDefs = {
title_seo: {
type: 'string',
description:
'The meta title of the doc, this will override the title extracted from the markdown and the nav title',
},
description: {
type: 'string',
description: 'The description of the doc',
},
image: {
type: 'string',
description: 'The image of the doc',
},
image_alt: {
type: 'string',
description: 'The image alt of the doc',
},
disable_edit: {
type: 'boolean',
description: 'Disable the github edit button',
},
canonical_url: {
type: 'string',
description: 'The canonical url of the doc, if different from the url',
},
}
const computedFields: ComputedFields = {
slug: {
type: 'string',
resolve: (doc) => doc._raw.flattenedPath,
},
toc: { type: 'json', resolve: (doc) => extractTocHeadings(doc.body.raw) },
title: {
type: 'string',
resolve: async (doc) => {
const headings = await extractTocHeadings(doc.body.raw, [1])
return headings[0]?.value
},
},
editUrl: {
type: 'string',
resolve: (doc) =>
`https://github.com/nitrictech/docs/edit/${branch}/docs/${doc._raw.sourceFilePath}`,
},
lastModified: {
type: 'date',
resolve: (doc) => {
// Get the full path to the markdown file
const filePath = path.join(
process.cwd(),
contentDirPath,
doc._raw.sourceFilePath,
)
// Extract and return the last modified date
const stats = fs.statSync(filePath)
return stats.mtime // This is the last modified date
},
},
}
const Doc = defineDocumentType(() => ({
name: 'Doc',
filePathPattern: '!**/guides/**/*.mdx',
fields: baseFields,
computedFields,
}))
const Featured = defineNestedType(() => ({
name: 'Featured',
fields: {
image: {
type: 'string',
description:
'The featured image of the post, not the same as og image. Use 1024x1024 with transparent background.',
required: true,
},
image_alt: {
type: 'string',
description: 'The featured image alt of the post',
required: true,
},
},
}))
const Guide = defineDocumentType(() => ({
name: 'Guide',
filePathPattern: '**/guides/**/*.mdx',
fields: {
...baseFields,
published_at: {
type: 'date',
description: 'The date the guide was published',
required: true,
},
updated_at: {
type: 'date',
description:
'The date the guide was last updated, will be set to published_at if not set',
},
featured: {
type: 'nested',
of: Featured,
},
tags: {
type: 'list',
of: {
type: 'string',
},
description: 'The tags of the post',
required: true,
},
languages: {
type: 'list',
of: {
type: 'string',
},
description: 'The languages of the content',
},
start_steps: {
type: 'markdown',
description: 'The start steps of the doc',
},
},
computedFields,
}))
export default makeSource({
contentDirPath,
documentTypes: [Doc, Guide],
})