-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathhljs.js
More file actions
58 lines (51 loc) · 1.9 KB
/
Copy pathhljs.js
File metadata and controls
58 lines (51 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const theme = localStorage.getItem('theme') || (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)')?.matches ? 'dark' : 'light');
const hlTheme = theme === 'light' ? 'vs' : 'vs2015';
const scripts = [
{ type: 'style', url: `third-party/${hlTheme}.min.css` },
{ type: 'script', url: 'third-party/highlight.min.js' },
];
async function loadResource(resource) {
return new Promise((resolve) => {
let element;
if (resource.type === 'script') {
element = document.createElement('script');
element.src = resource.url;
} else {
element = document.createElement('link');
element.rel = 'stylesheet';
element.id = 'hljs-theme';
element.href = resource.url;
}
// Add timeout to avoid hanging if resource never loads
const timeout = setTimeout(() => {
console.warn(`Resource load timeout: ${resource.url}`);
resolve();
}, 5000);
// Handle successful load
element.onload = () => {
clearTimeout(timeout);
resolve();
};
// Handle loading error
element.onerror = () => {
clearTimeout(timeout);
console.warn(`Failed to load resource: ${resource.url}`);
resolve(); // Continue despite errors
};
document.body.appendChild(element);
});
}
async function initializeApp() {
// Load all resources in parallel
const loadPromises = scripts.map(resource => loadResource(resource));
await Promise.all(loadPromises);
// Dispatch event to notify app.js that HLJS is ready
try {
window.dispatchEvent(new Event('hljs-loaded'));
} catch (error) {
console.error('Error dispatching hljs-loaded event:', error);
}
}
initializeApp().catch(error => {
console.error('Unexpected error in HLJS initialization:', error);
});