Skip to content

Latest commit

 

History

History
148 lines (123 loc) · 5.46 KB

File metadata and controls

148 lines (123 loc) · 5.46 KB

Highlight.js and Library Loading Fix Documentation

Issue Summary

The book website was experiencing a critical error where highlight.js was not being properly loaded, resulting in the error message "hljs is not defined" when attempting to load chapters. This prevented proper chapter loading and syntax highlighting, particularly for Rust code blocks.

Root Causes

  1. Library Loading Order: The highlight.js library was not fully loaded before the code attempted to use it
  2. CDN Reliability: Single CDN source without fallbacks created a single point of failure
  3. Initialization Timing: The code was not waiting for all required libraries to be fully loaded before initialization
  4. Error Handling: Insufficient error handling around highlight.js operations

Implemented Solutions

1. Robust Script Loading with Fallbacks

  • Implemented a custom script loader function that attempts to load from primary CDN source first
  • Added fallback CDN sources for each library to ensure availability
  • Added proper error handling and logging for script loading failures
function loadScript(src, fallbackSrc, callback) {
    var script = document.createElement('script');
    script.src = src;
    script.onload = callback;
    script.onerror = function() {
        console.warn('Failed to load script from primary source:', src);
        if (fallbackSrc) {
            console.log('Trying fallback source:', fallbackSrc);
            var fallbackScript = document.createElement('script');
            fallbackScript.src = fallbackSrc;
            fallbackScript.onload = callback;
            fallbackScript.onerror = function() {
                console.error('Failed to load script from fallback source:', fallbackSrc);
            };
            document.head.appendChild(fallbackScript);
        }
    };
    document.head.appendChild(script);
}

2. Improved Initialization Sequence

  • Moved initialization code inside DOMContentLoaded event
  • Added a library check function that verifies all required libraries are loaded
  • Implemented a polling mechanism to wait for libraries to load before initializing the app
document.addEventListener('DOMContentLoaded', function() {
    // Check if libraries are loaded
    var librariesLoaded = function() {
        return typeof hljs !== 'undefined' && 
               typeof marked !== 'undefined' && 
               typeof DOMPurify !== 'undefined';
    };

    // Initialize app when libraries are loaded
    var initializeApp = function() {
        if (librariesLoaded()) {
            console.log('All libraries loaded, initializing app');
            initApp();
        } else {
            console.log('Waiting for libraries to load...');
            setTimeout(initializeApp, 100);
        }
    };

    // Start checking for libraries
    initializeApp();
});

3. Enhanced Error Handling

  • Added robust error handling around all highlight.js operations
  • Implemented fallbacks for when syntax highlighting fails
  • Added explicit checks to verify highlight.js is available before use
// Highlight code blocks
if (typeof hljs !== 'undefined') {
    document.querySelectorAll('pre code').forEach((block) => {
        try {
            hljs.highlightElement(block);
        } catch (e) {
            console.error('Error highlighting element:', e);
        }
    });
} else {
    console.warn('highlight.js not available for syntax highlighting');
}

4. Improved Code Block Rendering

  • Enhanced the code block renderer to handle different types of code blocks
  • Added proper type checking for code content
  • Improved detection of ASCII diagrams vs. code blocks
renderer.code = function(code, language) {
    // Ensure code is a string
    code = typeof code === 'string' ? code : String(code);
    
    // Check if this is an ASCII diagram
    const isAsciiDiagram = code.includes('+-') || code.includes('|') || code.includes('--') || code.includes('==') || code.includes('[]');
    
    if (isAsciiDiagram) {
        return `<div class="ascii-diagram">${code}</div>`;
    }
    
    // Determine if this is a shell command
    const isShellCommand = language === 'bash' || language === 'sh' || language === 'shell';
    
    // Create code block with buttons
    let html = '<pre><div class="code-buttons">';
    html += '<button class="copy-button" onclick="copyCode(this)">Copy</button>';
    
    if (isShellCommand) {
        html += '<button class="execute-button" onclick="executeCode(this)">Execute</button>';
    }
    
    html += '</div><code';
    
    if (language) {
        html += ` class="language-${language}"`;
    }
    
    html += `>${hljs.highlightAuto(code, language ? [language] : undefined).value}</code></pre>`;
    
    return html;
};

5. Visual Enhancements

  • Improved styling for code blocks, tables, and ASCII diagrams
  • Enhanced field tag styling in bullet lists
  • Added better visual feedback for copy and execute buttons

Testing and Verification

  • Verified that all chapters load correctly without errors
  • Confirmed that Rust syntax highlighting works properly
  • Tested sidebar expansion and sub-chapter navigation
  • Verified search functionality across all chapters
  • Ensured copy and execute buttons work on code blocks
  • Tested responsive design on various screen sizes

Additional Improvements

  • Enhanced table styling for better readability
  • Improved ASCII diagram presentation
  • Added field tag styling for better visual hierarchy
  • Improved keyboard navigation and accessibility