@atlaskit/adf-schema-generator

中文文档
4.0.4发布于 2026/09/17Apache-2.0TypeScriptESM + CJS近 30 天下载 73万

Generates ADF and PM schemas

原始使用文档
文档目录6 节
  1. Usage
  2. DSL
  3. Stage 0
  4. Traverse
  5. Terminology
  6. Publishing

ADF Schema Generator

This package provides a simple DSL (Domain Specific Language) for defining the schema, that can be transformed into multiple output formats that are required to work with ADF (Atlassian Document Format):

  • ADF JSON Schema
  • ProseMirror Schema
  • Validator Specs

Usage

DSL

The package exports several functions that can be used to define the schema:

Nodes and Marks:

  • adfNode – defines a new ADF node
  • adfMark – defines a new ADF mark

Groups:

  • adfNodeGroup – defines a new ADF node group
  • adfMarkGroup – defines a new ADF mark group

Content Expressions:

  • $or - Create a content expression that allows any of the specified content items
  • $onePlus - Create a content expression that allows one or more of the specified content items
  • $zeroPlus - Create a content expression that allows zero or more of the specified content items
  • $range – Create a content expression that allows to limit number of children

Nodes and Variants:

There is quite often a need to define a slightly different version of the node. E.g.:

  • A feature flag to enable/disable certain attributes on a node
  • Stricter validation for a specific use case, like allowing certain marks on a top level node, and not allowing them on a nested variant of the same node.

Variants enable this use case.

Each variant shallowly overrides the base node spec. And then can be used via node.use('variant_name') method.

Adding new variant to a node definition doesn't, by default, affect the output schemas. The variant must be explicitly used in the schema definition via node.use('variant_name').

const paragraph = adfNode('paragraph')
  .define({
    group: blockGroup,
    content: [$zeroPlus($or(text))],
  })
  // Defines a variant of the node called 'with-attrs'
  .variant('with-attrs', {
    attrs: {
      alignment: {
        type: 'enum',
        values: ['start', 'end', 'center', 'justify'],
        default: 'start',
      },
    },
  });

// paragraphWithAttrs now references a new instance of the ADFNode with the 'with-attrs' variant applied.
const paragraphWithAttrs = paragraph.use('with-attrs');
const name = paragraphWithAttrs.getName(); // paragraph_with-attrs_node (name includes node type and variant name)

// Both base and a variant can be used in the schema definition, even as children of the same node:
const doc = adfNode('doc').define({
  root: true,
  content: [$onePlus($or(paragraph, paragraphWithAttrs))],
  //                                ^ Variant is explicitly used here
});

// This is similar to a union type in TypeScript, where you can have a type that is either one type or another.

Example

import {
  adfNode,
  adfMark,
  adfNodeGroup,
  $or,
  $onePlus,
  $zeroPlus,
} from '@atlaskit/adf-schema-generator';

const codeMark = adfMark('code').define();

const text = adfNode('text').define({
  marks: [codeMark],
});

const paragraph = adfNode('paragraph')
  .define({
    content: [$zeroPlus($or(text))],
  })
  .variant('with-attrs', {
    attrs: {
      alignment: {
        type: 'enum',
        values: ['start', 'end', 'center', 'justify'],
        default: 'start',
      },
    },
  });

const blockGroup = adfNodeGroup('block', [paragraph]);

const doc = adfNode('doc').define({
  root: true,
  content: [$onePlus($or(blockGroup))],
});

Stage 0

Stage 0 schema is an experimental super-set of the full schema. It is used to test new features and is not guaranteed to be stable.

There are 2 ways to define a stage 0 node.

1. Using inline stage0 node spec override:

Inline stage0 spec is a shallow override of the base node spec. Which will produce, for the following example, 2 PM node specs: paragraph and paragraphStage0.

const paragraph = adfNode('paragraph').define({
  content: [$zeroPlus($or(text))],
  stage0: {
    attrs: {
      alignment: {
        type: 'enum',
        values: ['start', 'end', 'center', 'justify'],
        default: 'start',
      },
    },
  },
});

Using inline stage0 override is a preferred way to modify/extend the node spec it simplifies future promotion to the full schema.

2. Marking the whole node as stage 0

Marking node as stage0: true will only output the stage 0 node spec. It will not appear in full json schema. Marking the whole node as stage0 is the preferred way to introduce new nodes to ADF schema.

const paragraph = adfNode('paragraph').define({
  content: [$zeroPlus($or(text))],
  stage0: true,
});

Traverse

The schema can be traversed to generate the output formats:

Traverse accepts a root node of the ADF DSL tree and a visitor object. Visitor is a pattern that is commonly used in tree traversal algorithms. It allows to separate the traversal logic from the actual processing logic.

import { traverse } from '@atlaskit/adf-schema-generator';

traverse(doc, {
  node: (node, variant, children) => {
    return node.type;
  },
  group: (group, children) => {
    return group.name;
  },
  $or: (children) => {
    return children.join(' | ');
  },
});

Terminology

  • @DSLCompatibilityException: This annotation marks special cases made by transformers during DSL conversion that address temporary compatibility issues with specific target schemas. It serves as a tracking mechanism to identify, prioritize, and eliminate these exceptions as the DSL evolves to encompass these special cases.

Publishing

Version bumps and publishing are managed through the changesets workflow.

版本与发布记录

@atlaskit/adf-schema-generator 共发布 108 个版本,首次发布于 2024/05/09;过去 12 个月发布了 13 个版本。

版本发布日期距今
4.0.4latest2026/09/171 个月前
4.0.32026/09/072 个月前
4.0.22026/08/182 个月前
4.0.12026/08/172 个月前
4.0.02026/08/112 个月前
3.1.12026/07/313 个月前

关于 @atlaskit/adf-schema-generator 的常见问题

@atlaskit/adf-schema-generator 是什么?

Generates ADF and PM schemas

如何安装 @atlaskit/adf-schema-generator?

在项目目录运行 npm install @atlaskit/adf-schema-generator,或使用 pnpm add / yarn add / bun add。

@atlaskit/adf-schema-generator 支持 TypeScript 吗?

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

@atlaskit/adf-schema-generator 支持 ESM 还是 CommonJS?

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

@atlaskit/adf-schema-generator 可以商用吗?

@atlaskit/adf-schema-generator 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@atlaskit/adf-schema-generator 的下载量有多少?

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

@atlaskit/adf-schema-generator 还在维护吗?

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