@eraserlabs/resolve

中文文档
0.5.2发布于 2026/11/12MITTypeScriptES Module构建来源已验证近 30 天下载 1.1万

Pure Eraser resolve engine

原始使用文档
文档目录6 节
  1. Introduction
  2. Why
  3. Usage
  4. Subsets and synchronous resolution
  5. Themes and internal paints
  6. Diagram Markdown

@eraserlabs/resolve

Introduction

@eraserlabs/resolve turns an authored diagram document into fully-resolved element data. It validates tags against a template library, applies derived-prop normalizers, sanitizes text, inlines icons, and emits a split { entities, connections } payload for @eraserlabs/render. There is no server, filesystem, or network of its own: icon loading and similar I/O are injected by the caller.

It owns the authored input grammar. A document is an object envelope — either { elements } or a split { entities, connections } with both keys required. A bare JSON array is rejected (E_ENVELOPE). Authored TypeScript shapes (AuthoredEntity, AuthoredConnection, DiagramInput) are exported from this package; @eraserlabs/protocol owns the resolved contracts.

All authored dimensions are rounded to the nearest integer during resolution: every element's x/y/width/height, each connection waypoint in points, and the labelPlacement box. The layout engine works on the integer grid, so fractional coordinates (float dust from app exports or measured round trips) are canonicalized rather than carried.

Why

Resolution has to run in Node, in the CLI, and in the browser playground with the same results. Coupling it to Chromium or a framework would force every consumer to take that dependency. A library that fails schema or template checks throws at createResolver, so no resolver exists over a vocabulary the engine has not proven it can run.

Usage

import { createResolver } from '@eraserlabs/resolve';
import { stockLibrary } from '@eraserlabs/diagram-templates';
import { stockNormalizers } from '@eraserlabs/diagram-templates/normalizers';

const resolver = await createResolver({
  library: stockLibrary,
  normalizers: stockNormalizers,
});

const result = await resolver.resolve({
  entities: [
    { tag: 'Shape', id: 'a', x: 0, y: 0 },
    { tag: 'Shape', id: 'b', x: 200, y: 0 },
  ],
  connections: [{ from: 'a', to: 'b' }],
});

createResolver compiles the library once. resolve returns { ok, entities, connections, icons, warnings, errors }; validate runs the same pipeline without inlining icon bytes. Supply iconLoader to fetch SVGs by name; onUnknownIcon is 'placeholder' or 'error'. Loaded SVGs pass an allowlist sanitizer that parses them by the browser's HTML rules: static drawing elements and local references only. Anything else rejects the icon, which then falls under onUnknownIcon, except editor metadata (Inkscape/Sodipodi, <metadata>, extra namespace declarations), filter effects, and style properties outside the allowlist, which are removed.

Portable document, component, font, and schema-vocabulary contracts live in @eraserlabs/protocol. Compatibility re-exports remain at @eraserlabs/resolve/schema. Component authoring is documented in CUSTOMIZATION.md.

Subsets and synchronous resolution

resolve(input, { partial: true }) and validate(input, { partial: true }) allow references to omitted elements. Schema errors, duplicate IDs, included targets of the wrong kind, and containment cycles remain errors. Calls are strict by default; partial results do not supply missing routing geometry.

resolveForLayout(input, options?) returns synchronously with placeholder icons, without loading icons or checking their existence. All other resolution stages are shared. It supports measurement before assets load when templates give real and placeholder icons the same bounds.

Themes and internal paints

Pass { theme: 'dark' } to either resolution method. Palette lookup and normalizers run first, then every schema-declared color is transformed once, including nested and internal colors. Authored input stays unchanged. Renderers apply the resulting theme metadata automatically; changing themes requires resolving the original input again.

Normalizers compute light-space paints. Declare new internal outputs in library.renderProps, e.g. renderProps: { Card: { fillColor: CssColor } }. These authorize template bindings and post-normalizer content/color policies without appearing in authoring schemas. Submitted internal fields are warned about and stripped before normalization. Names cannot overlap authored fields; derived colors use CssColor, not PaletteColor. Normalizers own structural validity; render declarations do not apply defaults or repeat full schema validation.

Custom CSS needs theme-aware defaults; stock CSS provides --er-text, --er-surface, --er-line, and code/syntax tokens. Fixed-color artwork is unchanged. currentColor stays inherited; use concrete colors for hue-preserving tint conversion.

Diagram Markdown

Block text supports paragraphs, headings (capped at level three), flat lists, simple quotes, rules, strikethrough, and fenced code. Labels use inline Markdown. Nested blocks, tables, images, and raw HTML are outside the presentation contract; raw HTML is escaped. These rules also affect existing documents.

Prose newlines draw line breaks; a blank line separates paragraphs. Extra and trailing empty lines are preserved. Four-space indentation remains prose. Marked handles code spans and links; fenced code preserves internal whitespace and wraps long tokens. Optional highlightCode returns text/class runs that must reproduce its input exactly; failures fall back to plain code.

版本与发布记录

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

版本发布日期距今
0.5.2latest2026/11/122 天前
0.5.12026/11/059 天前
0.5.02026/11/0311 天前
0.4.32026/10/2816 天前
0.4.22026/10/2817 天前
0.4.12026/10/2817 天前

关于 @eraserlabs/resolve 的常见问题

@eraserlabs/resolve 是什么?

Pure Eraser resolve engine

如何安装 @eraserlabs/resolve?

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

@eraserlabs/resolve 支持 TypeScript 吗?

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

@eraserlabs/resolve 支持 ESM 还是 CommonJS?

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

@eraserlabs/resolve 需要什么版本的 Node.js?

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

@eraserlabs/resolve 可以商用吗?

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

@eraserlabs/resolve 的下载量有多少?

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

@eraserlabs/resolve 还在维护吗?

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