-
Notifications
You must be signed in to change notification settings - Fork 1
Fix highlight.js loading error and enhance visual elements #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0xrinegade
wants to merge
1
commit into
main
Choose a base branch
from
temp-branch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| ``` | ||
|
|
||
| ### 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>`; | ||
|
||
|
|
||
| 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 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
loadScriptfunction showing how to call it for each library (e.g.,loadScript(primaryUrl, fallbackUrl, initializeApp);) so integrators know how to actually use this loader.