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 🙏

© 2026 – Pkg Stats / Ryan Hefner

foldkit-richtext-code-shiki

v0.2.3

Published

Shiki grammars as foldkit-richtext code tokenizers, producing syntax decorations.

Readme

foldkit-richtext-code-shiki

Shiki's grammars as tokenizers for a foldkit-richtext CodeBlock.

A code block's document holds its language and plain text; what a highlighter finds there is a set of decorations, derived for a render and discarded (RichText.codeDecorations). This package turns a Shiki highlighter the application already made into those tokenizers — one per language it loaded — so TypeScript, CSS, Python, or anything else Shiki has a grammar for highlights the way foldkit-richtext-code's JSON grammar does.

What it owns

The mapping from Shiki to the tokenizer contract, and nothing else. The application owns the highlighter: which grammars and theme it loads, and when. The document stays foldkit-richtext's, and drawing stays the renderer's (span[data-decoration=<kind>]).

Shiki's colours are not used. Its TextMate scopes are read into the same syntax-* kinds the JSON grammar names, so one stylesheet colours every language:

syntax-comment  syntax-property  syntax-string  syntax-number  syntax-keyword
syntax-operator syntax-function  syntax-type    syntax-punctuation

A scope matches by whole segments (string and string.quoted, never stringy), and the first kind that matches wins: a JSON key sits inside string and is a property, and a string's quotes are part of the string. Whitespace and plain identifiers are not decorated.

Install

pnpm add foldkit-richtext-code-shiki shiki

Highlight TypeScript

import * as RichText from 'foldkit-richtext'
import { shikiTokenizers } from 'foldkit-richtext-code-shiki'
import { createHighlighterCoreSync } from 'shiki/core'
import { createJavaScriptRegexEngine } from 'shiki/engine/javascript'
import typescript from 'shiki/langs/typescript.mjs'
import nord from 'shiki/themes/nord.mjs'

const highlighter = createHighlighterCoreSync({
  themes: [nord],
  langs: [typescript],
  engine: createJavaScriptRegexEngine(),
})

const tokenizers = shikiTokenizers(highlighter)

const decorate = (document: RichText.Document) => RichText.codeDecorations(document, tokenizers)

shikiTokenizers gives a Map from every name the highlighter loaded (typescript, ts, cts, mts) to its tokenizer, which is what codeDecorations takes. decorate is what an editor placement names, editorAt(hostId, { decorate }) in foldkit-richtext-dom, and what a read-only render passes to renderDocument.

The highlighter must be synchronous, which is why the example builds it with createHighlighterCoreSync and Shiki's JavaScript regex engine: a tokenizer is called on every render. Loading grammars is startup work that belongs to the application, before it places an editor. shikiTokenizer(highlighter, language) makes one tokenizer, and it throws when it is made if the highlighter has not loaded that language or any theme. Shiki needs a theme to tokenize at all, even though its colours are discarded.

Cost

Each tokenizer remembers the last 64 texts it read, so an editor that retokenizes every code block on every patch pays for a block only when its text changes. Reading scopes (includeExplanation) is Shiki's slow path, so a very large block still costs a full parse on each edit to it. Shiki's per-line time limit is turned off: with explanations on, a line cut short by it made Shiki throw, so a slow line now costs time rather than a failed render.

Limits

  • No asynchronous loading. A grammar loaded after an editor is placed is not picked up; build the tokenizers again and place them again.
  • The kinds are fixed. A scope outside the table above is not decorated, and there is no hook to map it; say what it should be and the table grows.