This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentlayer.config.ts
136 lines (128 loc) · 4.27 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
import { defineDocumentType, defineNestedType, makeSource } from 'contentlayer/source-files'
import rehypePrism from '@mapbox/rehype-prism'
import sections from './sections.json'
import { readdirSync } from 'node:fs'
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
export const Page = defineDocumentType(() => ({
name: 'Page',
filePathPattern: `pages/**/*.mdx`,
contentType: 'mdx',
fields: {
title: { type: 'string', required: true },
activeMenuItem: { type: 'string', required: false },
customLayout: { type: 'boolean', required: false },
},
computedFields: {
slug: {
type: 'string',
resolve: page => {
const path = page._raw.flattenedPath
if (path === 'pages/index') return '/'
return path.slice("pages/".length)
},
},
},
}))
export const Homework = defineDocumentType(() => ({
name: 'Homework',
filePathPattern: `homework/**/*.mdx`,
contentType: 'mdx',
fields: {
title: { type: 'string', required: true },
isReleased: { type: 'boolean', required: true },
releaseDate: { type: 'date', required: false },
dueDate: { type: 'date', required: true },
auxiliaryDates: {
type: 'list',
of: {
type: 'nested',
def: () => ({
fields: {
name: { type: 'string', required: true },
date: { type: 'date', required: true },
},
}),
},
required: false,
},
},
computedFields: {
slug: { type: 'string', resolve: page => page._raw.flattenedPath.slice("homework/".length) },
body: {
type: 'mdx',
resolve(page) {
if (page.isReleased) return page.body
return undefined
},
},
},
}))
export const Assessment = defineDocumentType(() => ({
name: 'Assessment',
filePathPattern: `assessments/**/*.mdx`,
contentType: 'mdx',
fields: {
title: { type: 'string', required: true },
isReleased: { type: 'boolean', required: true },
releaseDate: { type: 'date', required: false },
assessmentDate: { type: 'date', required: true },
},
computedFields: {
slug: { type: 'string', resolve: page => page._raw.flattenedPath.slice("assessments/".length) },
body: {
type: 'mdx',
resolve(page) {
if (page.isReleased) return page.body
return undefined
},
},
},
}))
const LectureDates = defineNestedType(() => {
const fields = {}
sections.forEach(section => {
fields[section.id] = { type: 'date', required: false }
})
return {
name: 'LectureDates',
fields,
}
})
export const Lecture = defineDocumentType(() => ({
name: 'Lecture',
filePathPattern: `lectures/*/data.yaml`,
contentType: 'data',
fields: {
title: { type: 'string', required: true },
dates: {
type: 'nested',
of: LectureDates,
required: true,
},
},
computedFields: {
slug: { type: 'string', resolve: page => page._raw.sourceFileDir.slice("lectures/".length) },
files: {
type: 'list',
resolve: page => {
const contents = readdirSync(join(dirname(fileURLToPath(import.meta.url)), "../../../content", page._raw.sourceFileDir)).filter(file => file !== "data.yaml" && !file.startsWith("."))
console.log(`Files for ${page._raw.sourceFileDir}: ${contents}`)
return contents.toSorted((a, b) => {
// Sort anything that starts with "slides" first
if (a.startsWith("slides") && !b.startsWith("slides")) return -1
if (!a.startsWith("slides") && b.startsWith("slides")) return 1
// Then sort by name
return a.localeCompare(b)
})
},
}
},
}))
export default makeSource({
contentDirPath: 'content',
documentTypes: [Page, Homework, Assessment, Lecture],
mdx: {
rehypePlugins: [rehypePrism],
},
})