"use strict"; function highlight() { const codeContent = document.querySelectorAll(".h5p-question-introduction pre"); for(const code of codeContent) { code.innerHTML = ""+code.innerText.replace("```java\n", "").replace(/\n[\u200B-\u200D\uFEFF]*```/, "")+""; hljs.highlightElement(code.firstChild); } const questionContent = document.querySelectorAll(".h5p-question-content p, .h5p-question-introduction p, .h5p-alternative-inner > div"); for(const questionElement of questionContent) { let withinCode = false; let preBlock; let codeBlock; for(let i = 0; i < questionElement.childNodes.length; i++) { const child = questionElement.childNodes[i]; if(child.nodeType === Node.TEXT_NODE) { if(child.wholeText.includes("```java ") && child.wholeText.trim().endsWith("```")) { preBlock = document.createElement("pre"); codeBlock = document.createElement("code"); codeBlock.className = "language-java"; codeBlock.style = "background:#f3f3f3"; codeBlock.innerText = child.wholeText.replace("```java ", "").replace("```", ""); hljs.highlightElement(codeBlock); preBlock.append(codeBlock); child.replaceWith(preBlock); i--; continue; } else if(child.wholeText.includes("```java")) { withinCode = true; preBlock = document.createElement("pre"); codeBlock = document.createElement("code"); codeBlock.className = "language-java"; codeBlock.style = "background:#f3f3f3"; preBlock.append(codeBlock); child.remove(); i--; continue; } else if(withinCode && child.wholeText.includes("```")) { withinCode = false; child.replaceWith(preBlock); continue; } } if(withinCode) { if(child.tagName === "BR") { child.remove(); if(codeBlock.childElementCount > 0) { const newline = document.createTextNode("\n"); codeBlock.appendChild(newline); } i--; } else if(child.nodeType == Node.TEXT_NODE) { const highlightedCode = document.createElement("span"); highlightedCode.innerText = child.wholeText; highlightedCode.className = "language-java"; hljs.highlightElement(highlightedCode); codeBlock.appendChild(highlightedCode); child.remove(); i--; } else { // we must be careful with text inputs, they have event listeners attached codeBlock.appendChild(child); i--; } } } } } // we have to wait until the question is rendered setTimeout(highlight, 1000);