@ferriki/core

中文文档
1.3.0发布于 2026/10/27MIT OR Apache-2.0TypeScriptES Module零依赖构建来源已验证近 30 天下载 3824

native syntax highlighting with HTML and CSS-class output

原始使用文档
文档目录13 节
  1. Compile once. Start optimized.
  2. Requirements and install
  3. See a highlighted page
  4. Shiki comparisons
  5. Reuse a highlighter
  6. Languages and themes
  7. Class-based output and theme maps
  8. Transformers
  9. Build-time macros
  10. Rust
  11. Compatibility
  12. Documentation
  13. License

Ferriki

npm version License: MIT OR Apache-2.0 Node >= 22.13.0

Beautiful code. Optimization, delivered. Ship polished code examples in your docs, CMS, or site build. Ferriki runs its curated TextMate grammar catalog on a native Rust engine and keeps familiar Shiki-shaped HTML calls for Node.js. Prepare examples as static HTML, or keep one highlighter ready for a stream of blocks.

Compile once. Start optimized.

Your next process can pick up the regex work already done. Powered by Ferroni 2.7, Ferriki delivers precompiled regex programs and optimized static scanner automata with its standard grammars. They arrive from the CDN, are verified and cached on disk, and restore Eager optimizations from the first static scan.

Keep the cache between builds to reuse the same downloads across rebuilds and deployments. Each fresh process restores the prepared automata instead of rebuilding them from source. This is the default with prefiltering on; unavailable packages fall back to source, and dynamic patterns remain local.

Embedded languages reuse their own prepared packages across hosts. Markdown's prepared download falls from 25.9 MB to 1.9 MB with the same grammar coverage; Astro falls from 6.5 MB to 0.52 MB. Required dependencies load with the host, while optional code-fence languages stay optional. See the package-sharing evidence.

The first load also caches the unpacked package. Later processes skip decompression and SHA-256 for unchanged local files, then restore the cached data on demand through a small index. Only requested program and group records are read into owned buffers. File-state checks detect changes and trigger verification again.

In our local seven-grammar diagnostic, C++ tokenizer startup dropped from about 144 ms to 68 ms, with execution comparable to ordinary Eager. Startup includes restoration and the first document; network, file I/O, and complete builds are outside the timer. See the measurements and limits.

The October 25 full Node comparison measures Source Off, Auto, Eager and Prepared separately. Prepared first use takes 39% less time than Source Eager on macOS, with 5% more time on Linux. Source Off still starts fastest. See the separate-mode results for all policies and the historical memory measurements.

Requirements and install

Ferriki is ESM-only and requires Node.js 22.13.0 or newer. npm installs the optional native package for your platform: Linux x64/arm64 with glibc or musl, macOS on Apple Silicon, and Windows x64/arm64. Keep optional dependencies enabled in production installs.

npm install @ferriki/core

Grammar and theme payloads are downloaded from assets.ferriki.dev the first time they are loaded, pinned to this release, verified by SHA-256, and cached in node_modules/.cache/ferriki. For offline use, populate the cache before the build or point FERRIKI_ASSETS_BASE_URL at a mirror. Set FERRIKI_ASSETS_REMOTE=0 to use the cache only. See asset loading for cache, proxy, and certificate settings.

See a highlighted page

Save this complete ESM file as highlight.mjs:

import { writeFile } from "node:fs/promises";
import { codeToHtml } from "@ferriki/core";

const html = await codeToHtml("const answer = 42;", {
  lang: "javascript",
  theme: "github-dark",
});

await writeFile("highlighted.html", html);
console.log("Saved highlighted.html. Open it in a browser to preview your code.");

Run node highlight.mjs, then open highlighted.html. The one-shot API loads the language and theme on first use and reuses a process-local highlighter on later calls.

Shiki comparisons

The October 25 Blacksmith capture uses the repository build with Ferroni 2.7.2 and prepared standard regex packages. On its 14-file corpus, a reused highlighter produced the same HTML as Shiki/WASM and ran 3.7× faster on Linux x86-64 and 4.3× faster on macOS ARM64. With the decoded cache already populated, first use was 2.0× / 4.4× faster than Shiki/WASM on Linux/macOS. See the benchmark report for hosts, first-use timings, raw results, and measurement limits.

Memory depends on the loaded languages. In the earlier October 24 memory capture, after 600 documents with 12 languages loaded together, Ferriki's whole-process RSS was 51–137% higher than Shiki/WASM. With only the large TypeScript workload, it was 33–55% lower. Ferriki loads requested languages and their dependencies; these are separate workloads, not measurements of the entire grammar catalog. See the memory details.

