mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 05:28:59 +01:00 
			
		
		
		
	use highlight.js in code_handlers where possible
This commit is contained in:
		
							parent
							
								
									b7d5d926f7
								
							
						
					
					
						commit
						6e8ab373d8
					
				@ -2,6 +2,9 @@
 | 
			
		||||
 * Helper functions for processing code notes, including language detection and structure extraction
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
// Import highlight.js dynamically when needed
 | 
			
		||||
let hljs: any = null;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Attempt to detect the programming language from code content or note attributes
 | 
			
		||||
 */
 | 
			
		||||
@ -46,6 +49,51 @@ export function detectLanguage(content: string, mime: string): string {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // For performance, limit the amount of code we pass to highlight.js
 | 
			
		||||
    // Use a maximum of ~10KB of code for detection
 | 
			
		||||
    const MAX_SAMPLE_SIZE = 10240;
 | 
			
		||||
    const codeSample = content.length > MAX_SAMPLE_SIZE
 | 
			
		||||
        ? content.substring(0, MAX_SAMPLE_SIZE)
 | 
			
		||||
        : content;
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
        // Browser environment
 | 
			
		||||
        if (typeof window !== 'undefined' && window.hljs) {
 | 
			
		||||
            const result = window.hljs.highlightAuto(codeSample);
 | 
			
		||||
            if (result && result.language) {
 | 
			
		||||
                return result.language;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        // Node.js environment
 | 
			
		||||
        else if (typeof process !== 'undefined' && process.versions && process.versions.node) {
 | 
			
		||||
            // Only load highlight.js if we haven't already
 | 
			
		||||
            if (!hljs) {
 | 
			
		||||
                try {
 | 
			
		||||
                    // We need to dynamically require highlight.js
 | 
			
		||||
                    // Using a try/catch since it might not be available
 | 
			
		||||
                    hljs = require('@highlightjs/cdn-assets/highlight.min.js');
 | 
			
		||||
                } catch (e) {
 | 
			
		||||
                    try {
 | 
			
		||||
                        // Try alternative path
 | 
			
		||||
                        hljs = require('highlight.js');
 | 
			
		||||
                    } catch (e2) {
 | 
			
		||||
                        console.debug("highlight.js not available in this environment, falling back to regex detection");
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (hljs && typeof hljs.highlightAuto === 'function') {
 | 
			
		||||
                const result = hljs.highlightAuto(codeSample);
 | 
			
		||||
                if (result && result.language) {
 | 
			
		||||
                    return result.language;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
        console.debug("Error using highlight.js for language detection:", e);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 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');
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user