@contentful/rich-text-types
Version:
Type definitions and constants for the Contentful rich text field type.
250 lines (249 loc) • 10.1 kB
JavaScript
function _array_like_to_array(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
return arr2;
}
function _array_without_holes(arr) {
if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _assert_this_initialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _call_super(_this, derived, args) {
derived = _get_prototype_of(derived);
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
}
function _class_call_check(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for(var i = 0; i < props.length; i++){
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _create_class(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _get_prototype_of(o) {
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _get_prototype_of(o);
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _set_prototype_of(subClass, superClass);
}
function _iterable_to_array(iter) {
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _non_iterable_spread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _possible_constructor_return(self, call) {
if (call && (_type_of(call) === "object" || typeof call === "function")) {
return call;
}
return _assert_this_initialized(self);
}
function _set_prototype_of(o, p) {
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _set_prototype_of(o, p);
}
function _to_consumable_array(arr) {
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
function _type_of(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
function _unsupported_iterable_to_array(o, minLen) {
if (!o) return;
if (typeof o === "string") return _array_like_to_array(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(n);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
function _is_native_reflect_construct() {
try {
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
} catch (_) {}
return (_is_native_reflect_construct = function() {
return !!result;
})();
}
import { ObjectAssertion } from './assert.mjs';
export var VOID_CONTENT = [];
export var NodeAssertion = /*#__PURE__*/ function() {
"use strict";
function NodeAssertion(contentRule, validateData) {
_class_call_check(this, NodeAssertion);
_define_property(this, "contentRule", void 0);
_define_property(this, "validateData", void 0);
this.contentRule = contentRule;
this.validateData = validateData;
}
_create_class(NodeAssertion, [
{
key: "assert",
value: function assert(node, path) {
var $ = new ObjectAssertion(node, path);
if (!$.object()) {
return $.errors;
}
$.noAdditionalProperties([
'nodeType',
'data',
'content'
]);
var _ref = Array.isArray(this.contentRule) ? {
nodeTypes: this.contentRule
} : this.contentRule(node, path), nodeTypes = _ref.nodeTypes, _ref_min = _ref.min, min = _ref_min === void 0 ? 0 : _ref_min;
if (nodeTypes.length === 0 && min > 0) {
throw new Error("Invalid content rule. Cannot have enforce a 'min' of ".concat(min, " with no nodeTypes"));
}
$.minLength('content', min);
if (nodeTypes.length === 0) {
$.empty('content');
} else {
$.each('content', function(item, path) {
var item$ = new ObjectAssertion(item, path);
if (!item$.object()) {
return item$.errors;
}
item$.enum('nodeType', nodeTypes);
return item$.errors;
});
}
if ($.object('data')) {
var _$;
var _this_validateData, _this;
var _this_validateData1;
var dataErrors = (_this_validateData1 = (_this_validateData = (_this = this).validateData) === null || _this_validateData === void 0 ? void 0 : _this_validateData.call(_this, node.data, path.of('data'))) !== null && _this_validateData1 !== void 0 ? _this_validateData1 : [];
(_$ = $).catch.apply(_$, _to_consumable_array(dataErrors));
}
return $.errors;
}
}
]);
return NodeAssertion;
}();
export var EntityLinkAssertion = /*#__PURE__*/ function(NodeAssertion) {
"use strict";
_inherits(EntityLinkAssertion, NodeAssertion);
function EntityLinkAssertion(linkType, contentNodeTypes) {
_class_call_check(this, EntityLinkAssertion);
var _this;
_this = _call_super(this, EntityLinkAssertion, [
contentNodeTypes,
function(data, path) {
return _assert_this_initialized(_this).assertLink(data, path);
}
]), _define_property(_this, "linkType", void 0), _define_property(_this, "type", void 0), _define_property(_this, "assertLink", void 0), _this.linkType = linkType, _this.assertLink = function(data, path) {
var $ = new ObjectAssertion(data, path);
if ($.object('target')) {
var _$;
var sys$ = new ObjectAssertion(data.target.sys, path.of('target').of('sys'));
if (sys$.object()) {
sys$.enum('type', [
_this.type
]);
sys$.enum('linkType', [
_this.linkType
]);
if (_this.type === 'Link') {
sys$.string('id');
sys$.noAdditionalProperties([
'type',
'linkType',
'id'
]);
} else if (_this.type === 'ResourceLink') {
sys$.string('urn');
sys$.noAdditionalProperties([
'type',
'linkType',
'urn'
]);
}
}
(_$ = $).catch.apply(_$, _to_consumable_array(sys$.errors));
}
$.noAdditionalProperties([
'target'
]);
return $.errors;
};
_this.type = _this.linkType.startsWith('Contentful:') ? 'ResourceLink' : 'Link';
return _this;
}
return EntityLinkAssertion;
}(NodeAssertion);
export var HyperLinkAssertion = /*#__PURE__*/ function(NodeAssertion) {
"use strict";
_inherits(HyperLinkAssertion, NodeAssertion);
function HyperLinkAssertion() {
_class_call_check(this, HyperLinkAssertion);
var _this;
_this = _call_super(this, HyperLinkAssertion, [
[
'text'
],
function(data, path) {
return _assert_this_initialized(_this).assertLink(data, path);
}
]), _define_property(_this, "assertLink", function(data, path) {
var $ = new ObjectAssertion(data, path);
$.string('uri');
$.noAdditionalProperties([
'uri'
]);
return $.errors;
});
return _this;
}
return HyperLinkAssertion;
}(NodeAssertion);
export var assert = function(contentRule, validateData) {
return new NodeAssertion(contentRule, validateData);
};
export var assertLink = function(linkType, contentRule) {
return new EntityLinkAssertion(linkType, contentRule);
};