Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html> <head> <title>Title of the Document</title> </head> <body> <div id="highlightDemo">Example</div> <script> let observer = new MutationObserver(mutations => { for(let mutation of mutations) { // examine new nodes, there is something to highlight for(let node of mutation.addedNodes) { // we only track elements, skip other nodes if(!(node instanceof HTMLElement)) continue; // check the inserted element for being a code snippets if(node.matches('pre[class*="language-"]')) { Prism.highlightElement(node); } // or maybe there is a code snippet somewhere in its subtree for(let elem of node.querySelectorAll('pre[class*="language-"]')) { Prism.highlightElement(elem); } } } }); let demoEl = document.getElementById('highlightDemo'); observer.observe(demoEl, { childList: true, subtree: true }); </script> </body> </html>