-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontentlayer.config.ts
More file actions
144 lines (137 loc) · 5.24 KB
/
contentlayer.config.ts
File metadata and controls
144 lines (137 loc) · 5.24 KB
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
import { defineDocumentType, makeSource, ComputedFields } from 'contentlayer/source-files';
import remarkGfm from 'remark-gfm';
import rehypePrettyCode from 'rehype-pretty-code';
import rehypeSlug from 'rehype-slug';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import readingTime from 'reading-time';
function slugify(text: string) {
if (!text) return '';
return text
.toString()
.toLowerCase()
.trim()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/&/g, '-and-') // Replace & with 'and'
.replace(/[^\w-]+/g, '') // Remove all non-word chars
.replace(/--+/g, '-'); // Replace multiple - with single -
}
const computedFields = {
slug: {
type: 'string',
resolve: doc => {
// NOTE: use this when we have multiple languages
// const lang = doc._raw.flattenedPath.split('/')[1];
const fileName = doc._raw.flattenedPath.split('/').slice(-1)[0];
const slug = fileName.split('.')[0].split('_')[1];
return slug;
},
},
url: { type: 'string', resolve: post => post._raw.flattenedPath },
readingTime: {
type: 'json',
resolve: doc => readingTime(doc.body.raw, { wordsPerMinute: 300 }),
},
headings: {
type: 'json',
resolve: async doc => {
const withoutCode = doc.body.raw.replace(/```[\s\S]*?```/g, '');
const headings = withoutCode.match(/#{1,6}\s+.+/g) || [];
return headings.map((heading: any) => {
const level = heading.match(/#/g)?.length || 0;
const text = heading.replace(/#/g, '').trim();
return {
level,
text,
slug: slugify(text),
};
});
},
},
} satisfies ComputedFields;
export const Post = defineDocumentType(() => ({
name: 'Post',
filePathPattern: 'posts/**/*.mdx',
contentType: 'mdx',
fields: {
title: { type: 'string', required: true },
publishedAt: { type: 'date', required: true },
summary: { type: 'string', required: false },
image: { type: 'string', required: false },
isFeatured: { type: 'boolean', required: false, default: false },
isArchived: { type: 'boolean', required: false, default: false },
keywords: { type: 'list', of: { type: 'string' }, required: false },
isDraft: { type: 'boolean', required: false, default: false },
toc: { type: 'boolean', required: false, default: true },
},
computedFields,
}));
export const Project = defineDocumentType(() => ({
name: 'Project',
filePathPattern: 'projects/**/*.mdx',
contentType: 'mdx',
fields: {
title: { type: 'string', required: true },
description: { type: 'string', required: true },
stack: { type: 'list', of: { type: 'string' }, required: true },
cover: { type: 'string', required: true },
isFeatured: { type: 'boolean', required: false, default: false },
isArchived: { type: 'boolean', required: false, default: false },
isDraft: { type: 'boolean', required: false, default: true },
live: { type: 'string', required: true },
github: { type: 'string', required: true },
},
computedFields,
}));
export const Snippet = defineDocumentType(() => ({
name: 'Snippet',
filePathPattern: 'snippets/**/*.mdx',
contentType: 'mdx',
fields: {
title: { type: 'string', required: true },
description: { type: 'string', required: false },
tags: { type: 'list', of: { type: 'string' }, required: false },
isDraft: { type: 'boolean', required: false, default: false },
},
computedFields,
}));
export default makeSource({
contentDirPath: 'content',
documentTypes: [Post, Snippet, Project],
mdx: {
remarkPlugins: [remarkGfm],
rehypePlugins: [
rehypeSlug,
[
rehypePrettyCode,
{
theme: 'poimandres',
tokensMap: {
// VScode command palette: Inspect Editor Tokens and Scopes
// https://github.com/Binaryify/OneDark-Pro/blob/47c66a2f2d3e5c85490e1aaad96f5fab3293b091/themes/OneDark-Pro.json
fn: 'entity.name.function',
objKey: 'meta.object-literal.key',
},
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: ' ' }];
}
node.properties.className = [''];
},
// onVisitHighlightedLine(node) {
// node.properties.className.push(HIGHLIGHTED_LINE);
// },
},
],
[
rehypeAutolinkHeadings,
{
properties: {
className: ['anchor'],
},
},
],
],
},
});