filetree-pro
Version:
A powerful file tree generator for VS Code and Cursor. Generate beautiful file trees in multiple formats with smart exclusions and custom configurations.
98 lines (82 loc) • 2.5 kB
text/typescript
/**
* Markdown formatter for file trees
* Generates clean, readable Markdown output
*
* @module formatters
* @since 0.2.0
*/
import * as path from 'path';
import { FileTreeItem } from '../types';
import { getFileTypeInfo } from '../utils/fileUtils';
import { FormatOptions, FormatResult, TreeFormatter } from './treeFormatter.interface';
/**
* Formats file trees as Markdown with code blocks
* Perfect for documentation and README files
*/
export class MarkdownFormatter implements TreeFormatter {
getName(): string {
return 'Markdown';
}
getExtension(): string {
return 'md';
}
getLanguageId(): string {
return 'markdown';
}
async format(items: FileTreeItem[], options: FormatOptions): Promise<FormatResult> {
const lines: string[] = [];
// Add header
lines.push(`# File Tree: ${path.basename(options.rootPath)}`);
lines.push('');
lines.push(`**Generated:** ${new Date().toLocaleString()}`);
lines.push(`**Root Path:** \`${options.rootPath}\``);
lines.push('');
lines.push('```');
// Generate tree structure
this.generateTreeLines(items, '', lines, 0, options);
lines.push('```');
lines.push('');
lines.push('---');
lines.push('*Generated by FileTree Pro Extension*');
return {
content: lines.join('\n'),
extension: this.getExtension(),
languageId: this.getLanguageId(),
};
}
/**
* Recursively generate tree lines for items
*/
private generateTreeLines(
items: FileTreeItem[],
prefix: string,
lines: string[],
currentDepth: number,
options: FormatOptions
): void {
if (currentDepth >= options.maxDepth) {
return;
}
items.forEach((item, index) => {
const isLast = index === items.length - 1;
const connector = isLast ? '└── ' : '├── ';
const icon = options.showIcons ? this.getIcon(item) : '';
lines.push(`${prefix}${connector}${icon}${item.name}`);
// Process children recursively
if (item.children && item.children.length > 0) {
const newPrefix = prefix + (isLast ? ' ' : '│ ');
this.generateTreeLines(item.children, newPrefix, lines, currentDepth + 1, options);
}
});
}
/**
* Get icon for file/folder
*/
private getIcon(item: FileTreeItem): string {
if (item.type === 'folder') {
return '📁 ';
}
const typeInfo = getFileTypeInfo(item.name);
return typeInfo.icon ? `${typeInfo.icon} ` : '📄 ';
}
}