Commit a2a1924
Fix highlighter not loading in JS editor (#5335)
* Fix highlighter not loading in JS editor
The issue occurred because hljs.highlightElement was being called before
the highlight.js library finished loading. Since the script is loaded with
the defer attribute, it may not be available when the JS editor widget opens.
Added a check to verify that window.hljs exists and highlightElement is
available before attempting to use it. This prevents the TypeError and
allows the editor to work even if syntax highlighting hasn't loaded yet.
* Enhance JS Editor highlight.js compatibility and async loading
This commit improves the JS editor's syntax highlighting to address
issues with highlight.js loading asynchronously and version compatibility.
Key improvements:
1. Backward compatibility: Added fallback to highlightBlock() API for
older highlight.js versions (v9-10) while maintaining support for
the modern highlightElement() API (v11+)
2. Async loading handling: Implemented a retry mechanism that detects
when highlight.js hasn't loaded yet and automatically re-applies
highlighting once it becomes available (100ms retry interval)
3. Error resilience: Wrapped highlighting logic in try-catch to prevent
editor crashes from highlighting errors, with console warnings for
debugging
4. Independent error highlighting: Ensured syntax error highlighting
works independently of highlight.js availability
5. Code quality: Applied ESLint auto-fixes for consistent formatting
This ensures the JS editor works reliably across different highlight.js
versions and loading scenarios, with syntax highlighting enabled once
the library loads, without blocking editor functionality.
Fixes #5334
* Fix Prettier formatting issues in jseditor.js
* Improve highlight.js async loading with exponential backoff retry logic
* Refactor highlighter retry logic to use closure variable and exponential backoff
* Enhance hljs async loading with interval-based monitoring
- Replace limited retry mechanism with persistent interval checking
- Ensure syntax highlighting activates once hljs loads
- Add proper cleanup to prevent memory leaks
- Maintain support for both hljs v9-10 and v11+ APIs
- Fix issue where highlighting wouldn't enable if hljs loaded late
* Fix Prettier formatting issues
* Fix syntax highlighting initialization in JS Editor
- Add proper hljs availability check before highlighting
- Ensure highlightElement is called only when hljs is fully loaded
- Prevents 'hljs.highlightElement is not a function' error
- Syntax highlighting now works reliably when hljs loads asynchronously
Fixes #5335
* Fix Prettier formatting in jseditor.js
* Use local highlight.pack.js instead of CDN for JS Editor
- Switch from CDN highlight.js to local lib/codejar/highlight.pack.js
- Fixes syntax highlighting not loading in JS Editor
- As suggested by maintainer @walterbender
Fixes #5335
---------
Co-authored-by: Vanshika <vanshika@198625@gmail.com>1 parent e120bbb commit a2a1924
2 files changed
+86
-66
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
55 | | - | |
| 55 | + | |
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
104 | | - | |
| 104 | + | |
| 105 | + | |
105 | 106 | | |
106 | 107 | | |
107 | 108 | | |
| |||
226 | 227 | | |
227 | 228 | | |
228 | 229 | | |
229 | | - | |
| 230 | + | |
| 231 | + | |
230 | 232 | | |
231 | 233 | | |
232 | 234 | | |
| |||
282 | 284 | | |
283 | 285 | | |
284 | 286 | | |
285 | | - | |
| 287 | + | |
286 | 288 | | |
287 | 289 | | |
288 | 290 | | |
| |||
298 | 300 | | |
299 | 301 | | |
300 | 302 | | |
301 | | - | |
| 303 | + | |
302 | 304 | | |
303 | 305 | | |
304 | 306 | | |
305 | 307 | | |
306 | 308 | | |
307 | 309 | | |
308 | | - | |
309 | | - | |
310 | | - | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
311 | 313 | | |
312 | 314 | | |
313 | 315 | | |
314 | | - | |
315 | | - | |
316 | | - | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
317 | 319 | | |
318 | 320 | | |
319 | | - | |
320 | | - | |
321 | | - | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
322 | 324 | | |
323 | 325 | | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
328 | 330 | | |
329 | 331 | | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
334 | 336 | | |
335 | 337 | | |
336 | | - | |
337 | | - | |
338 | | - | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
339 | 341 | | |
340 | 342 | | |
341 | | - | |
342 | | - | |
343 | | - | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
344 | 347 | | |
345 | 348 | | |
346 | | - | |
347 | | - | |
348 | | - | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
349 | 352 | | |
350 | 353 | | |
351 | | - | |
352 | | - | |
353 | | - | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
354 | 357 | | |
355 | 358 | | |
356 | 359 | | |
357 | 360 | | |
358 | 361 | | |
359 | 362 | | |
360 | 363 | | |
361 | | - | |
362 | | - | |
363 | | - | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
364 | 367 | | |
365 | 368 | | |
366 | | - | |
367 | | - | |
368 | | - | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
369 | 372 | | |
370 | 373 | | |
371 | 374 | | |
372 | 375 | | |
373 | | - | |
374 | | - | |
| 376 | + | |
| 377 | + | |
375 | 378 | | |
376 | 379 | | |
377 | 380 | | |
| |||
416 | 419 | | |
417 | 420 | | |
418 | 421 | | |
419 | | - | |
| 422 | + | |
| 423 | + | |
420 | 424 | | |
421 | 425 | | |
422 | 426 | | |
| |||
429 | 433 | | |
430 | 434 | | |
431 | 435 | | |
432 | | - | |
433 | | - | |
434 | | - | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
435 | 439 | | |
436 | 440 | | |
437 | 441 | | |
| |||
504 | 508 | | |
505 | 509 | | |
506 | 510 | | |
507 | | - | |
508 | | - | |
509 | | - | |
510 | | - | |
511 | | - | |
512 | | - | |
513 | | - | |
514 | | - | |
515 | | - | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
516 | 520 | | |
517 | 521 | | |
518 | 522 | | |
| |||
574 | 578 | | |
575 | 579 | | |
576 | 580 | | |
577 | | - | |
578 | | - | |
| 581 | + | |
| 582 | + | |
579 | 583 | | |
580 | 584 | | |
581 | 585 | | |
| |||
824 | 828 | | |
825 | 829 | | |
826 | 830 | | |
827 | | - | |
| 831 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
550 | 550 | | |
551 | 551 | | |
552 | 552 | | |
553 | | - | |
554 | | - | |
555 | | - | |
556 | | - | |
557 | | - | |
558 | | - | |
559 | | - | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
560 | 576 | | |
561 | | - | |
| 577 | + | |
562 | 578 | | |
563 | 579 | | |
564 | 580 | | |
| |||
0 commit comments