@diglot/core

中文文档
0.2.2发布于 2026/11/08MITTypeScriptES Module构建来源已验证近 30 天下载 4097

Segment-level, human-first translation for markdown documentation

原始使用文档
文档目录7 节
  1. Segments
  2. Validation
  3. Apply events
  4. Changes
  5. Records
  6. Review
  7. Configuration

@diglot/core

The parser-neutral core of diglot: the segment model, placeholder form and hashing, translation validation, apply helpers, segment and sentence diffs, translation records, and policy and glossary types. It has no markdown parser dependency; adapters such as @diglot/comark and @diglot/mdast produce segments for it. It imports no Node built-ins, so it runs in browsers and edge runtimes as well as Node.

pnpm add @diglot/core

Segments

An adapter converts each translatable unit into a SegmentDraft with inline content in a small IR (text, strong / emphasis / delete, link with its opening and closing markdown, and atomic spans with their markdown source). buildSegment turns it into a Segment:

import { buildSegment } from '@diglot/core'

const segment = buildSegment({
  kind: 'paragraph',
  content: [
    { type: 'text', value: 'Nuxt is powered by ' },
    { type: 'link', open: '[', close: '](https://nitro.build)', children: [{ type: 'text', value: 'Nitro' }] },
    { type: 'text', value: '.' },
  ],
  context: { headingTrail: [], containers: [] },
  position: { startLine: 1, endLine: 1, precision: 'node' },
}, { file: 'intro.md', index: 0 })

segment.skeleton // 'Nuxt is powered by {0}Nitro{/0}.'
segment.hash // sha256 of the skeleton, truncated to 16 hex characters

{n} stands for an atomic span (inline code, image, component, HTML, binding, hard break) and {n}...{/n} wraps the text of a link. Whitespace is collapsed and markdown punctuation in text is escaped, so the skeleton is stable across re-wrapping and protected span edits. Plain segments (props, frontmatter, labels, code comments) hash apart from markdown segments with the same skeleton, so each translation validates under one set of rules. A policy can add a context salt to the hash of ambiguous short strings.

prepareSegment takes the same arguments and returns { segment, nodes } (or undefined for units without content, like buildSegment), where nodes[id] is the link or atomic node behind placeholder id. Adapters use it to rebuild a translated unit from the current source nodes.

Validation

import { validate } from '@diglot/core'

validate('Nuxt est propulsé par {0}Nitro{/0}.', segment).ok // true
validate('Nuxt [Vite](https://example.com)', segment).errors // link syntax, missing placeholder

A translation may contain text, emphasis markers and the placeholders of its source. Repeated code placeholders are a warning. Plain segments (props, frontmatter, labels, code comments) are single-line and may not add markup; code comments may not contain comment terminators.

validate(translation, segment, { style, maxLength }) also checks length: with a locale's style.lengthRatio, a translation of a source of 25 characters or more whose lengthRatio (characters outside placeholders) falls outside the band gets a length warning, and one longer than maxLength gets a too-long error. maxTranslationLength(source) is the limit editRecord applies by default: four times the source plus 100 characters, at most 8000.

resolveTranslation(segment, translations) looks up a translation by hash, validates it and re-injects the protected spans of the current source. Translations are skeletons or TranslationRecords, in a map keyed by hash or returned by a lookup function. parseSkeleton turns a validated skeleton into a tree for adapters that apply translations to an AST. pseudoTranslate produces a valid pseudo-locale translation of any skeleton.

Apply events

The adapters' apply functions accept onSegment, called with an ApplySegmentEvent for each segment in extraction order:

  • segment: the source segment, with its hash and words
  • outcome: translated, missing (no translation), rejected (a reviewer rejected the translation or it failed validation; errors lists why) or skipped (no translation and nothing to translate). Segments that are not translated keep their source text
  • record: the record the lookup returned, if any
  • status: what a reader sees, as from recordStatus. Translations given as bare skeletons have no status
  • target: for block-level segments, a handle whose setAttribute(name, value) sets an attribute on the output node; null for frontmatter, props, image alts, code labels and code comments

wordsByStatus(events) sums segment words by status, for per-page coverage. createSegmentEvent builds an event for adapters.

Changes

import { diffSegments, sentenceDiff } from '@diglot/core'

const { unchanged, added, removed, modified } = diffSegments(before, after)
for (const { before, after, similarity, ambiguous } of modified)
  console.log(sentenceDiff(before.skeleton, after.skeleton).changed)

diffSegments compares the segments of one file: equal hashes are unchanged, and the remaining removed and added segments are paired by skeleton similarity (threshold 0.5 by default) to find each modified segment's predecessor.

Records

TranslationRecord is one translation of one source segment into one locale: hash, source, text, origin (mt, tm, human), review (none, edited, verified, rejected), derived (fresh, carry, update), and optionally prev, salt, mt (model, prompt version and the unedited candidate), by, verifiedBy, rejectedBy and updatedAt.

A reviewer rejects a translation by setting review: 'rejected' and rejectedBy: { by, at } on its record. The text is kept for reviewers and metrics, but readers see the source text, and machine translation leaves the record alone. An edit (review: 'edited', without rejectedBy) or a new source hash replaces it.

  • parseRecord(value) checks the shape and throws RecordError. verifiedBy is required when review is verified; rejectedBy is required when review is rejected, and allowed only then
  • createRecord({ segment, text, origin }) fills identity fields
  • recordStatus(record, segment) returns what a reader sees: untranslated, machine, pending-review, edited, verified or rejected (a rejected record, or a text that fails validation against segment). A record carried or updated from a predecessor is pending-review until someone edits or verifies it, whatever its origin
  • editRatio(record) measures how much of the machine candidate a reviewer changed

Review

editRecord, verifyRecord and rejectRecord take the current record (or undefined) and return { ok: true, record, warnings } with the next record, or { ok: false, code, message, errors }:

import { editRecord, verifyRecord } from '@diglot/core'

const result = verifyRecord(record, { segment, by: 'danielroe', expected: textTheReviewerSaw })
if (!result.ok)
  throw new Error(result.message) // result.code: 'stale', 'rejected', ...
  • editRecord(record, { segment, text, by, style, maxLength }) writes a human edit (origin: 'human', review: 'edited'), keeping mt, prev and derived and dropping review stamps. It refuses a text that fails validation or is longer than maxLength (invalid; maxTranslationLength of the source by default, false for none) and an unchanged text (unchanged), unless the record is rejected. A record whose hash is not the segment's is refused as mismatch
  • verifyRecord(record, { segment, by }) sets review: 'verified' and verifiedBy. It refuses a missing record (no-record), a rejected one (rejected: an edit replaces a rejection), a text that fails validation against segment (invalid) and a verified record (unchanged)
  • rejectRecord(record, { by }) sets review: 'rejected' and rejectedBy and drops verifiedBy. It refuses a missing or rejected record

Each takes at (defaults to now) and expected, the text the reviewer saw: a record whose text differs is refused as stale, and null expects no record.

Configuration

snapshotPolicy(policy) returns the serialisable part of a policy with its policyFingerprint, for .diglot/policy.json. restorePolicy(snapshot, { salt, codeLabel }) rebuilds the policy for runtime extract and apply; functions cannot be serialised, so a snapshot lists custom ones in custom, and restoring throws unless they are passed in and the result has the same fingerprint. Fingerprints compare functions by their results on a fixed set of inputs.

defineConfig types a diglot.config.ts; defaultPolicy lists the prose props, layout containers and frontmatter paths extracted by default, and starlightPolicy lists those of Starlight sites (a dotted path such as navigation.title matches a literal navigation.title key or a nested navigation: { title }, and translations are written back to the key that was read); Glossary describes a per-locale glossary.yml and LocaleStyle a per-locale style.yml; MtConfig holds the machine translation settings; LocaleState describes a per-locale locale.yml with the machine translation switch and the review metrics behind it.

版本与发布记录

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

版本发布日期距今
0.2.2latest2026/11/083 天前
0.2.12026/11/066 天前
0.2.02026/11/057 天前
0.1.42026/11/048 天前
0.1.32026/11/038 天前
0.1.22026/11/039 天前

关于 @diglot/core 的常见问题

@diglot/core 是什么?

Segment-level, human-first translation for markdown documentation

如何安装 @diglot/core?

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

@diglot/core 支持 TypeScript 吗?

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

@diglot/core 支持 ESM 还是 CommonJS?

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

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

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

@diglot/core 可以商用吗?

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

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

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

@diglot/core 还在维护吗?

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