import { AnalysisResult, CodeElement } from '../types/index.js';
export class MarkdownGenerator {
generateDocumentation(results: AnalysisResult[]): string {
const sections: string[] = [];
sections.push('# Codebase Documentation\n');
sections.push(this.generateSummary(results));
for (const result of results) {
sections.push(this.generateFileDocumentation(result));
}
return sections.join('\n\n');
}
private generateSummary(results: AnalysisResult[]): string {
const totalFiles = results.length;
const totalElements = results.reduce((sum, r) => sum + r.totalElements, 0);
const totalDocumented = results.reduce((sum, r) => sum + r.documentedElements, 0);
const avgCoverage = totalElements > 0 ? (totalDocumented / totalElements) * 100 : 0;
const lines: string[] = [
'## Summary',
'',
`- **Total Files Analyzed**: ${totalFiles}`,
`- **Total Code Elements**: ${totalElements}`,
`- **Documented Elements**: ${totalDocumented}`,
`- **Documentation Coverage**: ${avgCoverage.toFixed(2)}%`,
''
];
return lines.join('\n');
}
private generateFileDocumentation(result: AnalysisResult): string {
const lines: string[] = [
`## ${result.filePath}`,
'',
`**Language**: ${result.language} `,
`**Coverage**: ${result.documentationCoverage}%`,
''
];
// Group elements by type
const byType = this.groupByType(result.elements);
for (const [type, elements] of Object.entries(byType)) {
lines.push(`### ${this.capitalizeType(type)}`);
lines.push('');
for (const element of elements) {
lines.push(this.generateElementDoc(element));
}
}
return lines.join('\n');
}
private generateElementDoc(element: CodeElement): string {
const lines: string[] = [];
lines.push(`#### \`${element.name}\``);
lines.push('');
if (element.signature) {
lines.push('```' + (element.filePath.endsWith('.py') ? 'python' : 'typescript'));
lines.push(element.signature);
lines.push('```');
lines.push('');
}
if (element.hasDocumentation && element.documentation) {
lines.push(element.documentation);
} else {
lines.push('*No documentation available*');
}
lines.push('');
lines.push(`**Location**: Line ${element.line}`);
lines.push('');
return lines.join('\n');
}
private groupByType(elements: CodeElement[]): Record<string, CodeElement[]> {
const groups: Record<string, CodeElement[]> = {};
for (const element of elements) {
if (!groups[element.type]) {
groups[element.type] = [];
}
groups[element.type].push(element);
}
return groups;
}
private capitalizeType(type: string): string {
return type.charAt(0).toUpperCase() + type.slice(1) + 's';
}
}