-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocumentation.js
More file actions
225 lines (199 loc) · 6.3 KB
/
Copy pathdocumentation.js
File metadata and controls
225 lines (199 loc) · 6.3 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
/**
* Documentation Collector
*
* Analyzes documentation files: README, PLAN, CHANGELOG, etc.
* Extracted from drift-detect/collectors.js for shared use.
*
* @module lib/collectors/documentation
*/
'use strict';
const fs = require('fs');
const path = require('path');
const DEFAULT_OPTIONS = {
depth: 'thorough',
cwd: process.cwd()
};
/**
* Validate file path to prevent path traversal
* @param {string} filePath - Path to validate
* @param {string} basePath - Base directory
* @returns {boolean} True if path is safe
*/
function isPathSafe(filePath, basePath) {
const resolved = path.resolve(basePath, filePath);
return resolved.startsWith(path.resolve(basePath));
}
/**
* Safe file read with path validation
* @param {string} filePath - Path to read
* @param {string} basePath - Base directory for validation
* @returns {string|null} File contents or null
*/
function safeReadFile(filePath, basePath) {
const fullPath = path.resolve(basePath, filePath);
if (!isPathSafe(filePath, basePath)) {
return null;
}
try {
return fs.readFileSync(fullPath, 'utf8');
} catch {
return null;
}
}
/**
* Analyze a single markdown file
*/
function analyzeMarkdownFile(content, filePath) {
// ReDoS fix: bound the \s+ run after the ## marker; line-anchored (.+) cannot
// cross newlines so this matches the same headings as before.
const sectionMatches = content.match(/^##\s{1,1000}(.+)$/gm) || [];
const sections = sectionMatches.slice(0, 10).map(s => s.replace(/^##\s+/, ''));
const sectionLower = sections.map(s => s.toLowerCase()).join(' ');
return {
path: filePath,
sectionCount: sectionMatches.length,
sections,
hasInstallation: /install|setup|getting.started/i.test(sectionLower),
hasUsage: /usage|how.to|example/i.test(sectionLower),
hasApi: /api|reference|methods/i.test(sectionLower),
hasTesting: /test|spec|coverage/i.test(sectionLower),
codeBlocks: Math.floor((content.match(/```/g) || []).length / 2),
wordCount: content.split(/\s+/).length
};
}
/**
* Extract checkboxes from content
*/
function extractCheckboxes(result, content) {
const checked = (content.match(/^[-*]\s+\[x\]/gim) || []).length;
const unchecked = (content.match(/^[-*]\s+\[\s\]/gim) || []).length;
result.checkboxes.checked += checked;
result.checkboxes.unchecked += unchecked;
result.checkboxes.total += checked + unchecked;
}
/**
* Extract documented features
*/
function extractFeatures(result, content) {
// ReDoS fix: bound the \s+ run and the line-content quantifiers so the lazy
// (.+?) / optional trailing (.+) pair cannot backtrack polynomially. Using
// [^\n] is equivalent to . here (. never matches newline), and the bounds far
// exceed the 80-char feature cap applied below, so matches are unchanged.
const featurePattern = /^[-*]\s{1,100}\*{0,2}([^\n]{1,2000}?)\*{0,2}(?:\s{0,100}[-–]\s{0,100}([^\n]{1,2000}))?$/gm;
let match;
while ((match = featurePattern.exec(content)) !== null && result.features.length < 20) {
const feature = match[1].trim();
if (feature.length > 5 && feature.length < 80) {
result.features.push(feature);
}
}
result.features = [...new Set(result.features)].slice(0, 20);
}
/**
* Extract planned items from content
*/
function extractPlans(result, content) {
const planPatterns = [
/(?:TODO|FIXME|PLAN):\s*(.+)/gi,
/^##\s+(?:Roadmap|Future|Planned|Coming Soon)/gim
];
for (const pattern of planPatterns) {
let match;
while ((match = pattern.exec(content)) !== null && result.plans.length < 15) {
const plan = (match[1] || match[0]).slice(0, 100);
result.plans.push(plan);
}
}
}
/**
* Identify documentation gaps
*/
function identifyDocGaps(result) {
const readme = result.files['README.md'];
if (!readme) {
result.gaps.push({ type: 'missing', file: 'README.md', severity: 'high' });
} else {
if (!readme.hasInstallation) {
result.gaps.push({ type: 'missing-section', file: 'README.md', section: 'Installation', severity: 'medium' });
}
if (!readme.hasUsage) {
result.gaps.push({ type: 'missing-section', file: 'README.md', section: 'Usage', severity: 'medium' });
}
}
if (!result.files['CHANGELOG.md']) {
result.gaps.push({ type: 'missing', file: 'CHANGELOG.md', severity: 'low' });
}
}
/**
* Analyze documentation files
* @param {Object} options - Collection options
* @returns {Object} Documentation analysis
*/
function analyzeDocumentation(options = {}) {
const opts = { ...DEFAULT_OPTIONS, ...options };
const basePath = opts.cwd;
const result = {
summary: { fileCount: 0, totalWords: 0 },
files: {},
features: [],
plans: [],
checkboxes: { total: 0, checked: 0, unchecked: 0 },
gaps: []
};
const docFiles = [
'README.md',
'PLAN.md',
'CLAUDE.md',
'AGENTS.md',
'CONTRIBUTING.md',
'CHANGELOG.md',
'docs/README.md',
'docs/PLAN.md'
];
for (const file of docFiles) {
const content = safeReadFile(file, basePath);
if (content) {
const analysis = analyzeMarkdownFile(content, file);
result.files[file] = analysis;
result.summary.totalWords += analysis.wordCount;
extractCheckboxes(result, content);
extractFeatures(result, content);
extractPlans(result, content);
}
}
// Find additional markdown files if depth is thorough
if (opts.depth === 'thorough') {
const docsDir = path.join(basePath, 'docs');
if (fs.existsSync(docsDir)) {
try {
const additionalFiles = fs.readdirSync(docsDir)
.filter(f => f.endsWith('.md') && !docFiles.includes(`docs/${f}`));
for (const file of additionalFiles.slice(0, 5)) {
const filePath = `docs/${file}`;
const content = safeReadFile(filePath, basePath);
if (content) {
const analysis = analyzeMarkdownFile(content, filePath);
result.files[filePath] = analysis;
result.summary.totalWords += analysis.wordCount;
}
}
} catch {
// Ignore directory read errors
}
}
}
result.summary.fileCount = Object.keys(result.files).length;
identifyDocGaps(result);
return result;
}
module.exports = {
DEFAULT_OPTIONS,
analyzeDocumentation,
analyzeMarkdownFile,
safeReadFile,
isPathSafe,
extractCheckboxes,
extractFeatures,
extractPlans,
identifyDocGaps
};