Skip to content

Instantly share code, notes, and snippets.

@g-plane
Last active May 9, 2025 03:20
Show Gist options
  • Save g-plane/6c3e6c82fb5bbe8a53b767d08c0dc419 to your computer and use it in GitHub Desktop.
Save g-plane/6c3e6c82fb5bbe8a53b767d08c0dc419 to your computer and use it in GitHub Desktop.
Replace highlight.js with Shiki in mdBook
window.hljs = {
/** @type {Promise<any>} */
shiki: undefined,
configure() {
this.shiki = import('https://esm.sh/[email protected]')
},
/** @param {HTMLElement} codeElement */
async highlightBlock(codeElement) {
const lang = [...codeElement.classList.values()]
.map((name) => name.match(/^language-(.+)$/)?.[1])
.find(Boolean)
if (!lang) {
return
}
const shiki = await this.shiki
codeElement.parentElement.outerHTML = await shiki.codeToHtml(codeElement.innerText, {
lang,
theme: 'one-light',
})
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment