-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
contentlayer.config.ts
90 lines (86 loc) · 2.49 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
import { all } from "@wooorm/starry-night"
import { defineDocumentType, makeSource } from "contentlayer2/source-files"
import { format, parseISO } from "date-fns"
import readingTime from "reading-time"
import rehypeMermaid from "rehype-mermaid"
import rehypeSlug from "rehype-slug"
import rehypeStarryNight from "rehype-starry-night"
import remarkEmoji from "remark-emoji"
import remarkGfm from "remark-gfm"
import { hashtagRegex, remarkHashtags } from "./src/lib/hashtags"
import { GenerateSocialImage } from "./src/lib/socialimage"
export const Post = defineDocumentType(() => ({
name: "Post",
filePathPattern: `**/*.md(x)?`,
contentType: "mdx",
fields: {
datetime: { type: "date", required: true },
draft: { type: "boolean", required: false, default: false },
id: { type: "number", required: true },
permalink: { type: "string", required: true },
title: { type: "string", required: true },
excerpt: { type: "markdown", required: false, default: "" },
},
computedFields: {
github: {
type: "string",
resolve: (post) => {
return `https://github.com/icco/writing/tree/main/posts/${post._raw.sourceFileName}`
},
},
url: {
type: "string",
resolve: (post) => {
if (post.draft) {
return `/api/draft?secret=${process.env.SECRET_TOKEN}&slug=${post.id}`
}
return post.permalink
},
},
tags: {
type: "list",
resolve: (post) => {
const match = post.body.raw.match(hashtagRegex)
if (!match) return []
const tags = new Set<string>(
match.map((m: string) =>
m.replace(hashtagRegex, "$<tag>").toLowerCase()
)
)
return Array.from(tags)
},
},
social_image: {
type: "string",
resolve: (post) =>
GenerateSocialImage(
post.title,
format(parseISO(post.datetime), "LLLL d, yyyy")
),
},
readingTime: {
type: "number",
resolve: (post) => readingTime(post.body.raw).minutes,
},
wordCount: {
type: "number",
resolve: (post) => readingTime(post.body.raw).words,
},
},
}))
export default makeSource({
contentDirPath: "posts",
documentTypes: [Post],
mdx: {
remarkPlugins: [
remarkHashtags,
remarkGfm,
[remarkEmoji, { emoticon: false, accessible: true }],
],
rehypePlugins: [
rehypeSlug,
[rehypeStarryNight, { grammars: all }],
[rehypeMermaid, { strategy: "img-svg", dark: true }],
],
},
})