npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dimerapp/shiki

v5.0.0

Published

Render markdown codeblocks using Shiki

Downloads

145

Readme

Dimer Shiki

Render markdown codeblocks using Shiki

gh-workflow-image typescript-image npm-image license-image

Note: This package is ESM only

The @dimerapp/shiki package let you render codeblocks using the shiki code highlighter, which internally uses VSCode themes and languages for transforming the code blocks.

Following are the benefits for using this package.

  • Ability to use VSCode themes and languages.
  • Codeblocks are converted to AST during compile phase. You do not need any frontend libraries to highlight codeblocks.
  • The colors and backgrounds are picked directly from VSCode themes. Therefore, no custom CSS files are required.
  • Adds custom CSS classes for @dimerapp/markdown codeblock enhancements.

Setup

Install the package from the npm registry as follows:

npm i @dimerapp/shiki

# yarn
yarn add @dimerapp/shiki

This package has a peer dependency on @dimerapp/markdown. So make sure to install it as well.

npm i @dimerapp/markdown

# yarn
yarn add @dimerapp/markdown

Usage

Import the Shiki class and the codeblocks remark plugin to process the codeblocks inside your markdown files.

import { MarkdownFile } from '@dimerapp/markdown'
import { Shiki, codeblocks } from '@dimerapp/shiki'

const md = new MarkdownFile(content)
const shiki = new Shiki()

shiki.useTheme('nord')

/**
 * Booting shiki will load the required grammar files and themes.
 * The operation is async and must be done only once.
 */
await shiki.boot()

/**
 * Register the "codeblock" remark plugin and pass the
 * shiki instance to it.
 */
md.transform(codeblocks, shiki)

/**
 * Process markdown file
 */
await md.process()

Shiki

The Shiki class allows you to configure shiki by using a custom theme and load custom languages.

Also, a single shiki instance can be used to process multiple markdown files.

import { Shiki } from '@dimerapp/shiki'
const shiki = new Shiki()

// 1. use theme before calling boot
// 2. load custom languages before calling boot

await shiki.boot()

Using different themes

You can use different themes by calling the useTheme method. It accepts one of the following values.

| Type | Description | |------|-------------| | Shorthand name | You can define the shorthand name from one of the bundled VSCode themes. For example: shiki.useTheme('github-dark'). Here is the list of all the shorthand names. | | Path to JSON file | You can use custom themes by passing an absolute path to the theme JSON file.

shiki.useTheme('nord')
shiki.useTheme(new URL('./custom-theme.json', import.meta.url))

Using different languages

Most of the common languages are already supported by Shiki. However, you can also register custom languages by calling the loadLanguage method.

shiki.loadLanguage({
  scopeName: 'text.html.edge',
  id: 'edge',
  path: fileURLToPath(new URL('../edge.tmLanguage.json', import.meta.url)),  
})
  • scopeName: You can find the scope name inside the grammar JSON file.
  • id: The id to recognize the language. It is the same name you will use on the codeblocks inside markdown.
  • path: Absolute path to the grammar JSON file.

Codeblocks plugin

Alongside the renderer, the codeblocks export is the remark plugin you can register on the instance of MarkdownFile.

const md = new MarkdownFile(content)

/**
 * Register codeblocks plugin that uses a specific
 * rendered
 */
md.transform(codeblocks, shiki)

/**
 * Process the markdown file
 */
await md.process()