@paulohenriquevn/m2js
Version:
Transform TypeScript/JavaScript code into LLM-friendly Markdown summaries + Smart Dead Code Detection + Graph-Deep Diff Analysis. Extract exported functions, classes, and JSDoc comments for better AI context with 60%+ token reduction. Intelligent dead cod
181 lines • 6.07 kB
JavaScript
;
/* eslint-disable max-lines-per-function */
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MermaidEnhancer = void 0;
const path_1 = __importDefault(require("path"));
const neutral_1 = require("./themes/neutral");
/**
* Enhanced Mermaid diagram generator with styling and themes
*/
class MermaidEnhancer {
constructor(theme = 'neutral') {
this.config = this.getThemeConfig(theme);
}
/**
* Generate enhanced dependency graph with styling
*/
generateEnhancedDependencyDiagram(graph, options) {
const sections = [];
// Add title if provided
if (options.title) {
sections.push(`## ${options.title}`);
}
else {
sections.push('## Enhanced Dependency Map');
}
// Add description
if (options.description) {
sections.push(`*${options.description}*`);
sections.push('');
}
// Start Mermaid block with configuration
sections.push('```mermaid');
sections.push(this.generateMermaidConfig());
sections.push('graph TD');
// Generate styled nodes
const { nodeMap, nodeDefinitions } = this.generateStyledNodes(graph);
sections.push(...nodeDefinitions);
// Generate edges
const edges = this.generateStyledEdges(graph, nodeMap, options.includeExternal);
sections.push(...edges);
// Add CSS classes
sections.push('');
sections.push(this.generateCSSClasses());
sections.push('```');
return {
content: sections.join('\n'),
metadata: {
type: 'dependency',
theme: options.theme,
nodeCount: graph.nodes.length,
edgeCount: graph.edges.length,
generatedAt: new Date(),
},
};
}
/**
* Generate Mermaid configuration header
*/
generateMermaidConfig() {
return `%%{init: ${JSON.stringify({
theme: this.config.theme,
flowchart: this.config.flowchart,
themeVariables: this.config.themeVariables,
})}}%%`;
}
/**
* Generate styled nodes with classification
*/
generateStyledNodes(graph) {
const nodeMap = new Map();
const nodeDefinitions = [];
graph.nodes.forEach((file, index) => {
const cleanName = path_1.default
.basename(file, path_1.default.extname(file))
.replace(/[^a-zA-Z0-9]/g, '')
.substring(0, 10);
const nodeId = `${cleanName}${index}`;
nodeMap.set(file, nodeId);
const fileName = path_1.default.basename(file);
const classification = this.classifyFile(fileName);
const icon = neutral_1.NEUTRAL_NODE_TYPES[classification]?.icon || 'FILE';
// Create styled node definition
nodeDefinitions.push(` ${nodeId}["${icon} ${fileName}"]:::${classification}`);
});
return { nodeMap, nodeDefinitions };
}
/**
* Generate styled edges
*/
generateStyledEdges(graph, nodeMap, includeExternal) {
const edges = [];
// Filter edges based on includeExternal option
const filteredEdges = graph.edges.filter(edge => includeExternal || !edge.isExternal);
filteredEdges.forEach(edge => {
const fromNode = nodeMap.get(edge.from);
const toNode = nodeMap.get(edge.to);
if (fromNode && toNode) {
// Different arrow styles for different import types
const arrowStyle = this.getArrowStyle(edge.importType);
edges.push(` ${fromNode} ${arrowStyle} ${toNode}`);
}
});
return edges;
}
/**
* Classify file type based on name patterns
*/
classifyFile(fileName) {
const name = fileName.toLowerCase();
if (name.includes('cli'))
return 'cli';
if (name.includes('parser'))
return 'parser';
if (name.includes('generator'))
return 'generator';
if (name.includes('analyzer'))
return 'analyzer';
if (name.includes('types'))
return 'types';
if (name.includes('util') || name.includes('helper'))
return 'utils';
if (name.includes('test') || name.includes('spec'))
return 'test';
return 'core';
}
/**
* Get arrow style based on import type
*/
getArrowStyle(importType) {
switch (importType) {
case 'default':
return '==>';
case 'named':
return '-->';
case 'namespace':
return '-.->';
case 'side-effect':
return '-.->';
default:
return '-->';
}
}
/**
* Generate CSS classes for styling
*/
generateCSSClasses() {
return (0, neutral_1.generateNeutralCSS)();
}
/**
* Get theme configuration
*/
getThemeConfig(theme) {
switch (theme) {
case 'neutral':
default:
return neutral_1.NEUTRAL_THEME;
// Add other themes later
}
}
/**
* Static method for backward compatibility
*/
static generateEnhanced(graph, options = {}) {
const enhancer = new MermaidEnhancer(options.theme || 'neutral');
const fullOptions = {
type: 'dependency',
theme: 'neutral',
format: 'markdown',
includeExternal: false,
interactive: false,
...options,
};
return enhancer.generateEnhancedDependencyDiagram(graph, fullOptions)
.content;
}
}
exports.MermaidEnhancer = MermaidEnhancer;
//# sourceMappingURL=mermaid-enhancer.js.map