summaryrefslogtreecommitdiffstats
path: root/web_src/js/features/highlight.js
blob: d3f6ba71b80349fcaf0fb59d7afeba68a18b9c3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;

  const {default: Worker} = await import(/* webpackChunkName: "highlight" */'./highlight.worker.js');
  const worker = new Worker();

  worker.addEventListener('message', ({data}) => {
    const {index, html} = data;
    nodes[index].outerHTML = html;
  });

  for (let index = 0; index < nodes.length; index++) {
    const node = nodes[index];
    if (!node) continue;
    worker.postMessage({index, html: node.outerHTML});
  }
}