-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
Use shiki for code highlight #9122
Comments
Several things, based on my experience with Shiki, do correct me if I'm wrong:
What do you think? |
I'm actually confused. PS: I know nothing about frontend nor React.
I don't know, but at least it seems more under development than Prism is. |
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. |
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. |
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. |
OMG that's amazing! |
May I suggest you make a library agnostic way of overriding the code highlighter. |
@ryanhamilton we have 4 ways of doing that:
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 |
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. |
Shiki may become the industry's best solution for code highlighting. |
I'm actually really keen on Shiki, but the Docusaurus integration of https://github.com/shikijs/twoslash/tree/main/packages/docusaurus-preset-shiki-twoslash unfortunately has issues that mean (much as I'd like to) I've been unable to make use of it on my own site at present: |
I don't think docusaurus-preset-shiki-twoslash was updated to work with Shiki 1.0 yet, nor Docusaurus 3.0. |
I'm +1 on investigating integration, but I think it would greatly help us if there's a community proof-of-concept. I'm mainly interested because Shiki can do other fun things like showing type/linting errors. |
@Josh-Cena I currently have the slightly older |
It would be hard to make Shiki the default option of Docusaurus considering we probably want by default to be consistent between the regular code block and the live code block (both using Prism) and afaik live code block is not really an option (nor a good idea?) with Shiki. We usually don't create official plugins for things we don't use ourselves. However, it's probably worth it to explore how to use Shiki/Twoslash to enhance the experience on our own website, and create an opt-in plugin that users can turn on with some code block metastring on Docusaurus v3.x? ```ts shiki
const hello = "World"
```
```ts shiki twoslash
// @module: esnext
// @filename: maths.ts
export function absolute(num: number) {
if (num < 0) return num * -1;
return num;
}
// @filename: hello.ts
const hello = "World"
// ^?
``` If this opt-in syntax highlighter becomes successful, we could see how to add an option to make it the default in Docusaurus v4. Note:
@lachieh thanks for your proposal, I'm curious to see how you implement such preset. To be honest, I think Docusaurus is missing a few primitives to make it possible to implement this as a robust plugin or preset, so it's probably better if we work on it in the main repo, but I'd be curious to look at your solution. |
Thanks, @slorber. That all makes sense. I'll hold off on making the preset for now but if anyone finds their way here from a search, the PR below shows one way to integrate the |
@lachieh thanks, I got it working on my install as I wanted to use shiki over prism. Prism is just not as good as shiki when showing more complex snippets. Here is an example of a 2k line bash script that would have multiple cosmetic issues with prism due to the bash parser getting confused as well as inconsistent highlighting but works fine with shiki. https://userdocs.github.io/qbittorrent-nox-static/docs/bash/ Now, I understand this is not a normal use case posting a 2k lines of code but i tried many things/langs/examples with prism, I made my own themes and the outcome was never as good as it is with shiki. It's just better. You can use any vscode json theme and it will look exactly like it does in vscode in docusaurus. I'm using this https://github.com/daltonmenezes/aura-theme/tree/main/packages/vscode This is one reason I wanted to jump over to astro/starlight which is using this https://github.com/expressive-code/expressive-code + shiki but i'd really like to see it implemented here. This vscode engine idea is doing something right. It looks amazing. |
TIL about https://expressive-code.com, thanks that looks interesting. |
Hi, @slorber, any success with this? |
Code Hike could be another alternative. There's an example of how to use it with Docusaurus (v3.5.0+) here: https://github.com/code-hike/examples/tree/main/with-docusaurus |
Indeed, thanks for suggesting it Also showing what can be built in userland! We don't necessarily need to build first-class support and the community can work on it too. Note Josh Comeau provided useful feedback on Shiki: https://www.joshwcomeau.com/blog/how-i-built-my-blog-v2/
It's possible that making Shiki the default for Docusaurus would slow down the build time and increase memory usage, in addition to the inability to use it in live code blocks. This confirms my initial intuition that we should keep Prism as the default highlighter for now, and Shiki should be an optional plugin. |
Shiki can sync use now #sync-usage By the way. Shiki can't use on Docusaurus v3 |
@SuperManito There are plenty of examples of Shiki on Docusaurus 3, including in this thread. Are you having a particular issues with it? |
The plugin docusaurus-preset-shiki-twoslash doesn't support Docusaurus 3. |
That's correct. It's a community preset that was created with the pre-1.0 version of Shiki.js before maintainership was taken over by Anthony Fu. There is already an issue about that on the old repo. That change is unlikely to happen since Shiki 1.0 is already supported as a rehype plugin, and therefore can be implemented directly into Docusaurus 3.0. |
I have yet to see any examples of Shiki on Docusaurus 3 that actually just work. |
Here is a guide on installing https://lachieh.github.io/docusaurus-with-shiki-rehype/docs/intro The following section shows how to add a dark/light mode, and the site itself includes examples on how to use some of the transformers from |
Thanks for the demo @lachieh It's a good starting point so that we can think about how we could simplify this setup in the future |
@slorber yeah, no worries. Is this something that could be bundled into a theme or a preset? Which would work better assuming most would likely try to install alongside |
We plan to introduce a global markdown config and plugin customization API to register global remark/rehype plugins, so this can be implemented as a plugin. We can figure out a solution where our theme components offer a "pass-through" mode so that you don't have to swizzle them to unplug Prism runtime code blocks. |
I'd be interested in contributing to that effort! |
I am running into a import error about some of the submodules not exporting the main. When I tried your example in stackblitz it failed to run. Running it locally with yarn it worked. I saw that you had to patch one of the libraries. Any idea why this error is happening and if it lies with the way docusaurus is importing the library or if it is an issue with they way shiki is exporting itself. I am trying to use this in a project that is using npm so it isn't possible to directly patch the library with our using an external package, but manually patching it in the node_modules folder it had the same issue in another one of the sub libraries
|
This one?
Because this is what I get when I try it. I believe it has something to do with my |
I thought i had removed that patch actually, sorry about that. Did some digging, it should be a pretty simple solve. Docusaurus uses Nov 25 Update: Created a PR (#10716) which is waiting on a potential bug in jiti. |
Is there a good workaround in the meantime? |
Did some digging because we're currently using Shiki on wasmcloud.com with no yarn patch or anything. Apparently, the last version of Shiki that works with Docusaurus is 1.22.2. I've updated my example instructions, but the TL;DR is this:
|
Have you read the Contributing Guidelines on issues?
Motivation
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):
Shiki (dracula):
PS: I'm aware of docusaurus-preset-shiki-twoslash, and that's what I used for the example above (https://github.com/felipecrs/shiki-docusaurus-test). This is to suggest a better default for the project as a whole.
Maybe it's a good breaking change candidate for Docusaurus v3?
Self-service
The text was updated successfully, but these errors were encountered: