Shiki

A beautiful Syntax Highlighter.

README

We are working towards 1.0, which means:

- 0.13.0 will be the last minor version
- No new features, only minor bug fixes
- Work will happen in dev branch
- If you want to help, or ask for features, comment in #424


Shiki


Shiki is a beautiful syntax highlighter.

About


Shiki uses TextMate grammars to tokenize strings, and colors the tokens via VS Code themes. In short, Shiki generates HTML that looks exactly like your code in VS Code, and it works great in your static website generator (or your dynamic website).

No custom RegEx to maintain, no custom CSS to maintain, no custom HTML to maintain. And as your favorite languages and themes in VS Code evolve - your syntax highlighting will evolve too.

Basic usage


Get a quick start in Node.js or in plain websites with the instructions below.

Node.js


To use Shiki in Node.js, install it with npm (or your favorite package manager):

  1. ```sh
  2. npm i shiki
  3. ```

  1. ```js
  2. const shiki = require('shiki')

  3. shiki
  4.   .getHighlighter({
  5.     theme: 'nord'
  6.   })
  7.   .then(highlighter => {
  8.     console.log(highlighter.codeToHtml(`console.log('shiki');`, { lang: 'js' }))
  9.   })

  10. //
  11. //  
  12. //
  13. ```

CDN


If you don't have a Node.js project, or if you just want to use the build in the browser, you can use one of the CDN builds:

  1. ```html
  2. <script src="https://unpkg.com/shiki"></script>
  3. !-- or -->
  4. <script src="https://cdn.jsdelivr.net/npm/shiki"></script>
  5. <script>
  6.   shiki
  7.     .getHighlighter({
  8.       theme: 'nord',
  9.       langs: ['js'],
  10.     })
  11.     .then(highlighter => {
  12.       const code = highlighter.codeToHtml(`console.log('shiki');`, { lang: 'js' })
  13.       document.getElementById('output').innerHTML = code
  14.     })
  15. </script>
  16. ```

For more custom usage, learn about Shiki's architecture, and take a look at the Configuration and Options section.

And after that you can (and should) check out the reference docs for deeper dives:


Shiki's architecture


Shiki has a lot of points for customization notably: themes, languages, and custom renderers.

Shiki leverages the TextMate grammar system to tokenize code. It uses the VS Code Oniguruma library to do the heavy lifting of matching the grammar rules to code.

At the core is Shiki's highlighter. The highlighter is a class which takes a theme and languages, and exposes functions likecodeToHtml which converts string of arbitrary code and returns a string of HTML for rendering on the web.

When you call getHighlighter, Shiki will:

- load the theme and the languages you've requested
- load the WebAssembly file from Oniguruma

and return a Highlighter object.

The default locations where the files are loaded from depend on the environment:

- In Node.js:

  - Languages are loaded from node_modules/shiki/languages. Similarly, the themes are loaded from node_modules/shiki/themes.
  - The Oniguruma WebAssembly file is loaded from the node_modules/vscode-oniguruma/release/onig.wasm.

- In the browser:
  - Languages are loaded from /languages, relative to your site's root. Similarly, the themes are loaded from /themes.
  - The Oniguruma WebAssembly file is loaded from /dist/onig.wasm path relative to your site's root.

After that you can use the highlighter to generate HTML using highlighter.codeToHtml. This works by converting the code into tokens and then rendering the tokens using a default renderer which applies the theme's colors to the tokens.

As with all things, there are more details, so let's take a look at the Configuration and Options section.

Configuration and Options


Creating the highlighter


The exported named function getHighlighter returns a Promise, choose either the async/await syntax or the .then syntax based on your preference:

  1. ```js
  2. // Note no languages are specified. Which then defaults to load all languages.
  3. // Which is probably ok in Node.js, but not something you want in the browser.
  4. const highlighter = await getHighlighter({
  5.   theme: 'nord'
  6. })
  7. ```

This async code is


