Skip to content

fix(jseditor): fallback to hljs.highlightBlock for older highlight.js…#5340

Merged
walterbender merged 1 commit intosugarlabs:masterfrom
Ashutoshx7:fix/hljs-highlighter-regression-5334
Jan 31, 2026
Merged

fix(jseditor): fallback to hljs.highlightBlock for older highlight.js…#5340
walterbender merged 1 commit intosugarlabs:masterfrom
Ashutoshx7:fix/hljs-highlighter-regression-5334

Conversation

@Ashutoshx7
Copy link
Contributor

@Ashutoshx7 Ashutoshx7 commented Jan 26, 2026

Description

Fixes #5334 - Regression where the highlighter used by the js-editor is not loading.

Problem

The JS Editor widget was calling hljs.highlightElement() which only exists in Highlight.js v11+. However, the bundled lib/codejar/highlight.pack.js is v10.1.2 which uses hljs.highlightBlock() instead.

Solution

Added feature detection to check for the presence of highlightElement:

  • Use hljs.highlightElement(editor) if available (Highlight.js v11+)
  • Fall back to hljs.highlightBlock(editor) for older versions (v10.x)

This ensures compatibility with both the local bundled library and any CDN-loaded version.

Changes

  • Modified js/widgets/jseditor.js to add fallback logic
  • Added module.exports for testing compatibility
  • Added js/widgets/tests/jseditor.test.js with unit tests for both code paths

Testing

  • ✅ Unit tests pass for both v11 and v10 scenarios
  • ✅ Verified fix addresses the exact error from the issue

Related Issue

Closes #5334

@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

1 similar comment
@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@walterbender
Copy link
Member

Maybe we should update lib/codejar/highlight.pack.js to a newer version?

@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@github-actions
Copy link
Contributor

❌ Some Jest tests failed. Please check the logs and fix the issues before merging.

Failed Tests:

jseditor.test.js

Addresses Walter's feedback to update the bundled highlight.js library to a newer version instead of adding fallback logic. The v11 API uses highlightElement() which is what jseditor.js expects.
@Ashutoshx7 Ashutoshx7 force-pushed the fix/hljs-highlighter-regression-5334 branch from fe6ed6c to 3fcfbcf Compare January 31, 2026 18:24
@github-actions
Copy link
Contributor

✅ All Jest tests passed! This PR is ready to merge.

@Ashutoshx7
Copy link
Contributor Author

Maybe we should update lib/codejar/highlight.pack.js to a newer version?

updated lib/codejar/highlight.pack.js to a newer version

@walterbender walterbender merged commit d3feb90 into sugarlabs:master Jan 31, 2026
6 checks passed
vanshika2720 pushed a commit to vanshika2720/musicblocks that referenced this pull request Feb 11, 2026
Addresses Walter's feedback to update the bundled highlight.js library to a newer version instead of adding fallback logic. The v11 API uses highlightElement() which is what jseditor.js expects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] highlighter not loading

2 participants