2020-04-13 18:32:31 +05:30
|
|
|
export default async function highlight(elementOrNodeList) {
|
|
|
|
if (!window.config || !window.config.HighlightJS || !elementOrNodeList) return;
|
|
|
|
const nodes = 'length' in elementOrNodeList ? elementOrNodeList : [elementOrNodeList];
|
|
|
|
if (!nodes.length) return;
|
2020-01-29 03:27:20 +05:30
|
|
|
|
2020-04-13 18:32:31 +05:30
|
|
|
const {default: Worker} = await import(/* webpackChunkName: "highlight" */'./highlight.worker.js');
|
|
|
|
const worker = new Worker();
|
2020-01-29 03:27:20 +05:30
|
|
|
|
2020-04-13 18:32:31 +05:30
|
|
|
worker.addEventListener('message', ({data}) => {
|
|
|
|
const {index, html} = data;
|
|
|
|
nodes[index].outerHTML = html;
|
|
|
|
});
|
2020-01-29 03:27:20 +05:30
|
|
|
|
2020-04-13 18:32:31 +05:30
|
|
|
for (let index = 0; index < nodes.length; index++) {
|
|
|
|
const node = nodes[index];
|
|
|
|
if (!node) continue;
|
|
|
|
worker.postMessage({index, html: node.outerHTML});
|
|
|
|
}
|
2020-01-29 03:27:20 +05:30
|
|
|
}
|