-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontentlayer.config.ts
119 lines (112 loc) · 3.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* eslint-disable @typescript-eslint/no-explicit-any */
// Colocation guide: https://github.com/contentlayerdev/contentlayer/issues/84#issuecomment-1739699901
// contentlayer.config.ts
import { defineDocumentType, makeSource } from 'contentlayer2/source-files';
import fs from 'node:fs';
import path from 'path';
import readingTime from 'reading-time';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import rehypePrettyCode from 'rehype-pretty-code';
import rehypeSlug from 'rehype-slug';
export const Post = defineDocumentType(() => ({
name: 'Post',
filePathPattern: `**/posts/*.mdx`,
contentType: 'mdx',
fields: {
title: { type: 'string', required: true },
date: { type: 'date', required: true },
featuredImage: { type: 'string' },
category: { type: 'string' },
description: {
type: 'string',
description: 'Keep it at under 70 characters',
},
keywords: { type: 'list', of: { type: 'string' }, default: [] },
},
computedFields: {
readTimeStats: {
type: 'nested',
resolve: (post) => {
const content = fs.readFileSync(
path.join('content', post._raw.sourceFilePath),
'utf-8'
);
const body = content.split('---').at(-1)?.trim() ?? '';
return readingTime(body);
},
},
slug: {
type: 'string',
resolve: (post) => `${post._raw.flattenedPath.split('/').at(1)}`,
},
url: {
type: 'string',
resolve: (post) => `/content/${post._raw.flattenedPath}`,
},
},
}));
export const Project = defineDocumentType(() => ({
name: 'Project',
filePathPattern: `**/projects/*.mdx`,
contentType: 'mdx',
fields: {
title: { type: 'string', required: true },
description: { type: 'string', required: true },
date: { type: 'date', required: true },
featuredImage: { type: 'string', required: true },
featureOrder: { type: 'number' },
demoURL: { type: 'string' },
featuredYoutubeURL: { type: 'string' },
githubURL: { type: 'string' },
tags: { type: 'list', of: { type: 'string' } },
},
computedFields: {
slug: {
type: 'string',
resolve: (post) => `${post._raw.flattenedPath.split('/').at(1)}`,
},
url: {
type: 'string',
resolve: (project) => `/content/${project._raw.flattenedPath}`,
},
},
}));
export default makeSource({
contentDirPath: 'content',
documentTypes: [Post, Project],
// Plugins
mdx: {
// remarkPlugins: [remarkGfm],
rehypePlugins: [
rehypeSlug,
[
rehypePrettyCode,
{
theme: 'github-dark',
onVisitLine(node: any) {
// Prevent lines from collapsing in `display: grid` mode, and allow empty
// lines to be copy/pasted
if (node.children.length === 0) {
node.children = [{ type: 'text', value: ' ' }];
}
},
onVisitHighlightedLine(node: any) {
node.properties.className = [`line--highlighted`];
},
onVisitHighlightedWord(node: any) {
node.properties.className = [`word--highlighted`];
},
},
],
[
rehypeAutolinkHeadings,
{
properties: {
className: ['subheading-anchor'],
ariaLabel: 'Link to section',
},
},
],
],
},
});