Skip to content

Move default highlight.js highlighting into a web worker #3591

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

Closed
Closed
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
10 changes: 10 additions & 0 deletions public/js/highlight-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// A very simple web worker for highlight.js
// See: https://highlightjs.org/usage/
'use strict';

importScripts('/vendor/plugins/highlight/highlight.pack.js');

onmessage = function(event) {
var result = self.hljs.highlightAuto(event.data.text);
postMessage({index: event.data.index, html: result.value});
};
9 changes: 8 additions & 1 deletion public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,14 @@ $(document).ready(function () {

// Highlight JS
if (typeof hljs != 'undefined') {
hljs.initHighlightingOnLoad();
var codeElements = $('pre code');
var worker = new Worker('/js/highlight-worker.js');
worker.onmessage = function(event) {
$(codeElements[event.data.index]).html(event.data.html);
}
$(codeElements).each(function (index, element) {
worker.postMessage({index: index, text: $(element).text()});
});
}

// Dropzone
Expand Down