-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample-processor.js
More file actions
190 lines (161 loc) · 5.27 KB
/
example-processor.js
File metadata and controls
190 lines (161 loc) · 5.27 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
/**
* Example Processor
*
* Converts an example project directory into a markdown file.
* Reads configuration from transformation-config/skip-patterns.yaml
*/
import fs from 'fs';
import path from 'path';
import yaml from 'js-yaml';
import { composePlugins, ignoreLinePlugin, ignoreFilePlugin, ignoreBlockPlugin } from '../plugins/index.js';
import { REPO_URL } from './constants.js';
/**
* Load skip patterns from YAML config
*/
function loadSkipPatterns(configPath) {
const content = fs.readFileSync(configPath, 'utf8');
const config = yaml.load(content);
return {
global: {
includes: config.global?.includes || [],
regex: (config.global?.regex || []).map(pattern => new RegExp(pattern)),
},
examples: config.examples || {},
};
}
/**
* Check if a file should be skipped based on patterns
*/
function shouldSkip(filePath, skipPatterns) {
// Check includes patterns (substring matching)
if (skipPatterns.includes.some(pattern => filePath.includes(pattern))) {
return true;
}
// Check regex patterns
if (skipPatterns.regex.some(regex => regex.test(filePath))) {
return true;
}
return false;
}
/**
* Merge global and example-specific skip patterns
*/
function mergeSkipPatterns(globalPatterns, examplePatterns = {}) {
return {
includes: [
...globalPatterns.includes,
...(examplePatterns.includes || []),
],
regex: [
...globalPatterns.regex,
...(examplePatterns.regex || []).map(p => new RegExp(p)),
],
};
}
/**
* Recursively collect all files in a directory
*/
function collectFiles(dirPath, baseDir, skipPatterns) {
const files = [];
const entries = fs.readdirSync(dirPath);
for (const entry of entries) {
const fullPath = path.join(dirPath, entry);
const relativePath = path.relative(baseDir, fullPath);
if (shouldSkip(relativePath, skipPatterns)) {
continue;
}
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
files.push(...collectFiles(fullPath, baseDir, skipPatterns));
} else {
files.push({ fullPath, relativePath });
}
}
return files;
}
/**
* Convert file content to markdown code block
*/
function fileToMarkdown(relativePath, content, extension, plugins = []) {
const context = { relativePath, extension };
// Apply plugins
const transformedContent = plugins.length > 0
? composePlugins(plugins)(content, context)
: content;
// Skip if empty after transformation
if (!transformedContent || transformedContent.trim() === '') {
return null;
}
let markdown = `## ${relativePath}\n\n`;
if (extension === 'md') {
markdown += transformedContent;
} else {
markdown += `\`\`\`${extension}\n`;
markdown += transformedContent;
markdown += '\n```\n';
}
markdown += '\n---\n\n';
return markdown;
}
/**
* Build markdown header for example
*/
function buildHeader(displayName, repoUrl, examplePath) {
let header = `# PostHog ${displayName} Example Project\n\n`;
header += `Repository: ${repoUrl}\n`;
header += `Path: ${examplePath}\n`;
header += '\n---\n\n';
return header;
}
/**
* Process an example project into markdown
*
* @param {Object} options
* @param {string} options.examplePath - Path to example directory (relative to repo root)
* @param {string} options.displayName - Human-readable name
* @param {string} options.id - Example identifier
* @param {string} options.repoRoot - Path to repository root
* @param {Object} options.skipPatterns - Merged skip patterns
* @param {Array} options.plugins - Content transformation plugins
* @returns {string} Generated markdown content
*/
function processExample({ examplePath, displayName, id, repoRoot, skipPatterns, plugins = [] }) {
const absolutePath = path.join(repoRoot, examplePath);
const repoUrl = REPO_URL;
if (!fs.existsSync(absolutePath)) {
throw new Error(`Example directory not found: ${absolutePath}`);
}
// Collect files
const files = collectFiles(absolutePath, absolutePath, skipPatterns);
// Sort: README.md first, then alphabetical
files.sort((a, b) => {
if (a.relativePath === 'README.md') return -1;
if (b.relativePath === 'README.md') return 1;
return a.relativePath.localeCompare(b.relativePath);
});
// Build markdown
let markdown = buildHeader(displayName, repoUrl, examplePath);
for (const file of files) {
try {
const content = fs.readFileSync(file.fullPath, 'utf8');
const extension = path.extname(file.fullPath).slice(1) || '';
const fileMarkdown = fileToMarkdown(file.relativePath, content, extension, plugins);
if (fileMarkdown !== null) {
markdown += fileMarkdown;
}
} catch (e) {
console.error(`[ERROR] Failed to process ${file.relativePath}:`, e.message);
}
}
return markdown;
}
/**
* Default plugins applied to all examples
*/
const defaultPlugins = [ignoreFilePlugin, ignoreBlockPlugin, ignoreLinePlugin];
export {
loadSkipPatterns,
mergeSkipPatterns,
processExample,
defaultPlugins,
};