@fuzdev/fuz_code

中文文档
0.59.0发布于 2026/11/090BSDTypeScriptES Module近 30 天下载 2255

syntax styler for TypeScript, Svelte, Markdown, and more

原始使用文档
文档目录7 节
  1. Usage
  2. Modules
  3. Languages
  4. Docs
  5. Experimental highlight support
  6. Contributing
  7. License 🐦

@fuzdev/fuz_code

a friendly pink spider facing you

syntax styler for TypeScript, Svelte, Markdown, and more 🎨

code.fuz.dev

fuz_code is a syntax styler: it turns source code into HTML with token CSS classes, or ranges for the CSS Custom Highlight API. It originated as a fork of Prism by Lea Verou and has since been rewritten: each language has a lexer that scans without regular expressions and emits tokens as a flat stream in a typed array, for high performance. The flat token stream is an idea borrowed from pngwn, the author of Twinkleplop (a featureful alternative to fuz_code).

Twinkleplop is the broader project: it compiles declarative grammars, covers more languages, and ships themes, annotations, and markdown integrations. fuz_code is narrower, with a lexer written as ordinary code for each of a small set of languages, and a few Svelte components.

Features:

  • a small, explicit API — stylize(code, lang) returns HTML, lex(code, lang) returns the flat token stream
  • two outputs: HTML with .token_* classes for any framework, or ranges for the CSS Custom Highlight API (experimental)
  • the lexers and styler have no dependencies
  • lexers accept any input, including incomplete code mid-edit, and run in linear time
  • built-in languages listed below, extensible by writing a lexer

Optional integrations:

Compared to Shiki, fuz_code is one to two orders of magnitude faster for runtime usage, about 60x at the geometric mean across languages in syntax-highlighter-bench (repo), which also covers Prism and Twinkleplop. fuz_code runs one single-pass lexer per language rather than the Oniguruma regexp engine that TextMate grammars require. Shiki targets build-time use and supports far more languages and themes.

Usage

npm i -D @fuzdev/fuz_code
<script lang="ts">
    import Code from '@fuzdev/fuz_code/Code.svelte';
</script>

<!-- defaults to Svelte -->
<Code content={svelte_code} />
<!-- select a lang -->
<Code content={ts_code} lang="ts" />
import { syntax_styler_global } from '@fuzdev/fuz_code/syntax_styler_global.ts';

// Generate HTML with syntax styling
const html = syntax_styler_global.stylize(code, 'ts');

// Get the flat event stream for custom processing
const lexed = syntax_styler_global.lex(code, 'ts');

Themes are CSS files and work with any framework.

With SvelteKit:

// +layout.svelte
import '@fuzdev/fuz_code/theme.css';

The default theme depends on fuz_css for color-scheme awareness. See the fuz_css usage docs for setup.

If you're not using fuz_css, import theme_variables.css alongside theme.css:

// Without fuz_css:
import '@fuzdev/fuz_code/theme.css';
import '@fuzdev/fuz_code/theme_variables.css';

Modules

See src/lib for the full set of modules.

Languages

Registered by default in syntax_styler_global:

  • markup (html, svg, mathml)
  • xml (rss, atom, ssml)
  • svelte
  • md (markdown)
  • ts (typescript, js, javascript — JS is a syntactic subset)
  • css
  • json — accepts comments (JSONC)
  • sh (bash, shell — the POSIX/bash family)
  • rust (rs)

Add a language by writing a SyntaxLang lexer and registering it with add_lang — see the existing lexer_*.ts modules.

Docs

Issues and questions are welcome.

Experimental highlight support

For browsers that support the CSS Custom Highlight API, fuz_code provides an experimental component that can use native browser highlighting as an alternative to HTML generation.

Browser support is limited, and the API can't apply layout-affecting styles — bold, italics, font sizes — so themes that use them render differently. The standard Code.svelte component using HTML generation is recommended for most use cases.

<script lang="ts">
    import CodeHighlight from '@fuzdev/fuz_code/CodeHighlight.svelte';
</script>

<!-- auto-detect and use CSS Highlight API when available -->
<CodeHighlight content={code} mode="auto" />
<!-- force HTML mode -->
<CodeHighlight content={code} mode="html" />
<!-- force ranges mode (requires browser support) -->
<CodeHighlight content={code} mode="ranges" />

When using the experimental highlight component, import the corresponding theme:

// instead of theme.css, import theme_highlight.css in +layout.svelte:
import '@fuzdev/fuz_code/theme_highlight.css';

Experimental modules:

Contributing

fuz.dev/contributing

License 🐦

Originally forked from Prism (prismjs.com) by Lea Verou — with the Svelte support originally based on prism-svelte by @pngwn.

0BSD

版本与发布记录

@fuzdev/fuz_code 共发布 31 个版本,首次发布于 2025/12/07;过去 12 个月发布了 31 个版本。

版本发布日期距今
0.59.0latest2026/11/096 天前
0.58.02026/11/087 天前
0.57.02026/11/088 天前
0.56.02026/10/2917 天前
0.55.02026/10/2918 天前
0.54.02026/10/1829 天前

关于 @fuzdev/fuz_code 的常见问题

@fuzdev/fuz_code 是什么?

syntax styler for TypeScript, Svelte, Markdown, and more

如何安装 @fuzdev/fuz_code?

在项目目录运行 npm install @fuzdev/fuz_code,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:esm-env(^1)、@fuzdev/fuz_util(>=0.81.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fuzdev/fuz_code 支持 TypeScript 吗?

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

@fuzdev/fuz_code 支持 ESM 还是 CommonJS?

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

@fuzdev/fuz_code 需要什么版本的 Node.js?

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

@fuzdev/fuz_code 可以商用吗?

@fuzdev/fuz_code 使用 0BSD 许可。几乎没有限制:可自由使用、修改和分发,连保留版权声明也不要求。以上为概括,不构成法律意见,请以许可原文为准。

@fuzdev/fuz_code 的下载量有多少?

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

@fuzdev/fuz_code 还在维护吗?

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