-
Notifications
You must be signed in to change notification settings - Fork 1
/
contentlayer.config.ts
108 lines (100 loc) · 2.94 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
import { ComputedFields, defineDocumentType, makeSource } from 'contentlayer/source-files';
import readingTime from 'reading-time';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import rehypeCodeTitles from 'rehype-code-titles';
import rehypeKatex from 'rehype-katex';
import rehypePrismPlus from 'rehype-prism-plus';
import rehypeProbeImageSize from 'rehype-probe-image-size';
import rehypeSlug from 'rehype-slug';
import rehypeTwemojify from 'rehype-twemojify';
import remarkGemoji from 'remark-gemoji';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
import remarkTextr from 'remark-textr';
import smartquotes from 'smartquotes-ts';
function trademarks(input: string) {
input = input.replace(/\((c|C)\)/gim, '©');
input = input.replace(/\((r|R)\)/gim, '®');
input = input.replace(/\((p|P)\)/gim, '℗');
input = input.replace(/\((tm|TM)\)/gim, '™');
return input;
}
function quotes(input: string) {
return smartquotes.string(input);
}
function ellipses(input: string) {
return input.replaceAll('...', '\u2026');
}
function dashes(input: string) {
return input.replaceAll('---', '—').replaceAll('--', '–');
}
const computedFields: ComputedFields = {
readingTime: {
type: 'json',
resolve: (doc) => readingTime(doc.body.raw, { wordsPerMinute: 275 })
},
wordCount: {
type: 'number',
resolve: (doc) => doc.body.raw.split(/\s+/gu).length
},
tweetIds: {
type: 'json',
resolve: (doc) => {
const tweetMatches = doc.body.raw.match(/<StaticTweet\sid="[0-9]+"\s\/>/g);
const tweetIDs = tweetMatches?.map((tweet: string) => tweet.match(/[0-9]+/g)![0]);
return tweetIDs ?? [];
}
},
slug: {
type: 'string',
resolve: (doc) => doc._raw.sourceFileName.replace(/\.mdx$/, '')
}
};
const Post = defineDocumentType(() => ({
name: 'Post',
filePathPattern: 'posts/**/*.{mdx,md}',
contentType: 'mdx',
fields: {
title: { type: 'string', required: true },
publishedAt: { type: 'string', required: true },
summary: { type: 'string', required: true }
},
computedFields
}));
const contentLayerConfig = makeSource({
contentDirPath: 'data',
documentTypes: [Post],
mdx: {
remarkPlugins: [
remarkMath,
remarkGfm,
remarkGemoji,
[remarkTextr, { plugins: [dashes, ellipses, trademarks, quotes] }]
],
rehypePlugins: [
rehypeKatex,
rehypeSlug,
[
rehypeAutolinkHeadings,
{
properties: {
className: ['anchor']
},
behavior: 'append'
}
],
rehypeCodeTitles,
[rehypePrismPlus, { showLineNumbers: true }],
[rehypeProbeImageSize as any, { staticDir: 'public' }],
[
rehypeTwemojify,
{
params: { w: 32, q: 100 },
twemoji: { size: 'svg', baseUrl: '/static/images/twemoji' },
exclude: ['©', '®', '™', '℗', '↩']
}
]
]
}
});
export default contentLayerConfig;