-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontentlayer.config.ts
More file actions
102 lines (97 loc) · 2.79 KB
/
contentlayer.config.ts
File metadata and controls
102 lines (97 loc) · 2.79 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
import { all } from "@wooorm/starry-night"
import { defineDocumentType, makeSource } from "contentlayer2/source-files"
import { format, parseISO } from "date-fns"
import fs from "fs"
import readingTime from "reading-time"
import rehypeGithubEmoji from "rehype-github-emoji"
import rehypeMermaid from "rehype-mermaid"
import rehypeSlug from "rehype-slug"
import rehypeStarryNight from "rehype-starry-night"
import remarkGfm from "remark-gfm"
import { hashtagRegex, remarkHashtags } from "./src/lib/hashtags"
import { normalizeTag } from "./src/lib/tagAliases"
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 },
summary: { type: "string", 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) =>
normalizeTag(m.replace(hashtagRegex, "$<tag>"))
)
)
return Array.from(tags)
},
},
social_image: {
type: "string",
resolve: (post) => {
const params = new URLSearchParams({
title: post.title,
date: format(parseISO(post.datetime), "LLLL d, yyyy"),
})
return `/api/og?${params.toString()}`
},
},
readingTime: {
type: "number",
resolve: (post) => readingTime(post.body.raw).minutes,
},
wordCount: {
type: "number",
resolve: (post) => readingTime(post.body.raw).words,
},
modifiedAt: {
type: "string",
resolve: (doc) => {
return new Date(
fs.statSync("posts/" + doc._raw.sourceFilePath).mtime
).toISOString()
},
},
},
}))
export default makeSource({
contentDirPath: "posts",
documentTypes: [Post],
mdx: {
remarkPlugins: [remarkHashtags, remarkGfm],
rehypePlugins: [
rehypeSlug,
rehypeGithubEmoji,
[rehypeStarryNight, { grammars: all }],
[
rehypeMermaid,
{ strategy: "img-svg", dark: true, mermaidConfig: { layout: "elk" } },
],
],
},
})