-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentlayer.config.ts
88 lines (81 loc) · 2.41 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
// contentlayer.config.ts
import {defineDocumentType, makeSource} from 'contentlayer/source-files'
import remarkGfm from 'remark-gfm';
import rehypePrismPlus from "rehype-prism-plus"
import rehypeSlug from 'rehype-slug'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import {slug} from 'github-slugger'
import {writeFileSync} from "fs";
export const Post = defineDocumentType(() => ({
name: 'Post',
filePathPattern: `blog/**/*.md`,
contentType: 'mdx',
fields: {
title: { type: 'string', required: true},
date: { type: 'date', required: true},
tags: { type: 'list', of: {type: 'string'}},
category: {type: 'string'},
summary: {type: 'string'},
published: {type: 'boolean'},
featured: {type: 'string'},
},
computedFields: {
url: {type: 'string', resolve: (post) => `/blog/${post._raw.flattenedPath}`},
slug: {
type: "string",
resolve: (doc) => doc._raw.flattenedPath.replace(/^.+?(\/)/, "")
},
},
}))
export const Diary = defineDocumentType(() => ({
name: 'Diary',
filePathPattern: `diary/**/*.md`,
contentType: 'mdx',
fields: {
title: { type: 'string', required: true},
date: { type: 'date', required: true},
tags: { type: 'list', of: {type: 'string'}},
category: {type: 'string'},
summary: {type: 'string'},
published: {type: 'boolean'},
featured: {type: 'string'},
},
computedFields: {
url: {type: 'string', resolve: (post) => `/diary/${post._raw.flattenedPath}`},
slug: {
type: "string",
resolve: (doc) => doc._raw.flattenedPath.replace(/^.+?(\/)/, "")
},
},
}))
export default makeSource({
contentDirPath: 'datas',
documentTypes: [Post, Diary],
mdx: {
cwd: process.cwd(),
remarkPlugins: [remarkGfm],
rehypePlugins: [
rehypeSlug,
rehypeAutolinkHeadings,
[rehypePrismPlus, { defaultLanguage: 'js', ignoreMissing: true }],
],
},
onSuccess: async (importData) => {
const {allPosts} = await importData()
const tags: Record<string, number> = {}
allPosts.forEach((post) => {
// console.log(post.type, post.title)
if (post.tags && post.published) {
post.tags.forEach((tag) => {
const slugifyTag = slug(tag);
if (slugifyTag in tags) {
tags[slugifyTag] += 1
} else {
tags[slugifyTag] = 1
}
})
}
})
writeFileSync('./app/tag-data.json', JSON.stringify(tags))
}
})