-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Better syntax highlighting: switching from highlight.js
to shiki
#2467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@ehuss Any update on this? Would this be something that you would consider? |
We are likely going to switch to build-side rendering with syntect (#1652), so probably not going to add any other systems. |
Looks really nice but the issue seems to be stale, so not sure if it can be included soon... It's been a pain for years to manually provide custom highlight.js file for unsupported languages. Also, shiki provide build-side rendering, with the possibility of exploring further ideas as I mentioned in the issue and with better syntax highlighting as, IMHO, TextMate grammars are slightly better than Sublime Text syntax with oniguruma. |
For those are interested in Shiki, you can put a let shikiModule
window.hljs = {
configure() {
shikiModule = import('https://esm.sh/[email protected]')
},
/** @param {HTMLElement} block */
async highlightBlock(block) {
const lang = [...block.classList.values()]
.map(name => name.match(/^language-(.+)$/)?.[1])
.filter(Boolean)[0]
if (!lang) {
return
}
const shiki = await shikiModule
block.parentElement.innerHTML =
await shiki.codeToHtml(block.innerText, { lang, theme: 'one-light' })
}
}
|
Uh oh!
There was an error while loading. Please reload this page.
Problem
The current highlighting library
highlight.js
is lightweight and can run on both server and client, but provides a very basic syntax parsing that could be greatly improved.Supersede #2313 #2292 #2268 #2238 #2237 #2360 #2445 #1652 ...
Proposed Solution
Consider switching to shiki, a high quality syntax highlighter that uses TextMate grammars and themes (same as VS Code), yet stays performant. It is also actively maintained.
There have been significant efforts and improvements that led to shiki-v1, and I believe that it's worthwile to make this migration and make shiki the default highlighter for mdbook. Today, mdbook is used to document a lot of code snippets and this change would definitely improve readability.
Example
Let's take Listing 20-24 from the rust book:
Vs-code reference (theme Github Dark Dimmed):

Implementation example
@cestef overriding of theme/book.js for shiki compatibility as explained here
Notes
Shiki v1: https://nuxt.com/blog/shiki-v1
Another alternative: https://github.com/wooorm/starry-night
Further ideas
Twoslash allows to extend Typescript code snippets from markdown file with extended Typescript compiler's information and vs code syntax engine. It is maintained and integrated perfectly with shiki.
With the strong
rust-analyzer
and the recent abstraction of the twoslash API in twoslash-protocol, it would make sense to build atwoslash-rs
for mdbook. Some preliminary explorations have been done: by ayazhafiz/twoslash-rust and @jxomThis first implementation of Shiki in mdbook would pave the way for this later on.
The text was updated successfully, but these errors were encountered: