-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent-collections.ts
79 lines (72 loc) · 2.13 KB
/
content-collections.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
import { defineCollection, defineConfig } from '@content-collections/core'
import { promisify } from 'node:util'
import { exec as syncExec } from 'node:child_process'
import { compileMDX } from '@content-collections/mdx'
import path from 'path'
import remarkGfm from 'remark-gfm'
import remarkMath from 'remark-math'
import { CodeHikeConfig, remarkCodeHike } from 'codehike/mdx'
import rehypeSlug from 'rehype-slug'
import rehypeKatex from 'rehype-katex'
import getReadingTime from '@/lib/reading-time'
const postDirectory = 'src/content/posts'
const exec = promisify(syncExec)
async function lastModificationDate(filePath: string) {
const { stdout } = await exec(
`git log -1 --format=%ai -- ${path.join(postDirectory, filePath)}`
)
if (stdout) {
return new Date(stdout.trim()).toISOString()
}
return new Date().toISOString()
}
const chConfig: CodeHikeConfig = {
components: {
code: 'Code'
}
}
const posts = defineCollection({
name: 'posts',
directory: postDirectory,
include: '**/*.mdx',
schema: (z) => ({
title: z.string(),
published: z.boolean().nullable(),
summary: z.string().nullable(),
date: z.string(),
category: z.string(),
tags: z.array(z.string()),
lang: z.enum(['en', 'zh-CN']),
author: z.string()
}),
transform: async (post, ctx) => {
const mdx = await compileMDX(ctx, post, {
files: (appender) => {
const directory = path.join(
postDirectory,
post._meta.directory,
'components'
)
appender.directory('./components', directory)
},
remarkPlugins: [remarkMath, remarkGfm, [remarkCodeHike, chConfig]],
rehypePlugins: [rehypeKatex, rehypeSlug]
})
// const lastModification = await ctx.cache(
// post._meta.filePath,
// lastModificationDate
// )
return {
...post,
slug: post._meta.fileName.replace('.mdx', ''),
readingTime: getReadingTime(post.content),
content: {
mdx,
raw: post.content
}
}
}
})
export default defineConfig({
collections: [posts]
})