@atlaskit/editor-json-transformer 9.2.0 → 9.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/dist/cjs/JSONTransformer-2.js +82 -0
- package/dist/cjs/SchemaStage.js +11 -0
- package/dist/cjs/index.js +7 -5
- package/dist/cjs/jsonTransformer.js +18 -260
- package/dist/cjs/sanitize/remove-marks.js +15 -10
- package/dist/cjs/sanitize/remove-non-annotation-marks.js +19 -0
- package/dist/cjs/sanitize/sanitize-node.js +6 -5
- package/dist/cjs/toJSON.js +189 -0
- package/dist/es2019/JSONTransformer-2.js +59 -0
- package/dist/es2019/SchemaStage.js +5 -0
- package/dist/es2019/index.js +4 -2
- package/dist/es2019/jsonTransformer.js +13 -236
- package/dist/es2019/sanitize/remove-marks.js +9 -9
- package/dist/es2019/sanitize/remove-non-annotation-marks.js +9 -0
- package/dist/es2019/sanitize/sanitize-node.js +2 -1
- package/dist/es2019/sanitize.js +7 -0
- package/dist/es2019/toJSON.js +176 -0
- package/dist/esm/JSONTransformer-2.js +75 -0
- package/dist/esm/SchemaStage.js +5 -0
- package/dist/esm/index.js +4 -2
- package/dist/esm/jsonTransformer.js +13 -258
- package/dist/esm/sanitize/remove-marks.js +9 -9
- package/dist/esm/sanitize/remove-non-annotation-marks.js +12 -0
- package/dist/esm/sanitize/sanitize-node.js +2 -1
- package/dist/esm/sanitize.js +7 -0
- package/dist/esm/toJSON.js +182 -0
- package/dist/types/JSONTransformer-2.d.ts +27 -0
- package/dist/types/SchemaStage.d.ts +4 -0
- package/dist/types/index.d.ts +4 -2
- package/dist/types/jsonTransformer.d.ts +10 -37
- package/dist/types/sanitize/remove-marks.d.ts +4 -7
- package/dist/types/sanitize/remove-non-annotation-marks.d.ts +8 -0
- package/dist/types/sanitize.d.ts +6 -0
- package/dist/types/toJSON.d.ts +11 -0
- package/main/package.json +4 -4
- package/markOverrideRules/package.json +4 -4
- package/package.json +4 -4
- package/sanitize/remove-marks/package.json +4 -4
- package/sanitize/sanitize-node/package.json +4 -4
- package/tsconfig.json +5 -15
- package/types/package.json +4 -4
- package/dist/cjs/entry-points/main.js +0 -12
- package/dist/cjs/entry-points/markOverrideRules.js +0 -12
- package/dist/cjs/entry-points/sanitize-remove-marks.js +0 -12
- package/dist/cjs/entry-points/sanitize-sanitize-node.js +0 -12
- package/dist/cjs/entry-points/types.js +0 -1
- package/dist/es2019/entry-points/main.js +0 -2
- package/dist/es2019/entry-points/markOverrideRules.js +0 -2
- package/dist/es2019/entry-points/sanitize-remove-marks.js +0 -2
- package/dist/es2019/entry-points/sanitize-sanitize-node.js +0 -2
- package/dist/es2019/entry-points/types.js +0 -0
- package/dist/esm/entry-points/main.js +0 -2
- package/dist/esm/entry-points/markOverrideRules.js +0 -2
- package/dist/esm/entry-points/sanitize-remove-marks.js +0 -2
- package/dist/esm/entry-points/sanitize-sanitize-node.js +0 -2
- package/dist/esm/entry-points/types.js +0 -0
- package/dist/types/entry-points/main.d.ts +0 -1
- package/dist/types/entry-points/markOverrideRules.d.ts +0 -1
- package/dist/types/entry-points/sanitize-remove-marks.d.ts +0 -1
- package/dist/types/entry-points/sanitize-sanitize-node.d.ts +0 -1
- package/dist/types/entry-points/types.d.ts +0 -1
- package/editor-json-transformer.docs.tsx +0 -43
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
var _typeof3 = require("@babel/runtime/helpers/typeof");
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.toJSON = void 0;
|
|
9
|
+
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
|
10
|
+
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
11
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
12
|
+
var _adfSchema = require("@atlaskit/adf-schema");
|
|
13
|
+
var _markOverrideRules = require("./markOverrideRules");
|
|
14
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof3(i) ? i : i + ""; }
|
|
15
|
+
function _toPrimitive(t, r) { if ("object" != _typeof3(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof3(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
16
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
17
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
18
|
+
var isType = function isType(type) {
|
|
19
|
+
return function (node) {
|
|
20
|
+
return node.type.name === type;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// Create a Set of node types that should have empty attrs removed
|
|
25
|
+
var NODES_WITH_EMPTY_ATTRS_REMOVAL = new Set(['paragraph', 'layoutSection', 'blockquote', 'nestedExpand', 'bulletList', 'listItem', 'orderedList', 'rule', 'caption', 'tableRow', 'media', 'mediaSingle', 'mediaInline']);
|
|
26
|
+
var isCodeBlock = isType('codeBlock');
|
|
27
|
+
var isMediaNode = isType('media');
|
|
28
|
+
var isMediaInline = isType('mediaInline');
|
|
29
|
+
var isMediaSingleNode = isType('mediaSingle');
|
|
30
|
+
var isMentionNode = isType('mention');
|
|
31
|
+
var isParagraph = isType('paragraph');
|
|
32
|
+
var isHeading = isType('heading');
|
|
33
|
+
var isTable = isType('table');
|
|
34
|
+
var isTableCell = isType('tableCell');
|
|
35
|
+
var isTableHeader = isType('tableHeader');
|
|
36
|
+
var isLinkMark = isType('link');
|
|
37
|
+
var isUnsupportedMark = isType('unsupportedMark');
|
|
38
|
+
var isUnsupportedNodeAttributeMark = isType('unsupportedNodeAttribute');
|
|
39
|
+
var isExpand = isType('expand');
|
|
40
|
+
var isNestedExpand = isType('nestedExpand');
|
|
41
|
+
var isUnsupportedNode = function isUnsupportedNode(node) {
|
|
42
|
+
return isType('unsupportedBlock')(node) || isType('unsupportedInline')(node);
|
|
43
|
+
};
|
|
44
|
+
var isDataConsumer = isType('dataConsumer');
|
|
45
|
+
var isFragmentMark = isType('fragment');
|
|
46
|
+
var isHardBreak = isType('hardBreak');
|
|
47
|
+
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
49
|
+
var _filterNull = function filterNull(subject) {
|
|
50
|
+
var output = _objectSpread({}, subject);
|
|
51
|
+
// eslint-disable-next-line guard-for-in
|
|
52
|
+
for (var key in output) {
|
|
53
|
+
var current = output[key];
|
|
54
|
+
if (current === null) {
|
|
55
|
+
var _output = output,
|
|
56
|
+
unusedKey = _output[key],
|
|
57
|
+
filteredObj = (0, _objectWithoutProperties2.default)(_output, [key].map(_toPropertyKey));
|
|
58
|
+
output = filteredObj;
|
|
59
|
+
} else if ((0, _typeof2.default)(current) === 'object' && !Array.isArray(current)) {
|
|
60
|
+
output[key] = _filterNull(current);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return output;
|
|
64
|
+
};
|
|
65
|
+
var canOverrideMark = function canOverrideMark(mark, existingMarks) {
|
|
66
|
+
if (existingMarks.some(function (e) {
|
|
67
|
+
return mark.attrs.originalValue.type === e.type.name;
|
|
68
|
+
})) {
|
|
69
|
+
return (0, _markOverrideRules.markOverrideRuleFor)(mark.attrs.originalValue.type).canOverrideUnsupportedMark();
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
};
|
|
73
|
+
var getUnwrappedNodeAttributes = function getUnwrappedNodeAttributes(node, mark, obj) {
|
|
74
|
+
var nodeAttributes = node.type.spec.attrs;
|
|
75
|
+
var attributes = _objectSpread(_objectSpread({}, mark.attrs.unsupported), obj.attrs);
|
|
76
|
+
// Ignored via go/ees005
|
|
77
|
+
// eslint-disable-next-line no-var
|
|
78
|
+
for (var key in obj.attrs) {
|
|
79
|
+
if (obj.attrs.hasOwnProperty(key)) {
|
|
80
|
+
var attribute = nodeAttributes ? nodeAttributes[key] : null;
|
|
81
|
+
if (attribute) {
|
|
82
|
+
if (attribute.default === node.attrs[key] && mark.attrs.unsupported[key]) {
|
|
83
|
+
return _objectSpread(_objectSpread({}, attributes), {}, (0, _defineProperty2.default)({}, key, mark.attrs.unsupported[key]));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return attributes;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Transforms a Prosemirror node into a JSON representation. Compared with
|
|
93
|
+
* native Prosemirror JSON, this representation ensures we have valid ADF attrs.
|
|
94
|
+
*
|
|
95
|
+
* @param node The Prosemirror node to transform.
|
|
96
|
+
* @param mentionMap A map of mention IDs to their display names.
|
|
97
|
+
* @returns The JSON representation of the node.
|
|
98
|
+
*/
|
|
99
|
+
var _toJSON = exports.toJSON = function toJSON(node, mentionMap) {
|
|
100
|
+
var obj = {
|
|
101
|
+
type: node.type.name
|
|
102
|
+
};
|
|
103
|
+
if (isUnsupportedNode(node)) {
|
|
104
|
+
return node.attrs.originalValue;
|
|
105
|
+
} else if (isMediaNode(node)) {
|
|
106
|
+
obj.attrs = (0, _adfSchema.mediaToJSON)(node).attrs;
|
|
107
|
+
} else if (isMediaSingleNode(node)) {
|
|
108
|
+
obj.attrs = (0, _adfSchema.mediaSingleToJSON)(node).attrs;
|
|
109
|
+
} else if (isMediaInline(node)) {
|
|
110
|
+
obj.attrs = (0, _adfSchema.mediaToJSON)(node).attrs;
|
|
111
|
+
} else if (isMentionNode(node)) {
|
|
112
|
+
obj.attrs = (0, _adfSchema.mentionToJSON)(node).attrs;
|
|
113
|
+
} else if (isCodeBlock(node)) {
|
|
114
|
+
obj.attrs = (0, _adfSchema.codeBlockToJSON)(node).attrs;
|
|
115
|
+
} else if (isTable(node)) {
|
|
116
|
+
obj.attrs = (0, _adfSchema.tableToJSON)(node).attrs;
|
|
117
|
+
} else if (isTableCell(node)) {
|
|
118
|
+
obj.attrs = (0, _adfSchema.toJSONTableCell)(node).attrs;
|
|
119
|
+
} else if (isTableHeader(node)) {
|
|
120
|
+
obj.attrs = (0, _adfSchema.toJSONTableHeader)(node).attrs;
|
|
121
|
+
} else if (isExpand(node) || isNestedExpand(node)) {
|
|
122
|
+
obj.attrs = (0, _adfSchema.expandToJSON)(node).attrs;
|
|
123
|
+
} else if (node.attrs && Object.keys(node.attrs).length) {
|
|
124
|
+
obj.attrs = node.attrs;
|
|
125
|
+
}
|
|
126
|
+
if (obj.attrs) {
|
|
127
|
+
obj.attrs = _filterNull(obj.attrs);
|
|
128
|
+
}
|
|
129
|
+
if (isMentionNode(node) && mentionMap) {
|
|
130
|
+
var _text, _obj$attrs;
|
|
131
|
+
// If the mentionMap exists and has a name defined then we should use it and prepend @ to the begining of it.
|
|
132
|
+
var name = !!(mentionMap !== null && mentionMap !== void 0 && mentionMap[node.attrs.id]) ? "@".concat(mentionMap[node.attrs.id]) : '';
|
|
133
|
+
// Ignored via go/ees005
|
|
134
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
135
|
+
var text = (_text = (_obj$attrs = obj.attrs) === null || _obj$attrs === void 0 ? void 0 : _obj$attrs.text) !== null && _text !== void 0 ? _text : '';
|
|
136
|
+
obj.attrs = _objectSpread(_objectSpread({}, obj.attrs), {}, {
|
|
137
|
+
// If an explicit text value is set on the mention then that should take priority over the mentionMap value.
|
|
138
|
+
text: !!text ? text : name
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
if (NODES_WITH_EMPTY_ATTRS_REMOVAL.has(node.type.name) && obj.attrs && !Object.keys(obj.attrs).length || isHardBreak(node)) {
|
|
142
|
+
delete obj.attrs;
|
|
143
|
+
}
|
|
144
|
+
if (node.isText) {
|
|
145
|
+
obj.text = node.textContent;
|
|
146
|
+
} else {
|
|
147
|
+
node.content.forEach(function (child) {
|
|
148
|
+
obj.content = obj.content || [];
|
|
149
|
+
obj.content.push(_toJSON(child, mentionMap));
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
if (isParagraph(node) || isHeading(node)) {
|
|
153
|
+
obj.content = obj.content || [];
|
|
154
|
+
}
|
|
155
|
+
if (node.marks.length) {
|
|
156
|
+
// Run any custom mark serialisers
|
|
157
|
+
var parsedMarks = node.marks.map(function (mark) {
|
|
158
|
+
if (isUnsupportedMark(mark)) {
|
|
159
|
+
return canOverrideMark(mark, node.marks) ? null : mark.attrs.originalValue;
|
|
160
|
+
} else if (isUnsupportedNodeAttributeMark(mark)) {
|
|
161
|
+
return null;
|
|
162
|
+
} else if (isLinkMark(mark)) {
|
|
163
|
+
return (0, _adfSchema.linkToJSON)(mark);
|
|
164
|
+
} else if (isDataConsumer(mark)) {
|
|
165
|
+
var _serialised$attrs$sou;
|
|
166
|
+
var serialised = (0, _adfSchema.dataConsumerToJSON)(mark);
|
|
167
|
+
return !serialised.attrs.sources || ((_serialised$attrs$sou = serialised.attrs.sources) === null || _serialised$attrs$sou === void 0 ? void 0 : _serialised$attrs$sou.length) === 0 ? null : serialised;
|
|
168
|
+
} else if (isFragmentMark(mark)) {
|
|
169
|
+
var fragmentMark = (0, _adfSchema.fragmentToJSON)(mark);
|
|
170
|
+
if (!fragmentMark.attrs.localId) {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
return fragmentMark;
|
|
174
|
+
} else {
|
|
175
|
+
return mark.toJSON();
|
|
176
|
+
}
|
|
177
|
+
}).filter(function (maybeMark) {
|
|
178
|
+
return maybeMark !== null;
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// Only set if we have a non-empty array, otherwise explicitly undefine it (as we only run this path if `node.marks.length`)
|
|
182
|
+
obj.marks = (parsedMarks === null || parsedMarks === void 0 ? void 0 : parsedMarks.length) > 0 ? parsedMarks : undefined;
|
|
183
|
+
var nodeAttributeMark = node.marks.find(isUnsupportedNodeAttributeMark);
|
|
184
|
+
if (nodeAttributeMark && nodeAttributeMark.attrs.type.nodeType === obj.type) {
|
|
185
|
+
obj.attrs = _objectSpread({}, getUnwrappedNodeAttributes(node, nodeAttributeMark, obj));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return obj;
|
|
189
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import isEqual from 'lodash/isEqual';
|
|
2
|
+
import { defaultSchema, getSchemaBasedOnStage } from '@atlaskit/adf-schema/schema-default';
|
|
3
|
+
import { sanitizeNode } from './sanitize/sanitize-node';
|
|
4
|
+
import { toJSON } from './toJSON';
|
|
5
|
+
const createDocFromContent = content => {
|
|
6
|
+
return {
|
|
7
|
+
version: 1,
|
|
8
|
+
type: 'doc',
|
|
9
|
+
content: content || []
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
const emptyDoc = createDocFromContent([{
|
|
13
|
+
type: 'paragraph',
|
|
14
|
+
content: []
|
|
15
|
+
}]);
|
|
16
|
+
export class JSONTransformer {
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new JSONTransformer instance.
|
|
19
|
+
* @param schema The current editor schema
|
|
20
|
+
* @param mentionMap An optional mapping of user IDs to mention names. This is used by the encoder to substitute empty
|
|
21
|
+
* mention node.attr.text values with mapped names.
|
|
22
|
+
*/
|
|
23
|
+
constructor(schema = defaultSchema, mentionMap) {
|
|
24
|
+
this.schema = schema;
|
|
25
|
+
this.mentionMap = mentionMap;
|
|
26
|
+
}
|
|
27
|
+
encode(node, options = {}) {
|
|
28
|
+
const content = [];
|
|
29
|
+
node.content.forEach(child => {
|
|
30
|
+
content.push(sanitizeNode(toJSON(child, this.mentionMap), options));
|
|
31
|
+
});
|
|
32
|
+
if (!content || isEqual(content, emptyDoc.content)) {
|
|
33
|
+
return createDocFromContent([]);
|
|
34
|
+
}
|
|
35
|
+
return createDocFromContent(content);
|
|
36
|
+
}
|
|
37
|
+
internalParse(content, schema) {
|
|
38
|
+
const doc = schema.nodeFromJSON(content);
|
|
39
|
+
doc.check();
|
|
40
|
+
return doc;
|
|
41
|
+
}
|
|
42
|
+
parse(content, stage) {
|
|
43
|
+
if (content.type !== 'doc') {
|
|
44
|
+
throw new Error('Expected content format to be ADF');
|
|
45
|
+
}
|
|
46
|
+
const schema = !!stage ? getSchemaBasedOnStage(stage) : this.schema;
|
|
47
|
+
if (!content.content || content.content.length === 0) {
|
|
48
|
+
return this.internalParse(emptyDoc, schema);
|
|
49
|
+
}
|
|
50
|
+
return this.internalParse(content, schema);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* This method is used to encode a single node
|
|
55
|
+
*/
|
|
56
|
+
encodeNode(node) {
|
|
57
|
+
return sanitizeNode(toJSON(node, this.mentionMap));
|
|
58
|
+
}
|
|
59
|
+
}
|
package/dist/es2019/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Disable no-re-export rule for entry point files
|
|
2
2
|
/* eslint-disable @atlaskit/editor/no-re-export */
|
|
3
3
|
|
|
4
|
-
export { JSONTransformer
|
|
5
|
-
export {
|
|
4
|
+
export { JSONTransformer } from './JSONTransformer-2';
|
|
5
|
+
export { SchemaStage } from './SchemaStage';
|
|
6
|
+
export { toJSON as nodeToJSON } from './toJSON';
|
|
7
|
+
export { isJSONDocNode } from './main';
|
|
@@ -1,239 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { markOverrideRuleFor } from './markOverrideRules';
|
|
5
|
-
import { sanitizeNode } from './sanitize/sanitize-node';
|
|
6
|
-
export let SchemaStage = /*#__PURE__*/function (SchemaStage) {
|
|
7
|
-
SchemaStage["FINAL"] = "final";
|
|
8
|
-
SchemaStage["STAGE_0"] = "stage0";
|
|
9
|
-
return SchemaStage;
|
|
10
|
-
}({});
|
|
11
|
-
const isType = type => node => node.type.name === type;
|
|
12
|
-
|
|
13
|
-
// Create a Set of node types that should have empty attrs removed
|
|
14
|
-
const NODES_WITH_EMPTY_ATTRS_REMOVAL = new Set(['paragraph', 'layoutSection', 'blockquote', 'nestedExpand', 'bulletList', 'listItem', 'orderedList', 'rule', 'caption', 'tableRow', 'media', 'mediaSingle', 'mediaInline']);
|
|
15
|
-
const isCodeBlock = isType('codeBlock');
|
|
16
|
-
const isMediaNode = isType('media');
|
|
17
|
-
const isMediaInline = isType('mediaInline');
|
|
18
|
-
const isMediaSingleNode = isType('mediaSingle');
|
|
19
|
-
const isMentionNode = isType('mention');
|
|
20
|
-
const isParagraph = isType('paragraph');
|
|
21
|
-
const isHeading = isType('heading');
|
|
22
|
-
const isTable = isType('table');
|
|
23
|
-
const isTableCell = isType('tableCell');
|
|
24
|
-
const isTableHeader = isType('tableHeader');
|
|
25
|
-
const isLinkMark = isType('link');
|
|
26
|
-
const isUnsupportedMark = isType('unsupportedMark');
|
|
27
|
-
const isUnsupportedNodeAttributeMark = isType('unsupportedNodeAttribute');
|
|
28
|
-
const isExpand = isType('expand');
|
|
29
|
-
const isNestedExpand = isType('nestedExpand');
|
|
30
|
-
const isUnsupportedNode = node => isType('unsupportedBlock')(node) || isType('unsupportedInline')(node);
|
|
31
|
-
const isDataConsumer = isType('dataConsumer');
|
|
32
|
-
const isFragmentMark = isType('fragment');
|
|
33
|
-
const isHardBreak = isType('hardBreak');
|
|
34
|
-
const createDocFromContent = content => {
|
|
35
|
-
return {
|
|
36
|
-
version: 1,
|
|
37
|
-
type: 'doc',
|
|
38
|
-
content: content || []
|
|
39
|
-
};
|
|
40
|
-
};
|
|
41
|
-
const emptyDoc = createDocFromContent([{
|
|
42
|
-
type: 'paragraph',
|
|
43
|
-
content: []
|
|
44
|
-
}]);
|
|
45
|
-
|
|
46
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
47
|
-
const filterNull = subject => {
|
|
48
|
-
let output = {
|
|
49
|
-
...subject
|
|
50
|
-
};
|
|
51
|
-
// eslint-disable-next-line guard-for-in
|
|
52
|
-
for (const key in output) {
|
|
53
|
-
const current = output[key];
|
|
54
|
-
if (current === null) {
|
|
55
|
-
const {
|
|
56
|
-
[key]: unusedKey,
|
|
57
|
-
...filteredObj
|
|
58
|
-
} = output;
|
|
59
|
-
output = filteredObj;
|
|
60
|
-
} else if (typeof current === 'object' && !Array.isArray(current)) {
|
|
61
|
-
output[key] = filterNull(current);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return output;
|
|
65
|
-
};
|
|
66
|
-
const canOverrideMark = (mark, existingMarks) => {
|
|
67
|
-
if (existingMarks.some(e => mark.attrs.originalValue.type === e.type.name)) {
|
|
68
|
-
return markOverrideRuleFor(mark.attrs.originalValue.type).canOverrideUnsupportedMark();
|
|
69
|
-
}
|
|
70
|
-
return false;
|
|
71
|
-
};
|
|
72
|
-
const getUnwrappedNodeAttributes = (node, mark, obj) => {
|
|
73
|
-
const nodeAttributes = node.type.spec.attrs;
|
|
74
|
-
const attributes = {
|
|
75
|
-
...mark.attrs.unsupported,
|
|
76
|
-
...obj.attrs
|
|
77
|
-
};
|
|
78
|
-
// Ignored via go/ees005
|
|
79
|
-
// eslint-disable-next-line no-var
|
|
80
|
-
for (var key in obj.attrs) {
|
|
81
|
-
if (obj.attrs.hasOwnProperty(key)) {
|
|
82
|
-
const attribute = nodeAttributes ? nodeAttributes[key] : null;
|
|
83
|
-
if (attribute) {
|
|
84
|
-
if (attribute.default === node.attrs[key] && mark.attrs.unsupported[key]) {
|
|
85
|
-
return {
|
|
86
|
-
...attributes,
|
|
87
|
-
[key]: mark.attrs.unsupported[key]
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
return attributes;
|
|
94
|
-
};
|
|
1
|
+
/* eslint-disable @repo/internal/deprecations/deprecation-ticket-required -- VOLTC-139 tracks removal of these deprecated re-export shims. */
|
|
2
|
+
// Disable no-re-export rule for entry point files
|
|
3
|
+
/* eslint-disable @atlaskit/editor/no-re-export */
|
|
95
4
|
|
|
96
5
|
/**
|
|
97
|
-
*
|
|
98
|
-
* native Prosemirror JSON, this representation ensures we have valid ADF attrs.
|
|
99
|
-
*
|
|
100
|
-
* @param node The Prosemirror node to transform.
|
|
101
|
-
* @param mentionMap A map of mention IDs to their display names.
|
|
102
|
-
* @returns The JSON representation of the node.
|
|
6
|
+
* @deprecated Use `import { SchemaStage } from '@atlaskit/editor-json-transformer'` instead.
|
|
103
7
|
*/
|
|
104
|
-
export
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
obj.attrs = mediaSingleToJSON(node).attrs;
|
|
114
|
-
} else if (isMediaInline(node)) {
|
|
115
|
-
obj.attrs = mediaToJSON(node).attrs;
|
|
116
|
-
} else if (isMentionNode(node)) {
|
|
117
|
-
obj.attrs = mentionToJSON(node).attrs;
|
|
118
|
-
} else if (isCodeBlock(node)) {
|
|
119
|
-
obj.attrs = codeBlockToJSON(node).attrs;
|
|
120
|
-
} else if (isTable(node)) {
|
|
121
|
-
obj.attrs = tableToJSON(node).attrs;
|
|
122
|
-
} else if (isTableCell(node)) {
|
|
123
|
-
obj.attrs = toJSONTableCell(node).attrs;
|
|
124
|
-
} else if (isTableHeader(node)) {
|
|
125
|
-
obj.attrs = toJSONTableHeader(node).attrs;
|
|
126
|
-
} else if (isExpand(node) || isNestedExpand(node)) {
|
|
127
|
-
obj.attrs = expandToJSON(node).attrs;
|
|
128
|
-
} else if (node.attrs && Object.keys(node.attrs).length) {
|
|
129
|
-
obj.attrs = node.attrs;
|
|
130
|
-
}
|
|
131
|
-
if (obj.attrs) {
|
|
132
|
-
obj.attrs = filterNull(obj.attrs);
|
|
133
|
-
}
|
|
134
|
-
if (isMentionNode(node) && mentionMap) {
|
|
135
|
-
var _text, _obj$attrs;
|
|
136
|
-
// If the mentionMap exists and has a name defined then we should use it and prepend @ to the begining of it.
|
|
137
|
-
const name = !!(mentionMap !== null && mentionMap !== void 0 && mentionMap[node.attrs.id]) ? `@${mentionMap[node.attrs.id]}` : '';
|
|
138
|
-
// Ignored via go/ees005
|
|
139
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
140
|
-
const text = (_text = (_obj$attrs = obj.attrs) === null || _obj$attrs === void 0 ? void 0 : _obj$attrs.text) !== null && _text !== void 0 ? _text : '';
|
|
141
|
-
obj.attrs = {
|
|
142
|
-
...obj.attrs,
|
|
143
|
-
// If an explicit text value is set on the mention then that should take priority over the mentionMap value.
|
|
144
|
-
text: !!text ? text : name
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
if (NODES_WITH_EMPTY_ATTRS_REMOVAL.has(node.type.name) && obj.attrs && !Object.keys(obj.attrs).length || isHardBreak(node)) {
|
|
148
|
-
delete obj.attrs;
|
|
149
|
-
}
|
|
150
|
-
if (node.isText) {
|
|
151
|
-
obj.text = node.textContent;
|
|
152
|
-
} else {
|
|
153
|
-
node.content.forEach(child => {
|
|
154
|
-
obj.content = obj.content || [];
|
|
155
|
-
obj.content.push(toJSON(child, mentionMap));
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
if (isParagraph(node) || isHeading(node)) {
|
|
159
|
-
obj.content = obj.content || [];
|
|
160
|
-
}
|
|
161
|
-
if (node.marks.length) {
|
|
162
|
-
// Run any custom mark serialisers
|
|
163
|
-
const parsedMarks = node.marks.map(mark => {
|
|
164
|
-
if (isUnsupportedMark(mark)) {
|
|
165
|
-
return canOverrideMark(mark, node.marks) ? null : mark.attrs.originalValue;
|
|
166
|
-
} else if (isUnsupportedNodeAttributeMark(mark)) {
|
|
167
|
-
return null;
|
|
168
|
-
} else if (isLinkMark(mark)) {
|
|
169
|
-
return linkToJSON(mark);
|
|
170
|
-
} else if (isDataConsumer(mark)) {
|
|
171
|
-
var _serialised$attrs$sou;
|
|
172
|
-
const serialised = dataConsumerToJSON(mark);
|
|
173
|
-
return !serialised.attrs.sources || ((_serialised$attrs$sou = serialised.attrs.sources) === null || _serialised$attrs$sou === void 0 ? void 0 : _serialised$attrs$sou.length) === 0 ? null : serialised;
|
|
174
|
-
} else if (isFragmentMark(mark)) {
|
|
175
|
-
const fragmentMark = fragmentToJSON(mark);
|
|
176
|
-
if (!fragmentMark.attrs.localId) {
|
|
177
|
-
return null;
|
|
178
|
-
}
|
|
179
|
-
return fragmentMark;
|
|
180
|
-
} else {
|
|
181
|
-
return mark.toJSON();
|
|
182
|
-
}
|
|
183
|
-
}).filter(maybeMark => maybeMark !== null);
|
|
184
|
-
|
|
185
|
-
// Only set if we have a non-empty array, otherwise explicitly undefine it (as we only run this path if `node.marks.length`)
|
|
186
|
-
obj.marks = (parsedMarks === null || parsedMarks === void 0 ? void 0 : parsedMarks.length) > 0 ? parsedMarks : undefined;
|
|
187
|
-
const nodeAttributeMark = node.marks.find(isUnsupportedNodeAttributeMark);
|
|
188
|
-
if (nodeAttributeMark && nodeAttributeMark.attrs.type.nodeType === obj.type) {
|
|
189
|
-
obj.attrs = {
|
|
190
|
-
...getUnwrappedNodeAttributes(node, nodeAttributeMark, obj)
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
return obj;
|
|
195
|
-
};
|
|
196
|
-
export class JSONTransformer {
|
|
197
|
-
/**
|
|
198
|
-
* Creates a new JSONTransformer instance.
|
|
199
|
-
* @param schema The current editor schema
|
|
200
|
-
* @param mentionMap An optional mapping of user IDs to mention names. This is used by the encoder to substitute empty
|
|
201
|
-
* mention node.attr.text values with mapped names.
|
|
202
|
-
*/
|
|
203
|
-
constructor(schema = defaultSchema, mentionMap) {
|
|
204
|
-
this.schema = schema;
|
|
205
|
-
this.mentionMap = mentionMap;
|
|
206
|
-
}
|
|
207
|
-
encode(node, options = {}) {
|
|
208
|
-
const content = [];
|
|
209
|
-
node.content.forEach(child => {
|
|
210
|
-
content.push(sanitizeNode(toJSON(child, this.mentionMap), options));
|
|
211
|
-
});
|
|
212
|
-
if (!content || isEqual(content, emptyDoc.content)) {
|
|
213
|
-
return createDocFromContent([]);
|
|
214
|
-
}
|
|
215
|
-
return createDocFromContent(content);
|
|
216
|
-
}
|
|
217
|
-
internalParse(content, schema) {
|
|
218
|
-
const doc = schema.nodeFromJSON(content);
|
|
219
|
-
doc.check();
|
|
220
|
-
return doc;
|
|
221
|
-
}
|
|
222
|
-
parse(content, stage) {
|
|
223
|
-
if (content.type !== 'doc') {
|
|
224
|
-
throw new Error('Expected content format to be ADF');
|
|
225
|
-
}
|
|
226
|
-
const schema = !!stage ? getSchemaBasedOnStage(stage) : this.schema;
|
|
227
|
-
if (!content.content || content.content.length === 0) {
|
|
228
|
-
return this.internalParse(emptyDoc, schema);
|
|
229
|
-
}
|
|
230
|
-
return this.internalParse(content, schema);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* This method is used to encode a single node
|
|
235
|
-
*/
|
|
236
|
-
encodeNode(node) {
|
|
237
|
-
return sanitizeNode(toJSON(node, this.mentionMap));
|
|
238
|
-
}
|
|
239
|
-
}
|
|
8
|
+
export { SchemaStage } from './SchemaStage';
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated Use `import { toJSON } from '@atlaskit/editor-json-transformer/jsonTransformer'` instead.
|
|
11
|
+
*/
|
|
12
|
+
export { toJSON } from './toJSON';
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated Use `import { JSONTransformer } from '@atlaskit/editor-json-transformer/jsonTransformer'` instead.
|
|
15
|
+
*/
|
|
16
|
+
export { JSONTransformer } from './JSONTransformer-2';
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/* eslint-disable @repo/internal/deprecations/deprecation-ticket-required -- VOLTC-139 tracks removal of these deprecated re-export shims. */
|
|
2
|
+
// Disable no-re-export rule for entry point files
|
|
3
|
+
/* eslint-disable @atlaskit/editor/no-re-export */
|
|
4
|
+
|
|
1
5
|
export function removeMarks(node) {
|
|
2
6
|
const newNode = {
|
|
3
7
|
...node
|
|
@@ -5,12 +9,8 @@ export function removeMarks(node) {
|
|
|
5
9
|
delete newNode.marks;
|
|
6
10
|
return newNode;
|
|
7
11
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
newNode.marks = node.marks.filter(mark => mark.type === 'annotation');
|
|
14
|
-
}
|
|
15
|
-
return newNode;
|
|
16
|
-
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated Use `import { removeNonAnnotationMarks } from '@atlaskit/editor-json-transformer/sanitize/remove-marks'` instead.
|
|
15
|
+
*/
|
|
16
|
+
export { removeNonAnnotationMarks } from './remove-non-annotation-marks';
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { transformNestedTableNodeOutgoingDocument } from '@atlaskit/adf-utils/transforms';
|
|
2
2
|
import { traverse } from '@atlaskit/adf-utils/traverse';
|
|
3
|
-
import { removeMarks
|
|
3
|
+
import { removeMarks } from './remove-marks';
|
|
4
|
+
import { removeNonAnnotationMarks } from './remove-non-annotation-marks';
|
|
4
5
|
const hasNestedTable = tableCellNode => {
|
|
5
6
|
var _tableCellNode$conten;
|
|
6
7
|
return (_tableCellNode$conten = tableCellNode.content) === null || _tableCellNode$conten === void 0 ? void 0 : _tableCellNode$conten.some(node => (node === null || node === void 0 ? void 0 : node.type) === 'table');
|
package/dist/es2019/sanitize.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
/* eslint-disable @repo/internal/deprecations/deprecation-ticket-required -- VOLTC-139 tracks removal of these deprecated re-export shims. */
|
|
1
2
|
// Disable no-re-export rule for entry point files
|
|
2
3
|
/* eslint-disable @atlaskit/editor/no-re-export */
|
|
3
4
|
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated Use `import { sanitizeNode } from '@atlaskit/editor-json-transformer/sanitize/sanitize-node'` instead.
|
|
7
|
+
*/
|
|
4
8
|
export { sanitizeNode } from './sanitize/sanitize-node';
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated Use `import { removeMarks } from '@atlaskit/editor-json-transformer/sanitize/remove-marks'` instead.
|
|
11
|
+
*/
|
|
5
12
|
export { removeMarks } from './sanitize/remove-marks';
|