-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathgenerate-mini-docs.ts
More file actions
300 lines (257 loc) · 9.41 KB
/
generate-mini-docs.ts
File metadata and controls
300 lines (257 loc) · 9.41 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* LayerChart Mini Docs Generator
*
* This script generates mini-docs component files for all components in the layerchart library.
* It extracts API properties from the generated/api/*.json files.
* It extracts metadata from the docs/src/content/*.md files.
* It extracts 1st usage example from the docs/src/examples/components/*.svelte files.
* It trims the code to remove module exports and data export statement.
* It applies the appropriate indentation to the code to preserve the indentation of the code in mini-docs.
* It generates the mini-docs content and saves it as a svelte file in docs/src/generated/mini-docs/*.svelte.
* Once the components imported to the Layerchart components, you can hover over the component
* in the IDE to see mini docs for each component.
*
* @description
* Scans all .md files in docs/src/content and extracts:
* - Component name
* - Component URL
* - Description
* - Category
* - Layers
* - Related
* - Usage example
*
*
* @output
* Generates individual ts files in generated/mini-docs/ for each component
*
* @usage
* pnpm extract:mini-docs
*
*/
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { toTitleCase } from '@layerstack/utils';
import type { ComponentAPI } from '../src/lib/api-types.js';
interface MiniDocsMetadata {
componentName: string;
componentUrl: string;
description: string;
category: string;
layers: string[];
related: string[];
exampleName: string;
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const MARKDOWN_DIR = path.resolve(__dirname, '../src/content');
const API_DIR = path.resolve(__dirname, '../src/generated/api');
const EXAMPLES_DIR = path.resolve(__dirname, '../src/examples');
const CATEGORIES_DIR = path.resolve(__dirname, '../src/content/components');
const OUTPUT_DIR = path.resolve(__dirname, '../src/generated/mini-docs');
// Note: Identing of template is needed to preserve indentation of the code content.
// See https://www.youtube.com/watch?v=sY1Hqi5TENA&lc=UgyOIe4CX9hMxIywKEV4AaABAg
const miniDocsTemplate = `
<!--
@component
## <COMPONENT_NAME>
<DESCRIPTION>
<CATEGORY>
<LAYERS>
Full Documentation: [<COMPONENT_NAME>](<COMPONENT_URL>)
<API>
<RELATED>
<EXAMPLE>
-->
`;
/**
* Get all files with a specific extension in a directory (recursively), supporting files to ignore
*/
export function getFilesOfType(dir: string, ext: string, ignore: string[] = []): string[] {
const files: string[] = [];
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
// Recursively scan subdirectories
files.push(...getFilesOfType(fullPath, ext, ignore));
} else if (entry.isFile() && entry.name.endsWith(ext)) {
// Check if file should be ignored
const shouldIgnore = ignore.some((i) => fullPath.includes(i));
if (!shouldIgnore) {
files.push(fullPath);
}
}
}
return files;
}
/**
* Extract component name, component URL, description, category, layers, related and usage example metadata from a markdown file
*/
function extractMiniDocsMetadata(filePath: string): MiniDocsMetadata {
const content = fs.readFileSync(filePath, 'utf-8');
const componentName = path.basename(filePath, '.md');
const componentUrl = `/docs/components/${componentName}`;
// Extract the description, categories, layers, and related metadatafrom the markdown file
const descriptionMatch = content.match(/^description:\s*(.*?)\s*$/ms);
const categoryMatch = content.match(/^category:\s*(.*?)\s*$/ms);
const layersMatch = content.match(/^layers:\s*\[(.*?)\]\s*$/ms);
const relatedMatch = content.match(/^related:\s*\[(.*?)\]\s*$/ms);
const exampleNameMatch = content.match(/^:example\{\s*name=["|'](.*?)["'].*\}$/ms);
const description = descriptionMatch?.[1]?.trim() || '';
const category = toTitleCase(categoryMatch?.[1]?.trim() || '');
const layers = layersMatch?.[1]?.split(',').map((l) => l.trim()) || [];
const related = relatedMatch?.[1]?.split(',').map((r) => r.trim()) || [];
const exampleName = exampleNameMatch?.[1]?.trim() || '';
return { componentName, componentUrl, description, category, layers, related, exampleName };
}
/**
* Trim code to remove module exports and data export statement
* Note indentation included because it is required to preserve the indentation of the code in mini-docs
*/
function trimCode(code: string, indentLevel: number = 1): string {
return code
.replace(/<script\s+module>[\s\S]*?<\/script>/g, '') /* remove module exports section */
.replace(
/\n+\s*export \{ data \};\s*\n+\s*<\/script>/gm,
'\n</script>'
) /* remove data export statement */
.replaceAll(
/\n(?=.)/g,
indentLevel > 0 ? '\n' + '\t'.repeat(indentLevel) : '\n'
) /* apply indent only to lines with content (postiive lookahead)*/
.trim();
}
/**
* Get URL for component documentation page
*/
function getComponentUrl(component: string): string {
return `/docs/components/${component}`;
}
/**
* Main function
*/
function main() {
console.warn(
'WARNING: This script assumes API files have already been generated by the generate-component-api.ts script.'
);
console.log('\n\nExtracting component metadata from markdown files...');
const mdFiles = getFilesOfType(MARKDOWN_DIR, '.md', ['async', 'promise', 'source-plugin']);
console.log(`\nFound ${mdFiles.length} markdown files`);
const mdMetadata: MiniDocsMetadata[] = [];
for (const filePath of mdFiles) {
const mdName = path.basename(filePath, '.md');
console.log(`Processing ${mdName}...`);
const metadata = extractMiniDocsMetadata(filePath);
mdMetadata.push(metadata);
}
// Sort by component name
mdMetadata.sort((a, b) => a.componentName.localeCompare(b.componentName));
console.log('\nLoading corresponding component APIs from generated JSON files...');
const apiFiles = getFilesOfType(API_DIR, '.json');
console.log(`Found ${apiFiles.length} API files\n`);
const apis: ComponentAPI[] = [];
for (const filePath of apiFiles) {
const componentName = path.basename(filePath, '.json');
if (componentName === 'index') continue;
console.log(`Loading API for ${componentName}...`);
try {
const apiContent = fs.readFileSync(filePath, 'utf-8');
const api: ComponentAPI = JSON.parse(apiContent);
apis.push(api);
console.log(` ✓ Loaded ${api.properties.length} properties`);
} catch (error) {
console.log(` ⚠ Error loading API for ${componentName}:`, error);
}
}
// Create output directory if it doesn't exist
if (!fs.existsSync(OUTPUT_DIR)) {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
}
// Write individual mini-docs files
console.log(`\nWriting individual mini-docs files...`);
for (const md of mdMetadata) {
const miniDocsFile = path.join(OUTPUT_DIR, `${md.componentName}.svelte`);
// Find matching API for the component
const api = apis.find((a) => a.component === md.componentName);
if (!api) {
console.log(` ⚠ No API found for ${md.componentName}`);
continue;
}
// Get content of example file
console.log(` ⏳ Getting example content for example ${md.componentName}: ${md.exampleName}`);
let exampleContent = '';
if (md.exampleName) {
const exampleFile = path.join(
EXAMPLES_DIR,
'components',
md.componentName,
`${md.exampleName}.svelte`
);
try {
exampleContent = fs.readFileSync(exampleFile, 'utf-8');
} catch (error) {
console.log(` ⚠ No example file found for ${md.componentName}: ${error}`);
}
exampleContent = trimCode(exampleContent);
}
// Generate mini-docs content
const miniDocsContent = miniDocsTemplate
.replaceAll('<COMPONENT_NAME>', md.componentName)
.replaceAll('<COMPONENT_URL>', md.componentUrl)
.replace('<DESCRIPTION>', md.description ? '## Description\n\n\t' + md.description : '')
.replace(
'<CATEGORY>',
md.category
? `## Category\n\n\t[[${md.category}](${CATEGORIES_DIR}/${md.category}.md)]`
: ''
)
.replace(
'<LAYERS>',
md.layers
.map((l, i) => {
const h2 = i === 0 ? '## Layers\n\n\t' : '';
return `${h2}[${l === 'html' || l === 'svg' ? l.toUpperCase() : toTitleCase(l)}](${getComponentUrl(l)})`;
})
.join(', ')
)
.replace(
'<API>',
api.properties
.map((p, i) => {
const h2 = i === 0 ? '## API Properties\n\n\t' : '';
return `${h2}* ${p.name}:<${p.type}>${p.default ? `=${p.default}` : ''} - ${p.description} ${p.required ? ' (REQUIRED)' : ''}`;
})
.join('\n\t')
)
.replace(
'<RELATED>',
md.related
.map((r, i) => {
const h2 = i === 0 ? '## Related\n\n\t' : '';
return `${h2}[${r}](${getComponentUrl(r)})`;
})
.join('\n\t')
)
.replace(
'<EXAMPLE>',
exampleContent ? '@example\n\t```svelte\n\t' + exampleContent + '\n\t```' : ''
);
fs.writeFileSync(miniDocsFile, miniDocsContent);
console.log(` ✓ Generated mini-docs for ${md.componentName}`);
}
// // Write index file with list of all mini-docs components
// const indexFile = path.join(OUTPUT_DIR, 'index.json');
// const indexOutput = {
// generatedAt: new Date().toISOString(),
// components: mdMetadata.map((md) => ({
// component: md.componentName,
// file: `${md.componentName}.svelte`
// }))
// };
// fs.writeFileSync(indexFile, JSON.stringify(indexOutput, null, 2));
console.log(`\n✅ Generated ${mdMetadata.length} component mini docs files in ${OUTPUT_DIR}`);
// console.log(`✅ Generated index file: ${indexFile}`);
}
main();