A Beautiful VS Code Style Syntax Highlighter | shiki.js

Shiki is a beautiful Syntax Highlighter that uses monstrous TextMate grammar rules for a textbook for chains. The rules of the format are used to adjust the strings and colors of icons that contain VS icon topics. In short, HTML Shiki generates just like your code in VS icon and works great in your fixed website generator.

Shiki uses TextMate’s grammar to encode strings, and colors the tokens via VS Code themes. In short, Shiki generates HTML that looks exactly like your code in VS Code and works great in your static website builder (or your dynamic website).

There is no RegEx meant to be preserved, no CSS meant to be preserved, and no HTML meant to be preserved. And as your favorite languages and themes in VS Code evolve – syntax highlighting will, too.

Custom display of code blocks

You will use the default function to display the codeToHtml code blocks from the Highlighter instance.

If you want to convert emoticons into an emoticon yourself, Shiki offers two main ways to do so.

  • codeToThemedTokens takes a string of code and a language identifier and returns an array of tokens. A token is one piece of code, for example, a keyword, string, comment, etc.
  • renderToHTML takes an array of tokens and returns an HTML string representing the rendered code.

Multiple Shiki instances on the same page

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

Common scenarios are using the observed pattern, or using a singleton pattern.

In either case, you must ensure that the Highlighter is only instantiated once and that it is booted asynchronously before any of the exposed functions are called.

Must Read: JavaScript Library To Syntax Highlighting Code In Notepad++ | CodeHighlighter

How to make use of it:

1. Install the shiki.js package deal managers.

# Yarn
$ yarn add shiki

# NPM
$ npm install shiki --save

2. Import the shiki.js library.

const shiki = require('shiki')
// or
<script src="dist/index.iife.js"></script>

3. Create a container to carry the Syntax Highlighter.

<div id="output"></div>

4. Initialize the shiki.js and insert the source code to be highlighted.

shiki
.getHighlighter({
  theme: 'nord'
})
.then(highlighter => {
  // codeToHtml(code, language)
  const code = highlighter.codeToHtml(`console.log('shiki');`, 'js')
  document.getElementById('output').innerHTML = code
})

Seen

See Demo And Download

vscode-change-syntax-highlighting

Official Website(shikijs): Click Here

This superior jQuery/javascript plugin is developed by shikijs. For extra Advanced Usage, please go to the official website.

Related Posts

Google-Translate-Dropdown-Customize-With-Country-Flag

Google Translate Dropdown Customize With Country Flag | GT API

Flag google translates jQuery text that takes advantage of the Google Cloud Translation API to translate web content between languages by selecting a country from the dropdown…

Bootstrap-Fileinput

HTML 5 File Input Optimized for Bootstrap 4.x./3.x with File Preview | Bootstrap Fileinput

bootstrap-fileinput is an improved HTML 5 file input  Bootstrap 5.x, 4.x and 3.x with file preview for different files, provides multiple selections, resumable section uploads, and more….

HStack-and-VStack-in-CSS

CSS Layout Components Horizontal/Vertical Stack | HStack and VStack

HStack and VStack in CSS – CSS layout components that (basically) stack anything horizontally and vertically. A pure CSS library that makes it easy to stack elements…

Floating-Whatsapp-Chat-Button

How to Add Floating Whatsapp Chat Button In HTML | venom-button

Venom Button is a very simple plugin for the jQuery floating WhatsApp button. Adds a floating button to your site that calls WhatsApp Click to Chat API. It will automatically start the WhatsApp…

Data-Table-Generator-Tabulator

Interactive Data Table Generator with JS/jQuery and JSON | Tabulator

Tabulator allows you to create interactive tables in seconds from any HTML Table, JavaScript array, AJAX data source, or JSON format data. Just include the library in your…

alert-confirm-prompt-attention-js

Simple Alert, Confirm, Prompt Popup Using Vanilla JavaScript Library | attention.js

JavaScript provides various built-in functionality to display popup messages for different purposes. Attention JS is a vanillaJS plugin used to create a custom alert, confirm, or Prompt…

Leave a Reply

Your email address will not be published. Required fields are marked *