Skip to content

Use shiki for code highlight #9122

Open

Description

@felipecrs

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):

chrome_tz08jPgBEk

Shiki (dracula):

chrome_7sR87uZFQO

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

  • I'd be willing to do some initial work on this proposal myself.

Activity

  1. added
    proposalThis issue is a proposal, usually non-trivial change
    status: needs triageThis issue has not been triaged by maintainers
    on Jul 4, 2023
  2. Josh-Cena commented on Jul 6, 2023

    @Josh-Cena
    Collaborator

    Several things, based on my experience with Shiki, do correct me if I'm wrong:

    1. Shiki is server-side and generates static markup during Markdown parsing (using a Rehype plugin, IIRC), so there's no way to use it on a React page.
    2. Shiki doesn't support as many languages as Prism, neither is it as easy to add them.
    3. Shiki being purely server-side means it cannot be used in live code blocks, so there's inconsistency out-of-the-box.

    What do you think?

  3. added
    status: needs more informationThere is not enough information to take action on the issue.
    and removed
    status: needs triageThis issue has not been triaged by maintainers
    on Jul 6, 2023
  4. felipecrs commented on Jul 6, 2023

    @felipecrs
    ContributorAuthor

    I'm actually confused.

    PS: I know nothing about frontend nor React.

    1. 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

    2. 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.

    3. I think that's not actually applicable anymore. See the reference below:

    How about Highlight.js?

    I don't know, but at least it seems more under development than Prism is.

  5. Josh-Cena commented on Jul 6, 2023

    @Josh-Cena
    Collaborator

    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.

  6. felipecrs commented on Jul 6, 2023

    @felipecrs
    ContributorAuthor

    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.

  7. slorber commented on Jul 7, 2023

    @slorber
    Collaborator

    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.

  8. felipecrs commented on Jul 7, 2023

    @felipecrs
    ContributorAuthor

    There's also https://bright.codehike.org/

    OMG that's amazing!

  9. ryanhamilton commented on Jan 9, 2024

    @ryanhamilton

    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 :)

  10. slorber commented on Jan 9, 2024

    @slorber
    Collaborator

    @ryanhamilton we have 4 ways of doing that:

    • implementing your own theme
    • 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

  11. felipecrs commented on Feb 7, 2024

    @felipecrs
    ContributorAuthor

    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.

  12. 25 remaining items

  13. lachieh commented on Nov 22, 2024

    @lachieh
    Contributor

    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.

  14. Nova38 commented on Dec 30, 2024

    @Nova38

    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.

    Is there a good workaround in the meantime?

  15. lachieh commented on Dec 30, 2024

    @lachieh
  16. lachieh commented on Apr 15, 2025

    @lachieh
    Contributor

    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

    npm i @shikijs/rehype@^3 shiki@^3
    
  17. Sainan commented on Apr 15, 2025

    @Sainan
    Contributor

    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:

    {
      theme: 'monokai-patched',
      langs: [
        'c',
        'cpp',
        'lua',
        {
          id: 'pluto',
          name: 'pluto',
          scopeName: 'source.pluto',
          path: require.resolve('./src/theme/Pluto.tmLanguage.json'),
        }
      ],
      themes: [
        require('./src/theme/monokai-patched.json'),
      ],
    }
  18. lachieh commented on Apr 15, 2025

    @lachieh
    Contributor

    For the theme, the RehypeShikiOptions type says you can have theme or themes, 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:

    {
      theme: require('./src/theme/monokai-patched.json'),
      langs: [
        'c',
        'cpp',
        'lua',
        require('./src/theme/Pluto.tmLanguage.json'),
      ],
    }

    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.

  19. Sainan commented on Apr 15, 2025

    @Sainan
    Contributor

    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.

  20. lachieh commented on Apr 15, 2025

    @lachieh
    Contributor

    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.

  21. ejball commented on May 16, 2025

    @ejball

    the latest version of Shiki and the rehype plugin work out-of-the-box

    Thanks for this! Prism support for C# has fallen way behind, but Shiki looks great!

  22. Sainan commented on May 16, 2025

    @Sainan
    Contributor

    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.

  23. lachieh commented on May 16, 2025

    @lachieh
    Contributor

    @ejball Glad it was of some help!

  24. felipecrs commented on May 16, 2025

    @felipecrs
    ContributorAuthor

    @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?

  25. lachieh commented on May 16, 2025

    @lachieh
    Contributor

    Could be done with a shiki transformer or with the CSS counter like this example

  26. felipecrs commented on May 16, 2025

    @felipecrs
    ContributorAuthor

    Interesting. Maybe we can have some of these added to @shikijs/transformers, or maybe a standalone shiki transformer package just for Docusaurus.

  27. lachieh commented on May 19, 2025

    @lachieh
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalThis issue is a proposal, usually non-trivial change

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions