You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Have you guys ever considered switching to Shiki for code highlight, instead of Prism?
I find Shiki a lot nicer, with much better grammars that are able to highlight much more code than Prism does.
Also, Shiki is being actively maintained, while Prism seems a little bit abandoned. However, even if Shiki wasn't being maintained, its themes would never stop improving since they are taken from VS Code anyway.
Here is a quick comparison I did:
Prism (dracula - the default theme from Docusaurus):
Maybe it's a good breaking change candidate for Docusaurus v3?
Self-service
I'd be willing to do some initial work on this proposal myself.
Reacted by Nozomi Ishii, Gyorgy Kallai, Yang Ming-Tian, Nerixyz, Giampaolo Bellavite, saif-at-scalekit, Lehoczky Zoltán, Juha Mustonen, Cyprien Quilici, Fahd Ashour and 8 more
But looking at Shiki's documentation, I don't see any indicator that it runs on server-side. In fact, it can run on server-side with Node.js. But it also runs in the browser, including a WebAssembly library to perform language tokenization.
I think that's debatable. If there is a text grammar compatible with VS Code (and several other editors which uses the same standard), it's easy to port to Shiki. For example, I use a lot of Jenkinsfiles, and Prism's support for it is limited to Groovy. There are a few dark VSCode extensions that adds rich syntax highlight for declarative Jenkins pipelines that I could then easily port to my website.
I have a feeling that the list of languages supported by Shiki tends to increase more than Prism does, also, maybe the languages not supported by Shiki but supported by Prism aren't that meaningful anwyay.
I think that's not actually applicable anymore. See the reference below:
You mentioned docusaurus-preset-shiki-twoslash. It works by injecting a remark-shiki-twoslash plugin into our content plugin's config. This plugin is a Remark plugin, so it runs on server side. Basically, it takes a Markdown string and spits out a bunch of JSX. However, this transformation is done once and only once when the Markdown is compiled to JSX, but not if your code was authored in JSX to start with.
Now there could be a way to migrate all this logic to client side, sure; but that's not how any existing solution in the wild works, and it will need a lot of prototyping in the community before we can consider its feasibility to be recommended to everyone. There's nothing stopping you from implementing it yourself, or asking the Shiki folks to try it.
Right now I will not be working on implementing it myself, and I believe Shiki folks are busy working towards Shiki 1.0. Maybe I can follow up with them after.
Thanks for the feedback, I'll close this issue for now (and reopen if something changes).
@SuperManito, I'd recommend you open a separate issue to discuss/propose Highlight.js.
May I suggest you make a library agnostic way of overriding the code highlighter.
For different languages people will always want different highlighters. If you give them an extensible method of simply plugging in their own, they will do it. My personal preference is codemirror :)
providing a remark plugin that process code blocks in Markdown AST, possibly highlighting code at build time instead of runtime
providing your own @theme/MDXComponents renderer for code blocks
swizzling the built-in @theme/CodeBlock and modifying it to not use Prism for some languages
That's probably largely enough extension points, we are not coupled to Prism, it is only used in one theme, but you can swap it for something else at multiple levels
I thought i had removed that patch actually, sorry about that.
Did some digging, it should be a pretty simple solve. Docusaurus uses jiti to enable esm/cjs/json/ts imports but isn't able to import @shikijs/vscode-textmate for some reason. This was resolved in jiti@v2 so I'll open a PR to update that dependency.
Nov 25 Update: Created a PR (#10716) which is waiting on a potential bug in jiti.
Reacted by Felipe Santos and Corbin Crutchley
Reacted by Felipe Santos, Thomas N Atkins and Corbin Crutchley
I thought i had removed that patch actually, sorry about that.
Did some digging, it should be a pretty simple solve. Docusaurus uses jiti to enable esm/cjs/json/ts imports but isn't able to import @shikijs/vscode-textmate for some reason. This was resolved in jiti@v2 so I'll open a PR to update that dependency.
Nov 25 Update: Created a PR which is waiting on a potential bug in jiti.
For the theme, the RehypeShikiOptions type says you can have themeorthemes, not both. If you are only using that single monokai-patched theme, you should just be able to require it under the theme key (though I don't have a custom theme to test with so YMMV)
For the langs, the rehype package expects a LanguageInput which is actually the imported TextMate Grammar. This means you probably want to require the file as a value into to the langs array.
Putting it all together, you end up with something like this:
That way of supplying the theme and language does indeed work, although interestingly enough it case-sensitively uses the name field from the TextMate Grammar.
The state of everything here is honestly quite abysmal. The Shiki Rehype plugin 'works', but has some really stupid bugs with Docusaurus specifically (shikijs/shiki#805 (comment)), also it's missing some pretty basic functionality (shikijs/shiki#1013). So far I found that honestly the shiki twoslash plugin is still a much better integration for Docusaurus, albeit for V3 it needs a weird fix for MDX compilation (shikijs/twoslash#125 (comment)), and it also has some issues related to font styles not applying (shikijs/twoslash#198) — which is never going to be fixed now because of a nonsensical deprecation with no migration steps. My best solution is to just patch this library directly in node_modules and that's the best of all worlds. Just my 2 cents.
I believe @slorber is doing a good chunk of work to make <Code> et. al. a little more swizzle friendly. I believe that this should move us towards the possibility of a theme that makes replacing Prism.js with Shiki a little more straight forward but it's been a while since I looked closely at what is required.
Activity
Josh-Cena commentedon Jul 6, 2023
Several things, based on my experience with Shiki, do correct me if I'm wrong:
What do you think?
felipecrs commentedon Jul 6, 2023
I'm actually confused.
PS: I know nothing about frontend nor React.
But looking at Shiki's documentation, I don't see any indicator that it runs on server-side. In fact, it can run on server-side with Node.js. But it also runs in the browser, including a WebAssembly library to perform language tokenization.
About using with React, I believe this may help: https://www.npmjs.com/package/remark-shiki-twoslash
I think that's debatable. If there is a text grammar compatible with VS Code (and several other editors which uses the same standard), it's easy to port to Shiki. For example, I use a lot of Jenkinsfiles, and Prism's support for it is limited to Groovy. There are a few dark VSCode extensions that adds rich syntax highlight for declarative Jenkins pipelines that I could then easily port to my website.
I have a feeling that the list of languages supported by Shiki tends to increase more than Prism does, also, maybe the languages not supported by Shiki but supported by Prism aren't that meaningful anwyay.
I think that's not actually applicable anymore. See the reference below:
I don't know, but at least it seems more under development than Prism is.
Josh-Cena commentedon Jul 6, 2023
You mentioned docusaurus-preset-shiki-twoslash. It works by injecting a remark-shiki-twoslash plugin into our content plugin's config. This plugin is a Remark plugin, so it runs on server side. Basically, it takes a Markdown string and spits out a bunch of JSX. However, this transformation is done once and only once when the Markdown is compiled to JSX, but not if your code was authored in JSX to start with.
Now there could be a way to migrate all this logic to client side, sure; but that's not how any existing solution in the wild works, and it will need a lot of prototyping in the community before we can consider its feasibility to be recommended to everyone. There's nothing stopping you from implementing it yourself, or asking the Shiki folks to try it.
felipecrs commentedon Jul 6, 2023
Alright. That makes sense!
Right now I will not be working on implementing it myself, and I believe Shiki folks are busy working towards Shiki 1.0. Maybe I can follow up with them after.
Thanks for the feedback, I'll close this issue for now (and reopen if something changes).
@SuperManito, I'd recommend you open a separate issue to discuss/propose Highlight.js.
slorber commentedon Jul 7, 2023
There's also https://bright.codehike.org/
We'll figure out what is the best solution after migrating to server components.
For now the current solution is good enough and the system is flexible enough for you to use Shiki if you want.
felipecrs commentedon Jul 7, 2023
OMG that's amazing!
ryanhamilton commentedon Jan 9, 2024
May I suggest you make a library agnostic way of overriding the code highlighter.
For different languages people will always want different highlighters. If you give them an extensible method of simply plugging in their own, they will do it. My personal preference is codemirror :)
slorber commentedon Jan 9, 2024
@ryanhamilton we have 4 ways of doing that:
@theme/MDXComponentsrenderer for code blocks@theme/CodeBlockand modifying it to not use Prism for some languagesThat's probably largely enough extension points, we are not coupled to Prism, it is only used in one theme, but you can swap it for something else at multiple levels
felipecrs commentedon Feb 7, 2024
It looks like Shiki got a complete revamp for 1.0: https://github.com/shikijs/shiki/releases
Apparently things that were previously only server side can now run client side.
25 remaining items
lachieh commentedon Nov 22, 2024
I thought i had removed that patch actually, sorry about that.
Did some digging, it should be a pretty simple solve. Docusaurus uses
jitito enable esm/cjs/json/ts imports but isn't able to import@shikijs/vscode-textmatefor some reason. This was resolved injiti@v2so I'll open a PR to update that dependency.Nov 25 Update: Created a PR (#10716) which is waiting on a potential bug in jiti.
Nova38 commentedon Dec 30, 2024
Is there a good workaround in the meantime?
lachieh commentedon Dec 30, 2024
lachieh commentedon Apr 15, 2025
Update as of April 2025, the latest version of Shiki and the rehype plugin work out-of-the-box. Updated my instructions here:
https://lachieh.github.io/docusaurus-with-shiki-rehype/docs/intro
Sainan commentedon Apr 15, 2025
That's nice, but it seems it's missing some customisability, as e.g. my existing config with a custom theme and language do not seem to work:
lachieh commentedon Apr 15, 2025
For the theme, the
RehypeShikiOptionstype says you can havethemeorthemes, not both. If you are only using that singlemonokai-patchedtheme, you should just be able to require it under thethemekey (though I don't have a custom theme to test with so YMMV)For the
langs, the rehype package expects aLanguageInputwhich is actually the imported TextMate Grammar. This means you probably want to require the file as a value into to thelangsarray.Putting it all together, you end up with something like this:
We're using it with custom grammars, but not themes in the wasmcloud.com docs right here:
https://github.com/wasmCloud/wasmcloud.com/blob/4a8b0f9f22f1962fa74fd547a9e72efcefbbef15/docusaurus.config.ts#L44-L48
Edit: I will note that we are using esm versus commonjs, so as long as require will import and parse the JSON, you should be good.
Sainan commentedon Apr 15, 2025
That way of supplying the theme and language does indeed work, although interestingly enough it case-sensitively uses the
namefield from the TextMate Grammar.lachieh commentedon Apr 15, 2025
Glad to hear it! Yes, I remember now that you mention it that I had to adjust some fields in the tmLanguage file to get it to play nice.
ejball commentedon May 16, 2025
Thanks for this! Prism support for C# has fallen way behind, but Shiki looks great!
Sainan commentedon May 16, 2025
The state of everything here is honestly quite abysmal. The Shiki Rehype plugin 'works', but has some really stupid bugs with Docusaurus specifically (shikijs/shiki#805 (comment)), also it's missing some pretty basic functionality (shikijs/shiki#1013). So far I found that honestly the shiki twoslash plugin is still a much better integration for Docusaurus, albeit for V3 it needs a weird fix for MDX compilation (shikijs/twoslash#125 (comment)), and it also has some issues related to font styles not applying (shikijs/twoslash#198) — which is never going to be fixed now because of a nonsensical deprecation with no migration steps. My best solution is to just patch this library directly in
node_modulesand that's the best of all worlds. Just my 2 cents.lachieh commentedon May 16, 2025
@ejball Glad it was of some help!
felipecrs commentedon May 16, 2025
@lachieh support for the Docusaurus-like line highlighting comments would be great. They would simplify migration of large codebases a lot.
https://docusaurus.io/docs/3.4.0/markdown-features/code-blocks#line-highlighting
Do you have any idea on how to replicate that with shiki?
lachieh commentedon May 16, 2025
Could be done with a shiki transformer or with the CSS
counterlike this examplefelipecrs commentedon May 16, 2025
Interesting. Maybe we can have some of these added to @shikijs/transformers, or maybe a standalone shiki transformer package just for Docusaurus.
lachieh commentedon May 19, 2025
I believe @slorber is doing a good chunk of work to make
<Code>et. al. a little more swizzle friendly. I believe that this should move us towards the possibility of a theme that makes replacing Prism.js with Shiki a little more straight forward but it's been a while since I looked closely at what is required.