-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-docs.js
More file actions
224 lines (187 loc) · 5.85 KB
/
generate-docs.js
File metadata and controls
224 lines (187 loc) · 5.85 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
#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import {fileURLToPath} from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/**
Parse CSS file and extract function documentation.
*/
function parseCSSFunctions(cssContent) {
const functions = [];
const functionRegex = /@function\s+(--[\w-]+)\s*\(([\s\S]*?)\)\s*{/g;
const commentRegex = /\/\*\*([\s\S]*?)\*\//g;
let match;
const comments = [];
// Extract all comments with their positions
while ((match = commentRegex.exec(cssContent)) !== null) {
comments.push({
content: match[1],
index: match.index,
endIndex: match.index + match[0].length,
});
}
// Match functions with their preceding comments
functionRegex.lastIndex = 0;
while ((match = functionRegex.exec(cssContent)) !== null) {
const functionName = match[1];
const parametersString = match[2];
const functionIndex = match.index;
// Calculate line number
const lineNumber = cssContent.slice(0, functionIndex).split('\n').length;
// Find the comment that immediately precedes this function
let functionComment = null;
for (let i = comments.length - 1; i >= 0; i--) {
if (comments[i].endIndex < functionIndex) {
const between = cssContent.slice(comments[i].endIndex, functionIndex).trim();
if (between === '') {
functionComment = comments[i].content;
break;
}
}
}
if (functionComment) {
const documentation = parseComment(functionComment);
functions.push({
name: functionName,
parameters: parseParameters(parametersString),
lineNumber,
...documentation,
});
}
}
return functions;
}
/**
Parse JSDoc-style comment.
*/
function parseComment(comment) {
const lines = comment.split('\n').map(line => line.trim());
const documentation = {
description: '',
params: [],
returns: null,
example: null,
};
let currentSection = 'description';
for (const line of lines) {
if (line.startsWith('@param')) {
const parameterMatch = line.match(/@param\s+{([^}]+)}\s+(--[\w-]+)\s+-?\s*(.*)$/);
if (parameterMatch) {
documentation.params.push({
type: parameterMatch[1],
name: parameterMatch[2],
description: parameterMatch[3],
});
}
currentSection = 'params';
} else if (line.startsWith('@returns')) {
const returnsMatch = line.match(/@returns\s+{([^}]+)}\s+(.*)$/);
if (returnsMatch) {
documentation.returns = {
type: returnsMatch[1],
description: returnsMatch[2],
};
}
currentSection = 'returns';
} else if (line.startsWith('@example')) {
documentation.example = line.replace('@example', '').trim();
currentSection = 'example';
} else if (currentSection === 'description' && line && !line.startsWith('@')) {
documentation.description += (documentation.description ? '\n' : '') + line;
} else if (currentSection === 'example' && line && !line.startsWith('@')) {
documentation.example += '\n' + line;
}
}
return documentation;
}
/**
Parse function parameters.
*/
function parseParameters(parametersString) {
if (!parametersString.trim()) {
return [];
}
const parameters = [];
const parts = parametersString.split(',');
for (const part of parts) {
const trimmed = part.trim();
const colonIndex = trimmed.indexOf(':');
if (colonIndex > -1) {
parameters.push({
name: trimmed.slice(0, colonIndex).trim(),
defaultValue: trimmed.slice(colonIndex + 1).trim(),
});
} else {
parameters.push({
name: trimmed,
defaultValue: null,
});
}
}
return parameters;
}
/**
Generate Markdown documentation.
*/
function generateMarkdown(functions) {
let markdown = '# CSS Extras Function Reference\n\n';
markdown += 'Complete reference for all CSS custom functions in css-extras.\n\n';
markdown += `**Total functions:** ${functions.length}\n\n`;
markdown += '---\n\n';
// Generate function documentation (no categorization)
for (const cssFunction of functions) {
const sourceUrl = `../index.css#L${cssFunction.lineNumber}`;
markdown += `## \`${cssFunction.name}()\` [↗︎](${sourceUrl})\n\n`;
if (cssFunction.description) {
markdown += `${cssFunction.description}\n\n`;
}
if (cssFunction.params.length > 0) {
markdown += '### Parameters\n\n';
for (const parameter of cssFunction.params) {
const defaultString = cssFunction.parameters.find(p => p.name === parameter.name)?.defaultValue;
markdown += `- **\`${parameter.name}\`** (\`${parameter.type}\`): ${parameter.description}`;
if (defaultString) {
markdown += ` Default: \`${defaultString}\``;
}
markdown += '\n';
}
markdown += '\n';
}
if (cssFunction.returns) {
markdown += '### Returns\n\n';
markdown += `\`${cssFunction.returns.type}\`: ${cssFunction.returns.description}\n\n`;
}
if (cssFunction.example) {
markdown += '### Example\n\n';
markdown += '```css\n';
markdown += cssFunction.example + '\n';
markdown += '```\n\n';
}
markdown += '---\n\n';
}
return markdown;
}
// Main execution
async function main() {
const cssPath = path.join(__dirname, 'index.css');
const cssContent = fs.readFileSync(cssPath, 'utf8');
console.log('📝 Parsing CSS functions...');
const functions = parseCSSFunctions(cssContent);
console.log(`✓ Found ${functions.length} functions`);
// Generate Markdown only
console.log('📄 Generating Markdown documentation...');
const markdown = generateMarkdown(functions);
// Create docs directory if it doesn't exist
const documentationDirectory = path.join(__dirname, 'docs');
if (!fs.existsSync(documentationDirectory)) {
fs.mkdirSync(documentationDirectory);
}
fs.writeFileSync(path.join(documentationDirectory, 'functions.md'), markdown);
console.log('✓ Written to docs/functions.md');
console.log('✅ Documentation generated successfully!');
}
await main().catch(error => {
console.error('Error generating documentation:', error);
process.exit(1);
});