Last active
May 9, 2025 03:20
-
-
Save g-plane/6c3e6c82fb5bbe8a53b767d08c0dc419 to your computer and use it in GitHub Desktop.
Replace highlight.js with Shiki in mdBook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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