Reuse a highlighter

For a CMS worker or site build, load the languages and themes you need once. Rendering with loaded assets is synchronous. Create one highlighter per worker and dispose it when the worker shuts down.

import { createHighlighter } from "@ferriki/core";

const highlighter = await createHighlighter({
  langs: ["javascript", "typescript"],
  themes: ["github-light", "github-dark"],
});

try {
  const html = highlighter.codeToHtml("const answer: number = 42;", {
    lang: "typescript",
    theme: "github-dark",
  });
  console.log(html);
} finally {
  highlighter.dispose();
}

getSingletonHighlighter() shares one highlighter per process, and the one-shot functions use it. Custom languages and themes use Shiki's TextMate registration shapes in langs and themes and are validated before they reach the native engine. Languages embedded by a grammar load with it; lazy embeddings load after an explicit loadLanguage.

For tuning repeated work and short-lived highlighters, see the prefilter guide.

Languages and themes

Ferriki maintains its own curated catalog of standard TextMate grammars, including embedded dependencies, and 65 themes. Use bundledLanguages and bundledLanguagesAlias to discover available IDs and aliases. The grammar guide covers catalog coverage and external imports. For a local grammar, run:

npm exec -- ferriki grammar fetch ./grammar.tmLanguage.json \
  --name example --output ./example.ferriki.json

The importer also accepts URLs and commit-pinned GitHub sources. Review a grammar's license before redistributing it.

Class-based output and theme maps

Use styleMode: "classes" for nested TextMate scope classes, or codeToHtmlWithCss() to return highlighted HTML with its matching CSS. An ordered themes map can switch palettes on prepared markup without highlighting again. The class-based highlighting guide shows complete HTML and stylesheet examples for custom classes and theme switching.

Transformers

Transformer and decoration callbacks run in JavaScript and receive typed token and HAST data. The native engine handles decoration ranges, token boundaries, and wrapping. Shiki's @shikijs/transformers notation helpers for focus, highlights, diffs, and word highlights work with them.

Build-time macros

@ferriki/vite turns code() and React <Code /> calls into highlighted output during a Vite 8 build. The highlighter runs during the build, and the default React component uses a small framework-free presentation module to display the prepared output. The Vite guide has a complete React setup and advanced SSR details.

Rust

The same native engine is available as the ferriki crate. See the Rust API guide.

Compatibility

Ferriki follows Shiki v4.4.3. Shiki's tests run unchanged against the native addon from a pinned mirror, each classified as supported, deferred, or out of scope; the tokenizer is checked against the complete vscode-textmate v9.3.2 oracle. Node's public output is HTML; transformer callbacks can inspect token and HAST data. Terminal ANSI input is rejected with ShikiError instead of being rendered. See the compatibility policy and Shiki migration guide.

Documentation

License

Licensed under either of MIT or Apache-2.0 at your option.

ferriki is part of the Ferramenta family — A family of Rust tools.

Siblings: ferroni — Oniguruma-compatible regex engine · ferromark — Markdown to HTML, sanitized by default · ferrolex — Spell checking for text and code · ferrocat — Translation catalog engine · ferralk — Glob matching and parallel filesystem walking · ferrugo — PDF previews for untrusted files · palamedes — Internationalization for TypeScript applications · dalo — Your team's agent setup, versioned like code · ardo — Documentation sites built with React.

版本与发布记录

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

版本发布日期距今
1.3.0latest2026/10/2718 天前
1.2.02026/10/2520 天前
1.1.22026/10/2025 天前
1.0.02026/10/111 个月前
0.14.02026/10/111 个月前
0.13.02026/10/101 个月前

其他发布通道:placeholder → 0.0.0-placeholder.0。

关于 @ferriki/core 的常见问题

@ferriki/core 是什么?

native syntax highlighting with HTML and CSS-class output

如何安装 @ferriki/core?

在项目目录运行 npm install @ferriki/core,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 ferriki,可以用 npx ferriki 直接运行。

@ferriki/core 支持 TypeScript 吗?

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

@ferriki/core 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ferriki/core 需要什么版本的 Node.js?

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

@ferriki/core 可以商用吗?

@ferriki/core 使用 MIT OR Apache-2.0 许可。可在 MIT、Apache-2.0 中任选其一遵守。选择 MIT 时,宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ferriki/core 的下载量有多少?

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

@ferriki/core 还在维护吗?

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