Load the highlighter with a default theme and a list of languages:

  1. ```js
  2. // ONLY the languages specified will be loaded. If you need other languages later,
  3. // you'll need to load them separately (see below).
  4. const highlighter = await getHighlighter({
  5.   theme: 'nord',
  6.   langs: ['javascript', 'python']
  7. })
  8. ```

Load the highlighter with a default theme and no languages:

  1. ```js
  2. // ONLY the languages specified will be loaded. It's an empty array, so Shiki won't  preload anything.
  3. // For every language that you need later you'll have to load it separately (see down below).
  4. const highlighter = await getHighlighter({
  5.   theme: 'nord',
  6.   langs: []
  7. })
  8. ```

Load the highlighter with multiple themes, and a list of languages:

  1. ```js
  2. // The first theme in the list will be the default theme.

  3. const highlighter = await getHighlighter({
  4.   themes: ['github-light', 'nord'],
  5.   langs: ['javascript', 'python']
  6. })
  7. ```

Load the highlighter with multiple themes, a list of languages, and override the default paths for the languages and themes:

  1. ```js
  2. const highlighter = await getHighlighter({
  3.   themes: ['github-light', 'nord'],
  4.   langs: ['javascript', 'python'],
  5.   paths: {
  6.     themes: '/path/to/themes',
  7.     languages: '/path/to/languages'
  8.   }
  9. })
  10. ```

Most likely you won't need to do this, but it's useful to understand the options.


Convert code to HTML


The highlighter exposes a codeToHtml method that takes a string of code, an options object, and returns a string of HTML.

Convert code to HTML with the default theme:

  1. ```js
  2. const code = `console.log("Here is your code.");`
  3. const output = highlighter.codeToHtml(code, { lang: 'js' })
  4. ```

Convert code to HTML with a specific theme:

  1. ```js
  2. const code = `console.log("Here is your code.");`
  3. const output = highlighter.codeToHtml(code, { lang: 'js', theme: 'nord' })
  4. ```

Load languages and themes after the highlighter has been created


You can load languages and themes after the highlighter has been created. For that, the highlighter exposes getLoadedLanguages()/getLoadedThemes() and loadLanguage()/loadTheme().

A good practice, especially for browser environments, is to initialize the highlighter with your most used languages and themes, and then load more languages and themes on demand.

You can also access the list of all themes and languages which are bundled using BUNDLED_LANGUAGES and BUNDLED_THEMES.

  1. ```js
  2. import { BUNDLED_LANGUAGES, BUNDLED_THEMES, getHighlighter } from 'shiki'

  3. const highlighter = await getHighlighter({
  4.   theme: 'nord',
  5.   langs: ['javascript', 'python']
  6. })

  7. // Some code that tells you that you now need `ruby` highlighting
  8. const code = imaginaryCodeThatNeedsRubyHighlighting()
  9. const language = imaginaryLanguageDetection(code)
  10. const newTheme = imaginaryCodeThatDefinesTheTheme()

  11. // Check for the loaded languages, and load the language if it's not loaded yet.
  12. if (!highlighter.getLoadedLanguages().includes(language)) {
  13.   // Check if the language is supported by Shiki
  14.   const bundles = BUNDLED_LANGUAGES.filter((bundle) => {
  15.     // Languages are specified by their id, they can also have aliases (i. e. "js" and "javascript")
  16.     return bundle.id === language || bundle.aliases?.includes(language);
  17.   });
  18.   if (bundles.length > 0) {
  19.     await highlighter.loadLanguage(language)
  20.   } else {
  21.     // Do some error handling or default to another language or...
  22.   }
  23. }

  24. // Check for the loaded themes, and load the theme if it's not loaded yet.
  25. if (!highlighter.getLoadedThemes().includes(newTheme)) {
  26.   // Check if the theme is supported by Shiki
  27.   if (BUNDLED_THEMES.includes(newTheme) {
  28.     await highlighter.loadTheme(newTheme)
  29.   } else {
  30.     // Do some error handling or default to another theme or...
  31.   }
  32. }

  33. const output = highlighter.codeToHtml(code, { lang: language, theme: newTheme })
  34. ```

