1 Matching Annotations
  1. Dec 2021
    1. Using Web Workers

      You can run highlighting inside a web worker to avoid freezing the browser window while dealing with very big chunks of code.

      // In your main script:
      addEventListener('load', () => {
        const code = document.querySelector('#code');
        const worker = new Worker('worker.js');
        worker.onmessage = ({data}) => { code.innerHTML = data; }
        worker.postMessage(code.textContent);
      });
      
      // In worker.js:
      
      onmessage = (event) => {
        importScripts('<path>/highlight.min.js');
        const result = self.hljs.highlightAuto(event.data);
        postMessage(result.value);
      };