forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenDocs.ts
217 lines (176 loc) · 5.77 KB
/
genDocs.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
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 * as fs from "node:fs";
import * as path from "node:path";
import {
addTitleToGroups,
Files,
getExampleProjects,
getProjectFiles,
groupProjects,
Project,
} from "./util";
import { fileURLToPath } from 'url';
import { dirname } from 'path';
/*
`genDocs` generates the nextjs example blocks for the website docs.
Note that these files are not checked in to the repo, so this command should always be run before running / building the site
*/
const dir = dirname(fileURLToPath(import.meta.url));
const getLanguageFromFileName = (fileName: string) => fileName.split(".").pop();
/******* templates + generate functions *******/
const templateExampleBlock = (
project: Project,
files: Files
) => `import { ExampleBlock } from "@/components/example/ExampleBlock";
import { Tabs } from "nextra/components";
<ExampleBlock name="${project.fullSlug}" path="${
project.pathFromRoot
}" isProExample={props.isProExample}>
<Tabs items={${JSON.stringify(
Object.keys(files).map((fileName) => fileName.slice(1))
)}}>
${Object.entries(files)
.map(
([filename, file]) =>
`<Tabs.Tab>
<div className={"max-h-96 overflow-auto rounded-lg overscroll-contain"}>
\`\`\`${getLanguageFromFileName(filename)}
${file.code}
\`\`\`
</div>
</Tabs.Tab>`
)
.join("")}
</Tabs>
</ExampleBlock>`;
const COMPONENT_DIR = path.resolve(
dir,
"../../../docs/components/example/generated/"
);
const EXAMPLES_PAGES_DIR = path.resolve(dir, "../../../docs/pages/examples/");
/**
* Generates the <ExampleBlock> component that has all the source code of the example
* This block can be used both in the /docs and in the /example page
*/
async function generateCodeForExample(project: Project) {
const target = path.join(COMPONENT_DIR, "mdx", project.fullSlug + ".mdx");
const files = getProjectFiles(project);
const filtered = Object.fromEntries(
Object.entries(files).filter(([filename, file]) => !file.hidden)
);
const code = templateExampleBlock(project, filtered);
fs.mkdirSync(path.dirname(target), { recursive: true });
fs.writeFileSync(target, code);
}
const templatePageForExample = (
project: Project,
readme: string
) => `import { Example } from "@/components/example";
${readme}
<Example name="${project.fullSlug}" />`;
/**
* Generate the page for the example in /examples overview
*
* Consists of the contents of the readme + the interactive example
*/
async function generatePageForExample(project: Project) {
const target = path.join(EXAMPLES_PAGES_DIR, project.fullSlug + ".mdx");
const files = getProjectFiles(project);
const code = templatePageForExample(project, files["/README.md"]!.code);
fs.writeFileSync(target, code);
}
/**
* generates _meta.json file for each example group, so that order is preserved
*/
async function generateMetaForExampleGroup(group: {
title: string;
slug: string;
projects: Project[];
}) {
if (!fs.existsSync(path.join(EXAMPLES_PAGES_DIR, group.slug))) {
fs.mkdirSync(path.join(EXAMPLES_PAGES_DIR, group.slug));
}
const target = path.join(EXAMPLES_PAGES_DIR, group.slug, "_meta.json");
const meta = Object.fromEntries(
group.projects.map((project) => [
project.projectSlug,
{
title: project.config.shortTitle || project.title,
},
])
);
const code = JSON.stringify(meta, undefined, 2);
fs.writeFileSync(target, code);
}
const templateExampleComponents = (
projects: Project[]
) => `// generated by dev-scripts/examples/genDocs.ts
import dynamic from "next/dynamic";
export const examples = {
${projects
.map((p) => {
const importPath = `../../../../${p.pathFromRoot}/App`;
return ` "${p.fullSlug}": {
// App: () => <div>hello</div>,
App: dynamic(() => import(${JSON.stringify(importPath)}), {
ssr: false,
}),
ExampleWithCode: dynamic(() => import("./mdx/${p.fullSlug}.mdx"), {
//ssr: false,
}),
pro: ${p.config.pro || false}
},`;
})
.join("\n")}
};`;
/**
* Generate the file with all the dynamic imports for examples (exampleComponents.gen.tsx)
*/
async function generateExampleComponents(projects: Project[]) {
const target = path.join(COMPONENT_DIR, "exampleComponents.gen.tsx");
const code = templateExampleComponents(projects);
fs.writeFileSync(target, code);
}
/**
* generates exampleList.gen.ts file with data about all the examples
*/
async function generateExampleList(projects: Project[]) {
const target = path.join(COMPONENT_DIR, "exampleList.gen.ts");
const groups = addTitleToGroups(groupProjects(projects));
const items = Object.entries(groups).map(([key, group]) => {
return {
text: group.title,
items: group.projects.map((project) => {
return {
text: project.title,
link: `/examples/${project.fullSlug}`,
author: project.config.author,
};
}),
};
});
const code = `// generated by dev-scripts/examples/genDocs.ts
export const EXAMPLES_LIST = ${JSON.stringify(items, undefined, 2)};`;
fs.writeFileSync(target, code);
}
// clean old files / dirs
fs.rmSync(COMPONENT_DIR, { recursive: true, force: true });
fs.readdirSync(EXAMPLES_PAGES_DIR, { withFileTypes: true }).forEach((file) => {
if (file.isDirectory()) {
fs.rmSync(path.join(EXAMPLES_PAGES_DIR, file.name), {
recursive: true,
force: true,
});
}
});
// generate new files
const projects = getExampleProjects().filter((p) => p.config?.docs === true);
const groups = addTitleToGroups(groupProjects(projects));
for (const group of Object.values(groups)) {
await generateMetaForExampleGroup(group);
for (const project of group.projects) {
await generateCodeForExample(project);
await generatePageForExample(project);
}
}
await generateExampleComponents(projects);
await generateExampleList(projects);