@chromaglyph/node

中文文档
0.6.0发布于 2026/10/21Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2039

napi-rs bindings for chromaglyph: compile syntax highlighting into a color font, in-process

原始使用文档

@chromaglyph/node

napi-rs bindings for chromaglyph: compile syntax highlighting into an OpenType color font, in-process.

import { buildFontSafe } from '@chromaglyph/node'

const result = buildFontSafe({
  font: fontBytes,                       // static TTF / OTF (CFF) / WOFF1 / .ttc Buffer
  fontIndex: 0,                         // member of a .ttc collection (default 0)
  themes: ['dark-plus', 'light-plus'],   // builtin names or .json/.tmTheme paths
  blocks: [                              // batch any number of code blocks into ONE font
    { id: 'block-0', lang: 'rust', text: 'fn main() {}' },
    { id: 'block-1', lang: 'js', text: 'const x = 1' },
  ],
  verify: true,                          // harfrust oracle (default on)
  ligatures: true,                       // keep the font's != -> ligatures, highlighted
  format: 'woff2',                       // output container: 'woff' (default) | 'woff2'
  cacheDir: '.cache',                    // optional on-disk cache
})
// → { woff, ttf, css, themes, report, contentHash, cached }
  • buildFont throws on invalid input; buildFontSafe returns null (silent degradation for build pipelines).
  • Source fonts: static TrueType (TTF) and name-keyed CFF (OTF); WOFF1 containers and .ttc collections (fontIndex) are unwrapped, variable TrueType fonts are pinned to their default instance. CFF2 (variable OTF) and CID-keyed CFF are rejected with an explicit error.
  • format: 'woff2' wraps the output in WOFF2 (Brotli, 15–30% smaller than the default WOFF1); result.woff then carries the WOFF2 bytes. Both outline flavors ride in it (TrueType as-is, CFF null-transform). The format is part of the content hash, so both variants cache independently.
  • ligatures: true re-materializes the source font's ligatures (!=, ->) as build-time rules, so they render exactly as the font intends while staying highlighted; sites spanning multiple colors degrade to plain per-char text (colors win).
  • generic: 'rust' | 'js' | 'ts' | 'python' | 'go' | 'java' builds a font that highlights ANY text of that language (keywords, comments, strings, numbers) — blocks and semantic are ignored in that mode.
  • Artifacts are cached by content hash (font + theme sources + block texts
    • generic language + options): in-process LRU plus optional on-disk cache directory.
  • One GSUB rule set serves every theme (CPAL palettes); switching is a CSS @font-palette-values flip.
  • themes takes builtin names or paths. The builtins are shiki's tm-themes collection (64 themes, MIT/Apache-2.0 only); listThemes() returns the registry — name, displayName, type ('dark' | 'light'), license, source, foreground, background and per-scope swatches — enough to build a theme picker without compiling a font. tmThemesVersion() tells which collection version is embedded.

Build from source: pnpm --filter @chromaglyph/node build (requires Rust).

版本与发布记录

@chromaglyph/node 共发布 13 个版本,首次发布于 2026/10/16;过去 12 个月发布了 13 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.6.0latest2026/10/2119 天前
0.5.02026/10/2119 天前
0.4.12026/10/2020 天前
0.4.02026/10/1921 天前
0.3.02026/10/1822 天前
0.2.22026/10/1723 天前

关于 @chromaglyph/node 的常见问题

@chromaglyph/node 是什么?

napi-rs bindings for chromaglyph: compile syntax highlighting into a color font, in-process

如何安装 @chromaglyph/node?

在项目目录运行 npm install @chromaglyph/node,或使用 pnpm add / yarn add / bun add。

@chromaglyph/node 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@chromaglyph/node 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@chromaglyph/node 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@chromaglyph/node 可以商用吗?

@chromaglyph/node 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@chromaglyph/node 的下载量有多少?

根据 npm 官方下载统计,@chromaglyph/node 在 2026/10/10 – 2026/11/08 这 30 天内被下载 2,039 次,日均约 68 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chromaglyph/node 还在维护吗?

最新版本 0.6.0 发布于 2026/10/21(19 天前);至今共发布 13 个版本,其中过去 12 个月发布了 13 个;首次发布于 2026/10/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。