Convert Figma logo to code with AI

shikijs logoshiki

A beautiful yet powerful syntax highlighter

9,701
353
9,701
32

Top Related Projects

JavaScript syntax highlighter with language auto-detection and zero dependencies.

12,288

Lightweight, robust, elegant syntax highlighting.

In-browser code editor (version 5, legacy)

A browser based code editor

3,330

A pure Ruby code highlighter that is compatible with Pygments

Quick Overview

Shiki is a syntax highlighter for code snippets, powered by the TextMate grammar. It provides accurate syntax highlighting for a wide range of programming languages and can generate both HTML and SVG output. Shiki is designed to be fast, flexible, and easy to integrate into various applications.

Pros

  • Supports a vast array of programming languages out of the box
  • Produces highly accurate syntax highlighting using TextMate grammars
  • Offers both HTML and SVG output options for versatile integration
  • Customizable themes and easy to extend with new languages

Cons

  • Larger bundle size compared to some simpler syntax highlighters
  • Requires loading of language grammars, which can impact initial load time
  • Limited styling options compared to some more complex highlighting libraries
  • May require additional setup for server-side rendering in some frameworks

Code Examples

  1. Basic usage to highlight JavaScript code:
import { getHighlighter } from 'shiki'

const highlighter = await getHighlighter({ theme: 'nord' })
const code = 'console.log("Hello, World!")'
const html = highlighter.codeToHtml(code, { lang: 'js' })
  1. Highlighting with a custom theme:
import { getHighlighter } from 'shiki'

const highlighter = await getHighlighter({
  theme: {
    name: 'my-theme',
    settings: [
      {
        scope: ['comment'],
        settings: { foreground: '#888888' }
      }
    ]
  }
})

const code = '// This is a comment'
const html = highlighter.codeToHtml(code, { lang: 'js' })
  1. Generating SVG output:
import { getHighlighter } from 'shiki'

const highlighter = await getHighlighter({ theme: 'github-dark' })
const code = 'def greet(name):\n    print(f"Hello, {name}!")'
const svg = highlighter.codeToSvg(code, { lang: 'python' })

Getting Started

To use Shiki in your project, first install it via npm:

npm install shiki

Then, you can use it in your JavaScript or TypeScript code:

import { getHighlighter } from 'shiki'

async function highlightCode() {
  const highlighter = await getHighlighter({ theme: 'monokai' })
  const code = 'const greeting = "Hello, Shiki!";'
  const html = highlighter.codeToHtml(code, { lang: 'js' })
  document.getElementById('output').innerHTML = html
}

highlightCode()

This example sets up Shiki with the Monokai theme, highlights a JavaScript code snippet, and inserts the resulting HTML into a DOM element with the id 'output'.

Competitor Comparisons

JavaScript syntax highlighter with language auto-detection and zero dependencies.

Pros of highlight.js

  • Lightweight and fast, with a smaller bundle size
  • Supports a wide range of programming languages out of the box
  • Easy to integrate and use with minimal configuration

Cons of highlight.js

  • Less accurate syntax highlighting for complex language constructs
  • Limited customization options for themes and styles
  • Relies on regex-based parsing, which can lead to inconsistencies

Code Comparison

highlight.js:

<pre><code class="language-javascript">
function greet(name) {
  console.log(`Hello, ${name}!`);
}
</code></pre>

Shiki:

const shiki = require('shiki')

shiki.getHighlighter({
  theme: 'nord'
}).then(highlighter => {
  console.log(highlighter.codeToHtml(`console.log('shiki');`, { lang: 'js' }))
})

highlight.js uses a simpler approach with HTML classes, while Shiki requires more setup but offers more precise highlighting. highlight.js is easier to implement for basic use cases, whereas Shiki provides more accurate and customizable syntax highlighting at the cost of increased complexity and bundle size.

12,288

Lightweight, robust, elegant syntax highlighting.

Pros of Prism

  • Lightweight and fast, with a smaller footprint
  • Extensive language support out-of-the-box
  • Large ecosystem of plugins and themes

Cons of Prism

  • Less accurate syntax highlighting for complex languages
  • Requires manual updates for new language features
  • Limited customization options for highlighting rules

Code Comparison

Prism:

Prism.highlightAll();

Shiki:

const shiki = require('shiki');

shiki.getHighlighter({
  theme: 'nord'
}).then(highlighter => {
  const code = highlighter.codeToHtml(`console.log('hello')`, { lang: 'js' });
});

