-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcontent-collections.ts
129 lines (116 loc) · 3.38 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import path from 'path'
import type { Options } from '@content-collections/mdx'
import { defineCollection, defineConfig } from '@content-collections/core'
import { compileMDX } from '@content-collections/mdx'
import { isValid, parse } from 'date-fns'
import GithubSlugger from 'github-slugger'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import rehypeExternalLinks from 'rehype-external-links'
import rehypeKatex from 'rehype-katex'
import rehypePrettyCode from 'rehype-pretty-code'
import rehypeSlug from 'rehype-slug'
import remarkBreaks from 'remark-breaks'
import remarkGfm from 'remark-gfm'
import remarkMath from 'remark-math'
import { rehypeCodeSave, rehypeImage, rehypeToc } from './lib/mdx'
import { tocCache } from './lib/mdx/rehype-toc'
interface CollectionProps {
name: string
directory: string
prefixPath: string
}
const slugger = new GithubSlugger()
const options: Options = {
rehypePlugins: [
rehypeSlug,
[rehypeKatex, { output: 'html' }],
[rehypeAutolinkHeadings, { behavior: 'wrap' }],
[
rehypeExternalLinks,
{ target: '_blank', rel: ['nofollow', 'noopener', 'noreferrer'] },
],
rehypeCodeSave,
[
rehypePrettyCode,
{
theme: {
light: 'rose-pine-dawn',
dark: 'rose-pine-moon',
},
},
],
[rehypeImage, { root: 'public' }],
rehypeToc,
],
remarkPlugins: [remarkGfm, remarkBreaks, remarkMath],
}
const getCollection = ({ name, directory, prefixPath }: CollectionProps) =>
defineCollection({
name,
directory,
include: '**/*.md',
schema: (z) => ({
title: z.string(),
date: z
.string()
.refine(
(value) => isValid(parse(value, 'yyyy-MM-dd HH:mm:ss', new Date())),
{ message: 'Invalid datetime format or invalid date value.' },
),
tags: z.string().array(),
description: z.string().optional(),
level: z.enum(['Easy', 'Medium', 'Hard']).optional(), // Only for leetcode
}),
transform: async (document, context) => {
const match = document._meta.fileName.match(/^(\d+)-(.+)\.md$/)!
const [, no, title] = match
const slug = slugger.slug(title)
// handle polyglot url
let url = ''
if (prefixPath === '/polyglot') {
const language = document.tags[0].split('/')[0].toLowerCase()
url = path.join(prefixPath, language, slug)
console.log(document._meta.fileName)
} else {
url = path.join(prefixPath, slug)
}
const contentCode = await compileMDX(context, document, options)
const toc = tocCache.get(document._meta) ?? []
return {
...document,
no,
slug,
url,
contentCode,
toc,
}
},
})
const posts = getCollection({
name: 'posts',
directory: 'public/blog/posts',
prefixPath: '/blog/posts',
})
const notes = getCollection({
name: 'notes',
directory: 'public/blog/notes',
prefixPath: '/blog/notes',
})
const leetcode = getCollection({
name: 'leetcode',
directory: 'public/blog/leetcode',
prefixPath: '/blog/leetcode',
})
const album = getCollection({
name: 'album',
directory: 'public/blog/album',
prefixPath: '/album',
})
const polyglot = getCollection({
name: 'polyglot',
directory: 'public/blog/polyglot',
prefixPath: '/polyglot',
})
export default defineConfig({
collections: [posts, notes, leetcode, album, polyglot],
})