Rich text / foldkit-richtext-code-shiki
richtext-code-shiki
Built-in lexers cover simple formats like JSON, while languages such as TypeScript, CSS, or Python need full TextMate grammars. Rather than storing theme-specific colors inside your documents, you can keep plain text in the document and highlight it at render time using a synchronous Shiki highlighter.
foldkit-richtext-code-shiki adapts any synchronous Shiki highlighter you configure at startup into tokenizers for foldkit-richtext code blocks. It maps Shiki's TextMate scopes onto the same syntax-* decoration kinds used by foldkit-richtext-code, so one stylesheet paints every language. See it running in the rich-text playground.
Install
pnpm add foldkit-richtext-code-shiki shikiA first highlighter: TypeScript
Create a synchronous Shiki highlighter with the languages you want to support and convert it into a language-to-tokenizer map:
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)shikiTokenizers(highlighter) returns a Map with an entry for every loaded language and alias (typescript, ts, cts, mts). Pass that map to RichText.codeDecorations when rendering a read-only view or placing an editor:
import * as RichText from 'foldkit-richtext'
const decorate = (document: RichText.Document) => RichText.codeDecorations(document, tokenizers)Your application owns the highlighter—which grammars and themes it loads, and when. Because a tokenizer runs synchronously during render, build the highlighter at startup with createHighlighterCoreSync and Shiki's JavaScript regex engine before placing the editor. If you call shikiTokenizer(highlighter, language) for a single language, it throws right away if that language or any theme is missing (Shiki requires a theme to tokenize even though its inline colors are discarded).