/** * Helper functions for processing code notes, including language detection and structure extraction */ // Import highlight.js dynamically when needed let hljs: object | null = null; /** * Attempt to detect the programming language from code content or note attributes */ export function detectLanguage(content: string, mime: string): string { // First check MIME type for hints if (mime) { const mimeLower = mime.toLowerCase(); // Map of mime types to language names const mimeMap: {[key: string]: string} = { 'text/javascript': 'javascript', 'application/javascript': 'javascript', 'text/typescript': 'typescript', 'application/typescript': 'typescript', 'text/x-python': 'python', 'text/x-java': 'java', 'text/x-c': 'c', 'text/x-c++': 'cpp', 'text/x-csharp': 'csharp', 'text/x-go': 'go', 'text/x-ruby': 'ruby', 'text/x-php': 'php', 'text/x-rust': 'rust', 'text/x-swift': 'swift', 'text/x-kotlin': 'kotlin', 'text/x-scala': 'scala', 'text/x-perl': 'perl', 'text/x-lua': 'lua', 'text/x-r': 'r', 'text/x-dart': 'dart', 'text/html': 'html', 'text/css': 'css', 'application/json': 'json', 'application/xml': 'xml', 'text/markdown': 'markdown', 'text/yaml': 'yaml', 'text/x-sql': 'sql' }; if (mimeMap[mimeLower]) { return mimeMap[mimeLower]; } } // Fallback to regex-based detection if highlight.js is not available or fails // Check for common language patterns in the first few lines const firstLines = content.split('\n').slice(0, 10).join('\n'); // Simple heuristics for common languages if (firstLines.includes('') || firstLines.includes('')) return 'html'; if (firstLines.includes('function ') && firstLines.includes('var ') && firstLines.includes('const ')) return 'javascript'; if (firstLines.includes('interface ') && firstLines.includes('export class ')) return 'typescript'; if (firstLines.includes('@Component') || firstLines.includes('import { Component }')) return 'typescript'; // Default to 'text' if language can't be determined return 'text'; } /** * Extract structure from code to create a summary */ export function extractCodeStructure(content: string, language: string): string { // Avoid processing very large code files if (content.length > 100000) { return "Code content too large for structure extraction"; } let structure = ""; try { switch (language.toLowerCase()) { case 'javascript': case 'typescript': structure = extractJsStructure(content); break; case 'python': structure = extractPythonStructure(content); break; case 'java': case 'csharp': case 'cpp': structure = extractClassBasedStructure(content); break; case 'go': structure = extractGoStructure(content); break; case 'rust': structure = extractRustStructure(content); break; case 'html': structure = extractHtmlStructure(content); break; default: // For other languages, just return a summary of the file size and a few lines const lines = content.split('\n'); structure = `Code file with ${lines.length} lines.\n`; // Add first few non-empty lines that aren't comments const firstCodeLines = lines.filter(line => line.trim() !== '' && !line.trim().startsWith('//') && !line.trim().startsWith('#') && !line.trim().startsWith('*') && !line.trim().startsWith('