-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathvelite.config.ts
More file actions
185 lines (172 loc) · 5.2 KB
/
velite.config.ts
File metadata and controls
185 lines (172 loc) · 5.2 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
import path from "node:path";
import fs from "node:fs";
import { defineConfig, defineCollection, s } from "velite";
import { visit } from "unist-util-visit";
import rehypePrettyCode from "rehype-pretty-code";
import rehypeSlug from "rehype-slug";
import remarkToc from "remark-toc";
import { u } from "unist-builder";
const docs = defineCollection({
name: "Doc",
pattern: "docs/{*,**/*}.mdx",
schema: s
.object({
title: s.string(),
path: s.string(),
description: s.string(),
lastUpdated: s.isodate(),
links: s
.object({
source: s.string().optional(),
api_reference: s.string().optional(),
})
.optional(),
metadata: s.metadata(),
code: s.mdx(),
raw: s.raw(),
})
.transform((data) => ({
...data,
url: data.path === "/" ? "/docs" : `/docs${data.path}`,
})),
});
const blogs = defineCollection({
name: "Blog",
pattern: "blogs/**/*.mdx",
schema: s
.object({
title: s.string(),
slug: s.string(),
description: s.string(),
coverImage: s.string(),
publishedAt: s.isodate(),
author: s.object({
name: s.string(),
avatar: s.string(),
x: s.string().optional(),
linkedin: s.string().optional(),
}),
tags: s.array(s.string()),
status: s.enum(["draft", "published"]).default("draft"),
metadata: s.metadata(),
code: s.mdx(),
raw: s.raw(),
})
.transform((data) => ({
...data,
url: `/blogs/${data.slug}`,
})),
});
export default defineConfig({
root: "content",
output: {
data: ".velite",
assets: "public/static",
base: "/static/",
name: "[name]-[hash:6].[ext]",
clean: true,
},
collections: { docs, blogs },
mdx: {
remarkPlugins: [remarkToc],
rehypePlugins: [
rehypeSlug,
() => (tree: any) => {
// Load component config from JSON file
const configPath = path.join(process.cwd(), "velite-components.json");
const componentConfig = JSON.parse(fs.readFileSync(configPath, "utf8"));
visit(tree, (node: any) => {
if (node.name === "ComponentSource" && node.attributes) {
const nameAttr = node.attributes?.find((attr: any) => attr.name === "name");
const name = nameAttr?.value;
if (!name) {
return null;
}
// Try to get component, with fallback for Base UI
let component = componentConfig?.core?.[name];
if (!component && name.startsWith('baseui-')) {
// Fallback to Radix version
const radixName = name.replace('baseui-', '');
component = componentConfig?.core?.[radixName];
}
if (!component) {
return null;
}
const filePath = path.join(process.cwd(), component.filePath);
const source = fs.readFileSync(filePath, "utf8");
node.children?.push(
u("element", {
tagName: "pre",
properties: {
__src__: filePath,
},
children: [
u("element", {
tagName: "code",
properties: {
className: ["language-tsx"],
},
children: [
{
type: "text",
value: source,
},
],
}),
],
}),
);
}
if (node.name === "ComponentShowcase" && node.attributes) {
const nameAttr = node.attributes?.find((attr: any) => attr.name === "name");
const name = nameAttr?.value;
if (!name) {
return null;
}
// Try to get example, with fallback for Base UI
let component = componentConfig?.examples?.[name];
if (!component && name.startsWith('baseui-')) {
// Fallback to Radix version
const radixName = name.replace('baseui-', '');
component = componentConfig?.examples?.[radixName];
}
if (!component) {
return null;
}
const filePath = path.join(process.cwd(), component.filePath);
const source = fs.readFileSync(filePath, "utf8");
node.children?.push(
u("element", {
tagName: "pre",
properties: {
__src__: component.filePath,
__rawString__: source,
},
children: [
u("element", {
tagName: "code",
properties: {
className: ["language-tsx"],
},
children: [
{
type: "text",
value: source,
},
],
}),
],
}),
);
}
});
},
[
rehypePrettyCode as any,
{
theme: "dracula-soft",
},
],
],
},
});