Specify a custom root directory


You can specify a custom root directory for your languages and themes. This is useful if you want to use Shiki in a browser environment and want to specify a different host (and/or path), or if you want to specify a dedicated directory structure for the current host.

If used, setCDN must be invoked before getHighlighter is called.

You won't have to use setCDN if you use the pre-built packages on unpkg or jsDelivr.


Set a custom root path:

  1. ```js
  2. import { getHighlighter, setCDN } from 'shiki'

  3. // Assets will be loaded from the host root + asset path, for example `/assets/shiki/languages/'
  4. setCDN('/assets/shiki/')

  5. const highlighter = await getHighlighter({
  6.   theme: 'nord',
  7.   langs: ['javascript', 'python']
  8. })
  9. ```

Set a custom host and root path:

  1. ```js
  2. import { getHighlighter, setCDN } from 'shiki'

  3. // Assets will be loaded from remote host + asset path, for example `https://your.cdn.com/your/path/languages/`
  4. setCDN('https://your.cdn.com/your/path/')

  5. const highlighter = await getHighlighter({
  6.   theme: 'nord',
  7.   langs: ['javascript', 'python']
  8. })
  9. ```

Set a custom root path and a custom asset path:

  1. ```js
  2. import { getHighlighter, setCDN } from 'shiki'

  3. setCDN('/assets/shiki/')

  4. // Languages will be loaded from `/assets/shiki/imported/` instead of `/languages/`.
  5. // Themes will be loaded from `/assets/shiki/themes/` (as their default is not overridden).

  6. const highlighter = await getHighlighter({
  7.   theme: 'nord',
  8.   langs: ['javascript', 'python']
  9.   paths: {
  10.     languages: 'imported/',
  11.   }
  12. })
  13. ```

Specify how to load WebAssembly


Shiki makes use of VS Code Oniguruma for tokenization (see Shiki's Architecture for more information). Oniguruma is written in C and compiled to WebAssembly. Which means that the WebAssembly file needs to be loaded.

For Node.js environments the WASM file is automatically loaded, for browser environments you can specify how to load the WASM file.

If used, setWASM must be invoked before getHighlighter is called.


If you make use of a reverse proxy like nginx or traefik you may have to specify in its configuration the MIME type for wasm files.


Use the default loader: (applies to Node.js and browser environments)

  1. ```js
  2. import { getHighlighter } from 'shiki'

  3. // With the default settings, the WASM file will be loaded from `/dist/´.
  4. const highlighter = await getHighlighter({
  5.   theme: 'nord',
  6.   langs: ['javascript', 'python']
  7. })
  8. ```

Use the default loader, and override the default path for the WASM file:

  1. ```js
  2. import { getHighlighter } from 'shiki'

  3. const highlighter = await getHighlighter({
  4.   theme: 'nord',
  5.   langs: ['javascript', 'python'],
  6.   paths: {
  7.     wasm: 'your/path/' // If you use `setCDN`, this path will be relative to the CDN root.
  8.   }
  9. })
  10. ```

Load the WASM file yourself, and provide a Response object:

  1. ```js
  2. import { getHighlighter, setWasm } from 'shiki'

  3. // It is recommended to use a Response object. Oniguruma will then use WebAssembly.instantiateStreaming(), which
  4. // means it can start parsing the file while it's still downloading.
  5. const wasmResponse = await fetch('/your/path/onig.wasm')
  6. setWasm(wasmResponse)

  7. const highlighter = await getHighlighter({
  8.   theme: 'nord',
  9.   langs: ['javascript', 'python']
  10. })
  11. ```

Load the WASM file yourself, and provide an ArrayBuffer:

  1. ```js
  2. import { getHighlighter, setWasm } from 'shiki'

  3. // It is recommended to use a Response object. Oniguruma will then use WebAssembly.instantiateStreaming(), which
  4. // means it can start parsing the file while it's still downloading.
  5. //
  6. // With an ArrayBuffer this is not possible, so the file will be fully downloaded before parsing starts.
  7. const wasmBuffer = await fetch('/your/path/onig.wasm').then(res => res.arrayBuffer())
  8. setWasm(wasmBuffer)

  9. const highlighter = await getHighlighter({
  10.   theme: 'nord',
  11.   langs: ['javascript', 'python']
  12. })
  13. ```

Custom rendering of code blocks


The default functionality of rendering code blocks would use codeToHtml from the Highlighter instance.

If you want to render the tokens into a code yourself, Shiki exposes two key methods to do that.

- codeToThemedTokens takes a code string and a language id and returns an array of tokens. A token represents a single part of the code, for example a keyword, a string, a comment, etc.
- renderToHTML takes an array of tokens and returns an HTML string that represents the provided code.

  1. ```js
  2. import shiki, { getHighlighter } from 'shiki'

  3. const highlighter = await getHighlighter({
  4.   theme: 'nord',
  5.   langs: ['javascript', 'python']
  6. })

  7. const code = `console.log("Here is your code.");`

  8. // This will return an array of tokens for the provided code.
  9. // A token represents a single part of the code, for example a keyword, a string, a comment, etc.
  10. const tokens = highlighter.codeToThemedTokens(code, 'javascript')

  11. // This will return an HTML string that represents the provided code.
  12. const html = shiki.renderToHTML(tokens)
  13. ```

Alternatively you can add to renderToHTML the desired element shape for pre, code, line (span), and token (span), and override the theme colors for background and foreground.

For more about that, or to build your own renderer, check out the implementation in shiki.

  1. ```js
  2. const html = shiki.renderToHTML(tokens, {
  3.   fg: highlighter.getForegroundColor('nord'), // Set a specific foreground color.
  4.   bg: highlighter.getBackgroundColor('nord'), // Set a specific background color.
  5.   // Specified elements override the default elements.
  6.   elements: {
  7.     pre({ className, style, children }) {
  8.       return `${children}`
  9.     },
  10.     code({ className, style, children }) {
  11.       return `${children}`
  12.     }
  13.   }
  14. })
  15. ```

There are multiple Shiki renderers out there in the wild. You can see a bunch linked below.

Multiple Shiki instances on the same page


If you want to use Shiki in a browser multiple times on the same page, you should make sure that there is only a single Highlighter instance. Use cases are for example a markdown editor and a preview, or wrapping Shiki within a web component.

Common scenarios are to use the Observable pattern, or to use a singleton pattern.

In both cases you've to ensure that the Highlighter instance is only created once, and that it is bootstrapped asynchronously before calling any of the exposed functions.

Seen


- Shiki Docs: https://shiki.matsu.io
- Interactive Demo on CodeSandbox (with Next.js): https://codesandbox.io/s/shiki-next-js-cir0y
- Markdown Preview Shiki Highlighting, a VS Code plugin to use Shiki's highlighting in Markdown preview.
- Torchlight, a syntax highlighting API powered by the Shiki tokenizer.
- CodeChalk, A neat terminal code highlighting tool powered by Shiki tokenizer and Chalk.
- Code Hike, a collection of components and mods for MDX codeblocks.

Custom Renderers


- Shiki Twoslash: https://shikijs.github.io/twoslash/ Playground: https://shikijs.github.io/twoslash/playground
- Shiki LaTeX, a Shiki renderer for LaTeX. Compatible with minted, replacing Pygments.
- Scarbon, an elegant code renderer made using Shiki for highlighting, Prettier for formatting, and Canvas for rendering
- VPC Shiki, Shiki codeblocks on Discord. Powered by Powercord
- shiki-renderer-canvas, a Canvas renderer for Shiki that works in Node.js and the browser
- shiki-renderer-pdf, a Shiki renderer for PDFs. Used in pdfc to compile your source code to PDFs.

Contributing



Credits


- Shiki uses vscode-oniguruma
- A lot of code is based on vscode-textmate

Sponsorship


If you find Shiki useful, please consider sponsoring my Open Source development. Thank you 🙏

https://github.com/sponsors/octref

License


MIT © Pine Wu