 27e3cddfbe
			
		
	
	
		27e3cddfbe
		
			
		
	
	
	
	
		
			
			This should eliminate page freezes when loading big files/diff. `highlightBlock` is needed to preserve existing nodes when highlighting and for that, highlight.js needs access to the DOM API so I added a DOM implementation to make it work, which adds around 300kB to the output file size of the lazy-loaded `highlight.js`. Co-authored-by: Lauris BH <lauris@nix.lv>
		
			
				
	
	
		
			19 lines
		
	
	
	
		
			693 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			19 lines
		
	
	
	
		
			693 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 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});
 | |
|   }
 | |
| }
 |