-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvelite.config.ts
More file actions
217 lines (202 loc) · 6.07 KB
/
velite.config.ts
File metadata and controls
217 lines (202 loc) · 6.07 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { exec } from 'node:child_process';
import { basename } from 'node:path';
import { promisify } from 'node:util';
import rehypePrettyCode from 'rehype-pretty-code';
import { defineCollection, defineConfig, s } from 'velite';
const slugify = (input: string) =>
input
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^a-z0-9-]/g, '');
const icon = s.enum(['github', 'instagram', 'medium', 'twitter', 'youtube']);
const count = s
.object({ total: s.number(), posts: s.number() })
.default({ total: 0, posts: 0 });
const meta = s
.object({
title: s.string().optional(),
description: s.string().optional(),
keywords: s.array(s.string()).optional(),
})
.default({});
const execAsync = promisify(exec);
// refer to https://velite.js.org/guide/last-modified#based-on-git-timestamp for more details
const timestamp = () =>
s
.custom<string | undefined>((i) => i === undefined || typeof i === 'string')
.transform<string>(async (value, { meta, addIssue }) => {
if (value != null) {
addIssue({
fatal: false,
code: 'custom',
message:
'`s.timestamp()` schema will resolve the value from `git log -1 --format=%cd`',
});
}
const { stdout } = await execAsync(
`git log -1 --format=%cd ${meta.path}`,
);
return new Date(stdout || Date.now()).toISOString();
});
const options = defineCollection({
name: 'Options',
pattern: 'options/index.yml',
single: true,
schema: s.object({
name: s.string().max(20),
title: s.string().max(99),
description: s.string().max(999).optional(),
keywords: s.array(s.string()),
author: s.object({
name: s.string(),
email: s.string().email(),
url: s.string().url(),
}),
links: s.array(
s.object({
text: s.string(),
link: s.string(),
type: s.enum(['navigation', 'footer', 'copyright']),
}),
),
socials: s.array(
s.object({
name: s.string(),
icon,
link: s.string().optional(),
image: s.image().optional(),
}),
),
}),
});
const categories = defineCollection({
name: 'Category',
pattern: 'categories/*.yml',
schema: s
.object({
name: s.string().max(20),
slug: s.slug('global', ['admin', 'login']),
cover: s.image().optional(),
description: s.string().max(999).optional(),
count,
})
.transform((data) => ({ ...data, permalink: `/${data.slug}` })),
});
const tags = defineCollection({
name: 'Tag',
pattern: 'tags/index.yml',
schema: s
.object({
name: s.string().max(20),
slug: s.slug('global', ['admin', 'login']),
cover: s.image().optional(),
description: s.string().max(999).optional(),
count,
})
.transform((data) => ({ ...data, permalink: `/${data.slug}` })),
});
// const pages = defineCollection({
// name: 'Page',
// pattern: 'pages/**/*.mdx',
// schema: s
// .object({
// title: s.string().max(99),
// slug: s.slug('global', ['admin', 'login']),
// body: s.mdx(),
// })
// .transform((data) => ({ ...data, permalink: `/${data.slug}` })),
// });
const posts = defineCollection({
name: 'Post',
pattern: 'posts/**/*.mdx',
schema: s
.object({
title: s.string().max(99),
createdAt: s.isodate(),
author: s.string(),
tags: s.array(s.string()),
categories: s.array(s.string()),
summary: s.string(),
slug: s.path().transform<string>((data) => basename(data)),
// date: s.isodate(),
updatedAt: s.isodate().optional(),
// cover: s.image().optional(),
// video: s.file().optional(),
// description: s.string().max(999).optional(),
draft: s.boolean().default(false),
// featured: s.boolean().default(false),
// categories: s.array(s.string()).default(['Journal']),
// tags: s.array(s.string()).default([]),
meta: meta,
toc: s.toc({
maxDepth: 3,
minDepth: 1,
}),
metadata: s.metadata(),
excerpt: s.excerpt(),
content: s.markdown(),
})
.transform((data) => ({
...data,
lastModified: timestamp(),
permalink: `/posts/${data.slug}`,
})),
});
export default defineConfig({
root: 'content',
output: {
data: '.velite',
assets: 'public/static',
base: '/static/',
name: '[name]-[hash:6].[ext]',
clean: true,
},
collections: { categories, tags, posts }, //options, pages
markdown: { rehypePlugins: [rehypePrettyCode] },
prepare: ({ categories, tags, posts }) => {
const docs = posts.filter(
(i) => process.env.NODE_ENV !== 'production' || !i.draft,
);
// missing categories, tags from posts or courses inlined
const categoriesFromDoc = Array.from(
new Set(docs.flatMap((i) => i.categories)),
).filter((i) => categories.find((j) => j.name === i) == null);
categories.push(
...categoriesFromDoc.map((name) => ({
name,
slug: slugify(name),
permalink: '',
count: { total: 0, posts: 0 },
})),
);
for (const category of categories) {
category.count.posts = posts.filter((j) =>
j.categories.includes(category.name),
).length;
category.count.total = category.count.posts;
category.permalink = `/${category.slug}`;
}
const tagsFromDoc = Array.from(new Set(docs.flatMap((i) => i.tags))).filter(
(i) => tags.find((j) => j.name === i) == null,
);
tags.push(
...tagsFromDoc.map((name) => ({
name,
slug: slugify(name),
permalink: '',
count: { total: 0, posts: 0 },
})),
);
for (const tag of tags) {
tag.count.posts = posts.filter((j) => j.tags.includes(tag.name)).length;
tag.count.total = tag.count.posts;
tag.permalink = `/${tag.slug}`;
}
// push extra data to collections, it's ok!! but they are not type-safed
// Object.assign(collections, {
// anything: { name: 'Anything', data: { name: 'Anything' } },
// list: ['one', 'two', 'three']
// })
// return false // return false to prevent velite from writing data to disk
},
});