code-fns
raw JSON →code-fns (v0.11.0) is a JavaScript/TypeScript library for syntax highlighting and code visualization that outputs raw text and hex color tokens instead of HTML/CSS. It is designed for non-web rendering targets such as animations and videos, and is the primary highlighting engine for Motion Canvas. The library supports many languages via tree-sitter grammars, includes a diffing function to compute token-level transformations between code blocks (useful for animations), and ships TypeScript types. It is released on npm with an irregular cadence and requires modern EcmaScript features, so bundling/transpilation is left to the consumer. Key differentiators: output is language-agnostic token arrays, not DOM elements; built-in diff/morph support for transitions; no built-in theming—colors come straight from tree-sitter.
Common errors
error Error: Must call `ready` before accessing `language` ↓
await ready({ langs: ['javascript'] }); before accessing language.javascript. error TypeError: code_fns_1.default is not a function ↓
import { ready } from 'code-fns'. error Error: Unsupported language 'js'. Supported languages: ... ↓
Warnings
breaking Package requires modern EcmaScript features; no transpilation included. ↓
gotcha The `ready` function must be awaited before using `language` or `parse`. ↓
gotcha Language names are case-sensitive and must match exact property keys (e.g., `javascript` not `js`). ↓
gotcha The `diff` function returns tokens with a 'morph' property; the output format differs from `parse`. ↓
Install
npm install code-fns yarn add code-fns pnpm add code-fns Imports
- ready wrong
const ready = require('code-fns')correctimport { ready } from 'code-fns' - language wrong
import { language } from 'code-fns/tags'correctimport { language } from 'code-fns' - parse
import { parse } from 'code-fns'
Quickstart
import { ready, language, parse } from 'code-fns';
async function highlight() {
await ready({ langs: ['javascript'] });
const js = language.javascript;
const tokens = parse(js`const x = 42;`);
console.log(tokens);
// [
// { code: 'const ', color: '#c9d1d9' },
// { code: 'x', color: '#79c0ff' },
// { code: ' = ', color: '#c9d1d9' },
// { code: '42', color: '#a5d6ff' },
// { code: ';', color: '#c9d1d9' }
// ]
}
highlight().catch(console.error);