@atlaskit/adf-schema
Version:
Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs
36 lines (35 loc) • 1.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toJSON = void 0;
/**
* There's no concept of optional property in ProseMirror. It sets value as `null`
* when there's no use of any property. We are filtering out all private & optional attrs here.
*/
var optionalAttributes = ['occurrenceKey', 'width', 'height', 'url', 'alt', 'localId'];
var externalOnlyAttributes = ['type', 'url', 'width', 'height', 'alt', 'localId'];
var toJSON = exports.toJSON = function toJSON(node) {
return {
attrs: Object.keys(node.attrs)
// Strip private attributes e.g. __fileName, __fileSize, __fileMimeType, etc.
.filter(function (key) {
return !(key[0] === '_' && key[1] === '_');
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.reduce(function (obj, key) {
if (node.attrs.type === 'external' && externalOnlyAttributes.indexOf(key) === -1) {
return obj;
}
if (optionalAttributes.indexOf(key) > -1 && (node.attrs[key] === null || node.attrs[key] === '')) {
return obj;
}
if (['width', 'height'].indexOf(key) !== -1) {
obj[key] = Number(node.attrs[key]);
return obj;
}
obj[key] = node.attrs[key];
return obj;
}, {})
};
};