@dskripchenko/syntax

中文文档
0.6.0发布于 2026/11/04MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 3689

Syntax highlighting with zero runtime dependencies: 17 programming languages (JavaScript, TypeScript, Python, Go, Rust, Java, Kotlin, C#, Swift, C, C++, PHP, Ruby, Lua, Shell, PowerShell, SQL) and data formats, markup and the files around code (JSON, YAML

原始使用文档
文档目录4 节
  1. What it promises
  2. Documentation
  3. Install
  4. License

@dskripchenko/syntax

CI npm license

Syntax highlighting for programming languages, data formats and the files around code — with no runtime dependencies and no DOM. A string goes in, HTML (or tokens) comes out: in a browser, in server-side rendering, in a Node script.

import { foldRanges, highlight, highlightLines, IncrementalHighlighter, languageOf, LineHighlighter, logEntries, THEME_CSS, validate } from '@dskripchenko/syntax'

const language = languageOf('server.rs') ?? 'text' // by name, extension or `#!` line
element.innerHTML = highlight(code, language) // <span class="sx-keyword">fn</span> …

// Line by line: for line numbers, or for drawing only the lines in view.
const lines = highlightLines(code, language)

// The colours: CSS custom properties, light and dark.
style.textContent = THEME_CSS

// What is wrong with a data file, and what can be folded.
validate('{"a": 1,}', 'json') // [{ line: 1, column: 9, message: 'expected a string key', severity: 'error' }]
foldRanges(code, language) // [{ start: 3, end: 10 }, …]

// A 100 MB log: only the lines in view are highlighted.
new LineHighlighter(text, 'log').html(1000, 1040)

// Text being edited: after an edit, tokenized again only until the state is back as it was.
const h = new IncrementalHighlighter(lines, language) // lines: { lineCount, lineText(i) }
h.edit(10, 12, 15) // lines 10–12 are now 10–15
h.tokens(0, 40)
logEntries(text) // [{ start: 2, end: 9, level: 'error' }, …]

Programming languages: JavaScript, TypeScript, Python, Go, Rust, Java, Kotlin, C#, Swift, C, C++, PHP, Ruby, Lua, Shell, PowerShell, SQL.

Formats and the files around code: JSON (with comments, JSON Lines), YAML, XML (SVG, plists, project files), HTML (with its scripts and styles), CSS (SCSS, Less), TOML, INI, .env, CSV/TSV, LaTeX, Markdown (fenced blocks in their language, TeX math, ==highlights==), diff, logs (levels, times, stack traces), Dockerfile, Makefile, GraphQL, Protocol Buffers, HTTP.

Validation with the line, column and a message for JSON (strict, with comments, JSON Lines), YAML, XML, TOML, CSV/TSV, INI and .env; folding by brackets, tags, indentation or headings; big files highlighted a window at a time; text being edited highlighted again only as far as an edit changes it; logs read as entries with their level.

What it promises

  • The text comes back exactly. The tokens' texts joined give the input, byte for byte, whatever the input is.
  • Linear time. No pattern can backtrack without end and nothing nests without bound: the test suite throws pathological input (thousands of quotes, slashes, open brackets) at every language. On real code it runs at about 20 MB/s.
  • Escaped output. highlight() escapes every piece of the text; HTML in the code is shown as text.
  • Small. One module of about 32 KB gzipped (before your bundler minifies it), every language and checker included.

For code it is a highlighter, not a compiler: it reads the shape of the code — words, strings, comments, numbers, names — closely enough for reading, and reports no syntax errors in it; data formats are validated.

Documentation

Getting started: API, languages, theming, your own grammarenrudezh

Install

npm install @dskripchenko/syntax

License

MIT

版本与发布记录

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

版本发布日期距今
0.6.0latest2026/11/049 天前
0.5.02026/11/039 天前
0.4.12026/11/0310 天前
0.4.02026/11/0211 天前
0.3.42026/11/0211 天前
0.3.32026/11/0211 天前

关于 @dskripchenko/syntax 的常见问题

@dskripchenko/syntax 是什么?

Syntax highlighting with zero runtime dependencies: 17 programming languages (JavaScript, TypeScript, Python, Go, Rust, Java, Kotlin, C#, Swift, C, C++, PHP, Ruby, Lua, Shell, PowerShell, SQL) and data formats, markup and the files around code (JSON, YAML

如何安装 @dskripchenko/syntax?

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

@dskripchenko/syntax 支持 TypeScript 吗?

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

@dskripchenko/syntax 支持 ESM 还是 CommonJS?

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

@dskripchenko/syntax 需要什么版本的 Node.js?

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

@dskripchenko/syntax 可以商用吗?

@dskripchenko/syntax 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dskripchenko/syntax 的下载量有多少?

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

@dskripchenko/syntax 还在维护吗?

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