Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions fixed_hljs_documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# 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

```javascript
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);
}
Copy link

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a short example immediately after the loadScript function showing how to call it for each library (e.g., loadScript(primaryUrl, fallbackUrl, initializeApp);) so integrators know how to actually use this loader.

Suggested change
}
}
// Example usage of loadScript
loadScript(
'https://cdn.jsdelivr.net/npm/[email protected]/lib/highlight.min.js', // Primary URL
'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js', // Fallback URL
function() {
console.log('highlight.js loaded successfully');
// Initialize the app or perform actions requiring highlight.js
if (typeof hljs !== 'undefined') {
hljs.highlightAll();
} else {
console.error('highlight.js is not available');
}
}
);

Copilot uses AI. Check for mistakes.
```

### 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

```javascript
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

```javascript
// 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

```javascript
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>`;
Copy link

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Suggest calling hljs.highlightElement (used earlier in this doc) when a specific language is known, as highlightAuto can misidentify languages and may be slower on large blocks.

Copilot uses AI. Check for mistakes.

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
Loading