-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontentlayer.config.ts
More file actions
81 lines (76 loc) · 2.58 KB
/
contentlayer.config.ts
File metadata and controls
81 lines (76 loc) · 2.58 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
import { defineDocumentType, makeSource } from "contentlayer/source-files";
import GithubSlugger from "github-slugger";
import readingTime from "reading-time";
import rehypeAutolinkHeadings from "rehype-autolink-headings";
import rehypeCodeTitles from "rehype-code-titles";
import rehypeImgSize from "rehype-img-size";
import rehypePrism from "rehype-prism-plus";
import rehypeSlug from "rehype-slug";
import remarkGfm from "remark-gfm";
import { searchTermOptimization } from "./src/utils/format";
// <-- 직접 경로 지정
const rehypeImgSizeAny: any = rehypeImgSize;
export const Post = defineDocumentType(() => ({
name: "Post",
filePathPattern: `**/*.mdx`,
contentType: "mdx",
fields: {
title: { type: "string", required: true },
date: { type: "date", required: true },
category: { type: "string", required: true },
description: { type: "string" },
tags: { type: "list", of: { type: "string" } },
headerImg: { type: "string" },
},
computedFields: {
url: { type: "string", resolve: post => `/${post._raw.flattenedPath}` },
headings: {
type: "json",
resolve: async post => {
const regXHN = /\n(?<flag>#{1,6})\s+(?<content>.+)/g; //! 일단 1~6 까지
const ghSlugger = new GithubSlugger();
const headings = Array.from(post.body.raw.matchAll(regXHN)).map(({ groups }: any) => {
const level = groups?.flag?.length || 0;
const content = groups?.content || "";
return {
level,
content,
slug: content ? ghSlugger.slug(content) : "",
};
});
return headings;
},
},
readingTime: { type: "string", resolve: doc => readingTime(doc.body.raw).text },
path: { type: "string", resolve: doc => doc._raw.flattenedPath.replace(/^(post|project)\//, "") },
postType: { type: "string", resolve: doc => doc._raw.sourceFileDir.split("/")[0] },
searchTxt: { type: "string", resolve: doc => searchTermOptimization(doc.title) },
// slug: {
// type: "string",
// resolve: (doc) => doc._raw.flattenedPath,
// },
},
}));
export default makeSource({
contentDirPath: "posts",
contentDirExclude: ["private"],
documentTypes: [Post],
mdx: {
remarkPlugins: [remarkGfm],
rehypePlugins: [
rehypeSlug, // heading 에 id 심기
// rehypeCodeWrap,
rehypeCodeTitles,
rehypePrism,
[rehypeImgSizeAny, { dir: "public" }],
[
rehypeAutolinkHeadings, // id 통해서 anchor 생성
{
properties: {
className: ["anchor"],
},
},
],
],
},
});