Skip to content

Instantly share code, notes, and snippets.

@webarthur
Created July 7, 2023 03:14
Show Gist options
  • Save webarthur/b2729dca2df3d9b7f18d387486ff9081 to your computer and use it in GitHub Desktop.
Save webarthur/b2729dca2df3d9b7f18d387486ff9081 to your computer and use it in GitHub Desktop.
Manually load Prism.js for syntax highlighting after the DOM has loaded. Force to highlight javascript syntax.
// Set the manual property of Prism object to true
window.Prism = { manual: true }
// Add an event listener for when the window is loaded
window.addEventListener('load', () => {
// Import the Prism.js library from the specified URL
import('https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/prism.min.js')
.then(() => {
console.log('Prism loaded.')
// Select all <pre><code> elements in the document
document.querySelectorAll('pre code').forEach(el => {
// Add the 'language-javascript' class to each element
el.classList.add('language-javascript')
// Apply syntax highlighting to the code element using Prism
Prism.highlightElement(el)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment