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

@stefanprobst/remark-shiki

v2.2.1

Published

Highlight code blocks in markdown with [`shiki`](https://github.com/shikijs/shiki).

Downloads

3,200

Readme

remark-shiki

Highlight code blocks in markdown with shiki.

How to install

yarn add shiki @stefanprobst/remark-shiki

How to use

This package is a remark plugin.

To highlight code blocks in markdown:

import withShiki from '@stefanprobst/remark-shiki'
import toMarkdown from 'remark-stringify'
import fromMarkdown from 'remark-parse'
import * as shiki from 'shiki'
import { unified } from 'unified'

const doc = "```js\nconst hello = 'World';\n```\n"

async function createProcessor() {
  const highlighter = await shiki.getHighlighter({ theme: 'poimandres' })

  const processor = unified()
    .use(fromMarkdown)
    .use(withShiki, { highlighter })
    .use(toMarkdown)

  return processor
}

createProcessor()
  .then((processor) => processor.process(doc))
  .then((vfile) => {
    console.log(String(vfile))
  })

When transforming to html, make sure to parse html nodes with rehype-raw (or, alternatively, consider using @stefanprobst/rehype-shiki):

import withShiki from '@stefanprobst/remark-shiki'
import fromMarkdown from 'remark-parse'
import * as shiki from 'shiki'
import { unified } from 'unified'
import toHast from 'remark-rehype'
import withHtmlInMarkdown from 'rehype-raw'
import toHtml from 'rehype-stringify'

const doc = "```js\nconst hello = 'World';\n```\n"

async function createProcessor() {
  const highlighter = await shiki.getHighlighter({ theme: 'poimandres' })

  const processor = unified()
    .use(fromMarkdown)
    .use(withShiki, { highlighter })
    .use(toHast, { allowDangerousHtml: true })
    .use(withHtmlInMarkdown)
    .use(toHtml)

  return processor
}

createProcessor()
  .then((processor) => processor.process(doc))
  .then((vfile) => {
    console.log(String(vfile))
  })

Configuration

This plugin accepts a preconfigured highlighter instance created with shiki.getHighlighter.

Theme

You can either pass one of the built-in themes as string, or load a custom theme (any TextMate/VS Code theme should work):

// const gloom = await shiki.loadTheme(path.join(process.cwd(), 'gloom.json'))
// const gloom = require('./gloom.json')
const gloom = JSON.parse(
  fs.readFileSync(path.join(process.cwd(), 'gloom.json'), 'utf-8'),
)

const highlighter = await shiki.getHighlighter({ theme: gloom })

const processor = unified()
  .use(fromMarkdown)
  .use(withShiki, { highlighter })
  .use(toMarkdown)

Supported languages

Languages which are not included in Shiki's built-in grammars can be added as TextMate grammars:

const sparql = {
  id: 'sparql',
  scopeName: 'source.sparql',
  // provide either `path` or `grammar`
  path: path.join(process.cwd(), 'sparql.tmLanguage.json'),
  // grammar: JSON.parse(
  //   fs.readFileSync(path.join(process.cwd(), "sparql.tmLanguage.json")),
  // ),
}

const highlighter = await shiki.getHighlighter({
  langs: [...shiki.BUNDLED_LANGUAGES, sparql],
})

const processor = unified()
  .use(fromMarkdown)
  .use(withShiki, { highlighter })
  .use(toMarkdown)

Note that langs will substitute the default languages. To keep the built-in grammars, concat shiki.BUNDLED_LANGUAGES.

Unknown languages

Unknown languages are ignored by default. You can set ignoreUnknownLanguage: false to throw an error when an unsupported language is encountered.

Highlighted lines

Code block metadata can be used to add css classes to specific lines. By default, a highlighted-line class will be added for line ranges defined like this:

```js {highlight: '2..3'}
function hi() {
  console.log('Hi!')
  return true
}
```

Since there is no specification or widely used convention how code block metadata should be interpreted, it is possible to provide a custom parse function:

const processor = unified()
  .use(fromMarkdown)
  .use(withShiki, {
    highlighter,
    parseMeta(meta) {
      /** Parse the meta string however you want. */
      return [
        { line: 1, classes: ['highlighted'], }
        { line: 2, classes: ['highlighted'], }
      ]
    },
  })
  .use(toMarkdown)