ADF Schema 生成器
这个软件包提供了一个简单的DSL(域特定语言),用于定义 schema, 可转换成多种输出格式,与 ADF 合作需要这些格式(阿特拉斯文档格式):
- 民主同盟军JSON Schema
- 普罗塞米尔斯克马
- 校验光谱
使用量
DSL 服务器
软件包导出一些可用于定义方案功能:
节点和标记 :
adfNode– 定义一个新的 ADF 节点adfMark- 定义一个新的 ADF 标记
组 :
adfNodeGroup– 定义一个新的 ADF 节点组adfMarkGroup— 定义一个新的 ADF 标记组
内容表达式 :
$or- 创建允许任何指定内容项目的内容表达式$onePlus- 创建允许一个或多个指定内容项目的内容表达式$zeroPlus- 创建允许零或更多指定内容项目的内容表达式$range- 创建允许限制儿童数量的内容表达式
节点和变量:
经常需要定义一个略为不同的节点版本. 例如:
- 在节点上启用/禁用某些属性的特征旗
- 更严格地验证特定用途的大小写,比如允许在顶级节点上留下某些标记, 并且不允许它们在同一节点的嵌入变体上.
备选案文允许这种使用.
每个变体浅显地超过基点节点光谱.
然后可以通过 node.use('variant_name') 方法.
在节点定义中添加新的变体,默认不会影响输出计划.
该变体必须明确用于计划定义中,通过 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.
示例
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))],
});
第0阶段
阶段0 schema是一款实验性的超级集全 schema. 它被用来测试新的特征,不能保证稳定.
有两种方法可以定义一个阶段0节点.
1. 使用内线 stage0 节点光谱覆盖 :
内入 stage0 spec是碱性节点光谱的浅接.
例如,这会产生两个PM节点: paragraph 财务报告和审定财务报表 paragraphStage0。 。 。 .
const paragraph = adfNode('paragraph').define({
content: [$zeroPlus($or(text))],
stage0: {
attrs: {
alignment: {
type: 'enum',
values: ['start', 'end', 'center', 'justify'],
default: 'start',
},
},
},
});
使用内置相位 0 覆盖是修改/扩展节点光谱的首选方式 它简化了未来的晋升 全面计划.
2. 将整个节点标为第0阶段
标出节点为 stage0: true 将仅输出阶段 0 节点光谱。 它不会出现在完整的Json计划.
将整个节点标为舞台0是将新的节点引入ADF schema的首选方式.
const paragraph = adfNode('paragraph').define({
content: [$zeroPlus($or(text))],
stage0: true,
});
轨迹
用于生成输出格式的图案:
Traverse接受 ADF DSL 树的根节点和一个访问对象. 访问器是一种常见于树向转算法的图案. 它允许将曲面逻辑与实际处理逻辑相分离.
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(' | ');
},
});
术语
@DSLCompatibilityException:此注释标志着变压器在DSL转换过程中所做的特殊情况,这些变压器处理与特定目标计划的临时相容问题. 随着DSL逐渐包括这些特例,它充当追踪机制,以查明、确定并消除这些特例.
出版
版本突起和出版是通过变换器工作流程进行管理的.