MCPcopy
hub / github.com/ccampbell/rainbow / _highlightCodeBlocks

Function _highlightCodeBlocks

src/rainbow.js:203–233  ·  view source on GitHub ↗

* Browser Only - Sends messages to web worker to highlight elements passed * in * * @param {Array} codeBlocks * @param {Function} callback * @return {void}

(codeBlocks, callback)

Source from the content-addressed store, hash-verified

201 * @return {void}
202 */
203function _highlightCodeBlocks(codeBlocks, callback) {
204 const waitingOn = { c: 0 };
205 for (const block of codeBlocks) {
206 const language = getLanguageForBlock(block);
207 if (block.classList.contains('rainbow') || !language) {
208 continue;
209 }
210
211 // This cancels the pending animation to fade the code in on load
212 // since we want to delay doing this until it is actually
213 // highlighted
214 block.classList.add('loading');
215 block.classList.add('rainbow');
216
217 // We need to make sure to also add the loading class to the pre tag
218 // because that is how we will know to show a preloader
219 if (block.parentNode.tagName === 'PRE') {
220 block.parentNode.classList.add('loading');
221 }
222
223 const globalClass = block.getAttribute('data-global-class');
224 const delay = parseInt(block.getAttribute('data-delay'), 10);
225
226 ++waitingOn.c;
227 _messageWorker(_getWorkerData(block.innerHTML, { language, globalClass, delay }), _generateHandler(block, waitingOn, callback));
228 }
229
230 if (waitingOn.c === 0) {
231 callback();
232 }
233}
234
235function _addPreloader(preBlock) {
236 const preloader = document.createElement('div');

Callers 1

_highlightFunction · 0.85

Calls 4

getLanguageForBlockFunction · 0.90
_messageWorkerFunction · 0.85
_getWorkerDataFunction · 0.85
_generateHandlerFunction · 0.85

Tested by

no test coverage detected