Prism offers a simpler API for basic usage, while Shiki provides more control over the highlighting process. Shiki's approach allows for more accurate syntax highlighting, especially for complex languages, but comes with a slightly more verbose setup.

Shiki uses TextMate grammars for parsing, which results in more precise and consistent highlighting across different languages. Prism, on the other hand, uses custom regular expressions for each language, which can sometimes lead to inconsistencies or inaccuracies in highlighting.

Overall, Prism is a good choice for projects requiring a lightweight solution with broad language support, while Shiki is better suited for applications demanding high-fidelity syntax highlighting and greater customization options.

In-browser code editor (version 5, legacy)

Pros of CodeMirror 5

  • Full-featured code editor with extensive customization options
  • Supports real-time editing and collaborative features
  • Large ecosystem of plugins and extensions

Cons of CodeMirror 5

  • Heavier and more complex to implement for simple syntax highlighting
  • Requires more setup and configuration for basic use cases
  • May have performance issues with very large files

Code Comparison

Shiki (basic usage):

import { getHighlighter } from 'shiki'

const highlighter = await getHighlighter({ theme: 'nord' })
const html = highlighter.codeToHtml(code, { lang: 'js' })

CodeMirror 5 (basic setup):

var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  lineNumbers: true,
  mode: "javascript"
});

Summary

Shiki is a lightweight syntax highlighter that focuses on generating static HTML with accurate syntax highlighting. It's ideal for scenarios where you need to display code snippets without editing functionality.

CodeMirror 5 is a full-fledged code editor that offers real-time editing, extensive customization, and a rich ecosystem of plugins. It's better suited for applications requiring interactive code editing and more advanced features.

Choose Shiki for simple, accurate syntax highlighting in static contexts, and CodeMirror 5 for interactive code editing and more complex use cases.

A browser based code editor

Pros of Monaco Editor

  • Full-featured code editor with advanced capabilities like IntelliSense, error checking, and refactoring
  • Supports a wide range of programming languages out-of-the-box
  • Highly customizable with extensive API for integration and extension

Cons of Monaco Editor

  • Larger bundle size and more complex setup compared to lightweight syntax highlighters
  • May be overkill for simple code highlighting use cases
  • Requires more resources and can impact performance on low-end devices

Code Comparison

Monaco Editor:

import * as monaco from 'monaco-editor';

monaco.editor.create(document.getElementById('container'), {
    value: 'console.log("Hello, world!");',
    language: 'javascript'
});

Shiki:

import { getHighlighter } from 'shiki';

const highlighter = await getHighlighter({ theme: 'nord' });
const html = highlighter.codeToHtml('console.log("Hello, world!");', { lang: 'js' });

Monaco Editor provides a full editing experience, while Shiki focuses on syntax highlighting. Monaco offers more features but requires more setup, whereas Shiki is simpler to use for basic highlighting needs.

3,330

A pure Ruby code highlighter that is compatible with Pygments

Pros of Rouge

  • Written in Ruby, making it a natural choice for Ruby-based projects
  • Extensive language support with over 100 lexers
  • Actively maintained with regular updates and contributions

Cons of Rouge

  • Generally slower performance compared to Shiki
  • Less flexible theming options
  • Limited to server-side rendering, not suitable for client-side highlighting

Code Comparison

Rouge:

require 'rouge'
formatter = Rouge::Formatters::HTML.new
lexer = Rouge::Lexers::Ruby.new
formatter.format(lexer.lex("puts 'Hello, world!'"))

Shiki:

const shiki = require('shiki')
shiki.getHighlighter({
  theme: 'nord'
}).then(highlighter => {
  console.log(highlighter.codeToHtml(`console.log('Hello, world!')`, { lang: 'js' }))
})

Rouge is a Ruby-based syntax highlighter that offers extensive language support and is well-suited for Ruby projects. It's actively maintained but may have slower performance compared to Shiki. Rouge is primarily designed for server-side rendering.

Shiki, on the other hand, is a JavaScript-based syntax highlighter that uses TextMate grammars for precise highlighting. It offers better performance and more flexible theming options. Shiki can be used for both server-side and client-side highlighting, making it more versatile for web applications.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Shiki Logo

Shiki 式

NPM version NPM downloads Codecov

A beautiful syntax highlighter based on TextMate grammar, accurate and powerful.

📚 Documentation

[!NOTE] You are viewing the v1 branch, which is a major rewrite from v0.x. Check out the Migration Guide if you are upgrading.

Read more about The Evolution of Shiki v1.0.

For legacy code, check the v0.x branch.

License

MIT

NPM DownloadsLast 30 Days