@contentful/contentful-slatejs-adapter
Advanced tools
Comparing version 3.3.0 to 5.0.0
@@ -7,5 +7,5 @@ 'use strict'; | ||
var get = _interopDefault(require('lodash.get')); | ||
var Contentful = require('@contentful/rich-text-types'); | ||
var flatmap = _interopDefault(require('lodash.flatmap')); | ||
var get = _interopDefault(require('lodash.get')); | ||
var Contentful = require('@contentful/structured-text-types'); | ||
@@ -43,67 +43,112 @@ /*! ***************************************************************************** | ||
var defaultSchema = {}; | ||
/** | ||
* Creates an instance of Schema from json. | ||
* | ||
* @export | ||
* @param {SchemaJSON} [schema=defaultSchema] | ||
* @returns {Schema} | ||
*/ | ||
function fromJSON(schema) { | ||
if (schema === void 0) { schema = defaultSchema; } | ||
return { | ||
/** | ||
* Check if a `node` is void based on the schema rules. | ||
* | ||
* @param {ContentfulNonTextNodes} node | ||
* @returns | ||
*/ | ||
isVoid: function (node) { | ||
var root = Object.values(Contentful.BLOCKS).includes(node.nodeType) ? 'blocks' : 'inlines'; | ||
return get(schema, [root, node.nodeType, 'isVoid'], false); | ||
}, | ||
}; | ||
} | ||
function toSlatejsDocument(_a) { | ||
var document = _a.document, _b = _a.schema, schema = _b === void 0 ? {} : _b; | ||
var document = _a.document, schema = _a.schema; | ||
return { | ||
object: 'document', | ||
data: getDataOfDefault(document.data), | ||
nodes: flatmap(document.content, function (node) { return convertNode(node, schema); }), | ||
nodes: flatmap(document.content, function (node) { return convertNode(node, fromJSON(schema)); }), | ||
}; | ||
} | ||
// COMPAT: fixes the issue with void inline blocks in slate < v0.40 | ||
function getIsVoidValue(node, schema) { | ||
var root = node.nodeClass === 'block' ? 'blocks' : 'inlines'; | ||
return get(schema, [root, node.nodeType, 'isVoid'], false); | ||
} | ||
function convertNode(node, schema) { | ||
var nodes = []; | ||
switch (node.nodeClass) { | ||
case 'block': | ||
case 'inline': | ||
var contentfulBlock = node; | ||
var childNodes = flatmap(contentfulBlock.content, function (childNode) { | ||
return convertNode(childNode, schema); | ||
}); | ||
var slateBlock = { | ||
object: contentfulBlock.nodeClass, | ||
type: contentfulBlock.nodeType, | ||
nodes: childNodes, | ||
isVoid: getIsVoidValue(contentfulBlock, schema), | ||
data: getDataOfDefault(contentfulBlock.data), | ||
}; | ||
nodes.push(slateBlock); | ||
break; | ||
case 'text': | ||
var _a = node, _b = _a.marks, marks = _b === void 0 ? [] : _b, value = _a.value, data = _a.data; | ||
var slateText = { | ||
object: 'text', | ||
leaves: [ | ||
{ | ||
object: 'leaf', | ||
text: value, | ||
marks: marks.map(function (mark) { return (__assign({}, mark, { data: {}, object: 'mark' })); }), | ||
}, | ||
], | ||
data: getDataOfDefault(data), | ||
}; | ||
nodes.push(slateText); | ||
break; | ||
default: | ||
assertUnreachable(node); | ||
break; | ||
if (node.nodeType === 'text') { | ||
var slateText = convertTextNode(node); | ||
nodes.push(slateText); | ||
} | ||
else { | ||
var contentfulNode = node; | ||
var childNodes = flatmap(contentfulNode.content, function (childNode) { return convertNode(childNode, schema); }); | ||
var object = getSlateNodeObjectValue(contentfulNode.nodeType); | ||
var slateNode = void 0; | ||
if (object === 'inline') { | ||
slateNode = createInlineNode(contentfulNode, childNodes, schema); | ||
} | ||
else if (object === 'block') { | ||
slateNode = createBlockNode(contentfulNode, childNodes, schema); | ||
} | ||
else { | ||
throw new Error("Unexpected slate object '" + object + "'"); | ||
} | ||
nodes.push(slateNode); | ||
} | ||
return nodes; | ||
} | ||
function assertUnreachable(object) { | ||
throw new Error("Unexpected contentful object " + JSON.stringify(object)); | ||
function createBlockNode(contentfulBlock, childNodes, schema) { | ||
return { | ||
object: 'block', | ||
type: contentfulBlock.nodeType, | ||
nodes: childNodes, | ||
isVoid: schema.isVoid(contentfulBlock), | ||
data: getDataOfDefault(contentfulBlock.data), | ||
}; | ||
} | ||
function createInlineNode(contentfulBlock, childNodes, schema) { | ||
return { | ||
object: 'inline', | ||
type: contentfulBlock.nodeType, | ||
nodes: childNodes, | ||
isVoid: schema.isVoid(contentfulBlock), | ||
data: getDataOfDefault(contentfulBlock.data), | ||
}; | ||
} | ||
function convertTextNode(node) { | ||
var _a = node, _b = _a.marks, marks = _b === void 0 ? [] : _b, value = _a.value, data = _a.data; | ||
var slateText = { | ||
object: 'text', | ||
leaves: [ | ||
{ | ||
object: 'leaf', | ||
text: value, | ||
marks: marks.map(function (mark) { return (__assign({}, mark, { data: {}, object: 'mark' })); }), | ||
}, | ||
], | ||
data: getDataOfDefault(data), | ||
}; | ||
return slateText; | ||
} | ||
function getSlateNodeObjectValue(nodeType) { | ||
if (Object.values(Contentful.BLOCKS).includes(nodeType)) { | ||
return 'block'; | ||
} | ||
else if (Object.values(Contentful.INLINES).includes(nodeType)) { | ||
return 'inline'; | ||
} | ||
else { | ||
throw new Error("Unexpected contentful nodeType '" + nodeType + "'"); | ||
} | ||
} | ||
function toContentfulDocument(slateDocument) { | ||
function toContentfulDocument(_a) { | ||
var document = _a.document, schema = _a.schema; | ||
return { | ||
nodeClass: 'document', | ||
nodeType: Contentful.BLOCKS.DOCUMENT, | ||
data: getDataOfDefault(slateDocument.data), | ||
content: flatmap(slateDocument.nodes, convertNode$1), | ||
data: getDataOfDefault(document.data), | ||
content: flatmap(document.nodes, function (node) { return convertNode$1(node, fromJSON(schema)); }), | ||
}; | ||
} | ||
function convertNode$1(node) { | ||
function convertNode$1(node, schema) { | ||
var nodes = []; | ||
@@ -113,10 +158,15 @@ switch (node.object) { | ||
case 'inline': | ||
var slateBlock = node; | ||
var content = flatmap(slateBlock.nodes, convertNode$1); | ||
var slateNode = node; | ||
var content = flatmap(slateNode.nodes, function (childNode) { return convertNode$1(childNode, schema); }); | ||
if (!slateNode.type) { | ||
throw new Error("Unexpected slate node " + JSON.stringify(slateNode)); | ||
} | ||
var contentfulBlock = { | ||
nodeClass: slateBlock.object, | ||
nodeType: slateBlock.type, | ||
content: content, | ||
data: getDataOfDefault(slateBlock.data), | ||
nodeType: slateNode.type, | ||
content: [], | ||
data: getDataOfDefault(slateNode.data), | ||
}; | ||
if (!schema.isVoid(contentfulBlock)) { | ||
contentfulBlock.content = content; | ||
} | ||
nodes.push(contentfulBlock); | ||
@@ -128,3 +178,3 @@ break; | ||
default: | ||
assertUnreachable$1(node); | ||
assertUnreachable(node); | ||
break; | ||
@@ -136,3 +186,2 @@ } | ||
return node.leaves.map(function (leaf) { return ({ | ||
nodeClass: 'text', | ||
nodeType: 'text', | ||
@@ -144,3 +193,3 @@ value: leaf.text, | ||
} | ||
function assertUnreachable$1(object) { | ||
function assertUnreachable(object) { | ||
throw new Error("Unexpected slate object " + object); | ||
@@ -147,0 +196,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Contentful = require("@contentful/structured-text-types"); | ||
var Contentful = require("@contentful/rich-text-types"); | ||
function document() { | ||
@@ -10,3 +10,2 @@ var content = []; | ||
return { | ||
nodeClass: 'document', | ||
data: {}, | ||
@@ -24,3 +23,2 @@ nodeType: Contentful.BLOCKS.DOCUMENT, | ||
return { | ||
nodeClass: 'block', | ||
nodeType: nodeType, | ||
@@ -38,3 +36,2 @@ content: content, | ||
return { | ||
nodeClass: 'inline', | ||
nodeType: nodeType, | ||
@@ -52,3 +49,2 @@ content: content, | ||
return { | ||
nodeClass: 'text', | ||
nodeType: 'text', | ||
@@ -55,0 +51,0 @@ data: {}, |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _a; | ||
var contentful_to_slatejs_adapter_1 = require("../contentful-to-slatejs-adapter"); | ||
var slatejs_to_contentful_adapter_1 = require("../slatejs-to-contentful-adapter"); | ||
var Contentful = require("@contentful/structured-text-types"); | ||
var Contentful = require("@contentful/rich-text-types"); | ||
var slate = require("./slate-helpers"); | ||
var contentful = require("./contentful-helpers"); | ||
var schema = { blocks: (_a = {}, _a[Contentful.BLOCKS.EMBEDDED_ENTRY] = { isVoid: true }, _a) }; | ||
describe('adapters', function () { | ||
@@ -14,3 +16,3 @@ var testAdapters = function (message, contentfulDoc, slateDoc) { | ||
document: contentfulDoc, | ||
schema: { blocks: { voidnode: { isVoid: true } } }, | ||
schema: schema, | ||
}); | ||
@@ -22,3 +24,6 @@ expect(actualSlateDoc).toEqual(slateDoc); | ||
it(message, function () { | ||
var actualContentfulDoc = slatejs_to_contentful_adapter_1.default(slateDoc); | ||
var actualContentfulDoc = slatejs_to_contentful_adapter_1.default({ | ||
document: slateDoc, | ||
schema: schema, | ||
}); | ||
expect(actualContentfulDoc).toEqual(contentfulDoc); | ||
@@ -30,12 +35,13 @@ }); | ||
testAdapters('empty document', contentful.document(), slate.document()); | ||
testAdapters('document with block', contentful.document(contentful.block('paragraph', contentful.text(''))), slate.document(slate.block('paragraph', false, slate.text(slate.leaf(''))))); | ||
testAdapters('paragraph with inline', contentful.document(contentful.block('paragraph', contentful.inline('hyperlink'))), slate.document(slate.block('paragraph', false, slate.inline('hyperlink', false)))); | ||
testAdapters('paragraph with text', contentful.document(contentful.block('paragraph', contentful.text('hi'))), slate.document(slate.block('paragraph', false, slate.text(slate.leaf('hi'))))); | ||
testAdapters('text with marks', contentful.document(contentful.block('paragraph', contentful.text('this'), contentful.text('is', contentful.mark('bold')))), slate.document(slate.block('paragraph', false, slate.text(slate.leaf('this')), slate.text(slate.leaf('is', slate.mark('bold')))))); | ||
testAdapters('document with block', contentful.document(contentful.block(Contentful.BLOCKS.PARAGRAPH, contentful.text(''))), slate.document(slate.block(Contentful.BLOCKS.PARAGRAPH, false, slate.text(slate.leaf(''))))); | ||
testAdapters('paragraph with inline', contentful.document(contentful.block(Contentful.BLOCKS.PARAGRAPH, contentful.inline(Contentful.INLINES.HYPERLINK))), slate.document(slate.block(Contentful.BLOCKS.PARAGRAPH, false, slate.inline(Contentful.INLINES.HYPERLINK, false)))); | ||
testAdapters('paragraph with text', contentful.document(contentful.block(Contentful.BLOCKS.PARAGRAPH, contentful.text('hi'))), slate.document(slate.block(Contentful.BLOCKS.PARAGRAPH, false, slate.text(slate.leaf('hi'))))); | ||
testAdapters('text with marks', contentful.document(contentful.block(Contentful.BLOCKS.PARAGRAPH, contentful.text('this'), contentful.text('is', contentful.mark('bold')))), slate.document(slate.block(Contentful.BLOCKS.PARAGRAPH, false, slate.text(slate.leaf('this')), slate.text(slate.leaf('is', slate.mark('bold')))))); | ||
it('adds a default value to marks if undefined', function () { | ||
var slateDoc = slate.document(slate.block('paragraph', false, slate.text({ marks: undefined, object: 'leaf', text: 'Hi' }))); | ||
var ctflDoc = slatejs_to_contentful_adapter_1.default(slateDoc); | ||
expect(ctflDoc).toEqual(contentful.document(contentful.block('paragraph', { | ||
var slateDoc = slate.document(slate.block(Contentful.BLOCKS.PARAGRAPH, false, slate.text({ marks: undefined, object: 'leaf', text: 'Hi' }))); | ||
var ctflDoc = slatejs_to_contentful_adapter_1.default({ | ||
document: slateDoc, | ||
}); | ||
expect(ctflDoc).toEqual(contentful.document(contentful.block(Contentful.BLOCKS.PARAGRAPH, { | ||
nodeType: 'text', | ||
nodeClass: 'text', | ||
marks: [], | ||
@@ -46,8 +52,7 @@ data: {}, | ||
}); | ||
testAdapters('text with multiple marks', contentful.document(contentful.block('paragraph', contentful.text('this'), contentful.text('is', contentful.mark('bold')), contentful.text('huge', contentful.mark('bold'), contentful.mark('italic')))), slate.document(slate.block('paragraph', false, slate.text(slate.leaf('this')), slate.text(slate.leaf('is', slate.mark('bold'))), slate.text(slate.leaf('huge', slate.mark('bold'), slate.mark('italic')))))); | ||
testAdapters('document with nested blocks', contentful.document(contentful.block('paragraph', contentful.text('this is a test', contentful.mark('bold')), contentful.text('paragraph', contentful.mark('underline'))), contentful.block('block', contentful.block('block', contentful.text('this is it')))), slate.document(slate.block('paragraph', false, slate.text(slate.leaf('this is a test', slate.mark('bold'))), slate.text(slate.leaf('paragraph', slate.mark('underline')))), slate.block('block', false, slate.block('block', false, slate.text(slate.leaf('this is it')))))); | ||
testAdapters('text with multiple marks', contentful.document(contentful.block(Contentful.BLOCKS.PARAGRAPH, contentful.text('this'), contentful.text('is', contentful.mark('bold')), contentful.text('huge', contentful.mark('bold'), contentful.mark('italic')))), slate.document(slate.block(Contentful.BLOCKS.PARAGRAPH, false, slate.text(slate.leaf('this')), slate.text(slate.leaf('is', slate.mark('bold'))), slate.text(slate.leaf('huge', slate.mark('bold'), slate.mark('italic')))))); | ||
testAdapters('document with nested blocks', contentful.document(contentful.block(Contentful.BLOCKS.PARAGRAPH, contentful.text('this is a test', contentful.mark('bold')), contentful.text(Contentful.BLOCKS.PARAGRAPH, contentful.mark('underline'))), contentful.block(Contentful.BLOCKS.QUOTE, contentful.block(Contentful.BLOCKS.PARAGRAPH, contentful.text('this is it')))), slate.document(slate.block(Contentful.BLOCKS.PARAGRAPH, false, slate.text(slate.leaf('this is a test', slate.mark('bold'))), slate.text(slate.leaf(Contentful.BLOCKS.PARAGRAPH, slate.mark('underline')))), slate.block(Contentful.BLOCKS.QUOTE, false, slate.block(Contentful.BLOCKS.PARAGRAPH, false, slate.text(slate.leaf('this is it')))))); | ||
}); | ||
describe('converts additional data', function () { | ||
testAdapters('data in block', { | ||
nodeClass: 'document', | ||
nodeType: Contentful.BLOCKS.DOCUMENT, | ||
@@ -57,4 +62,3 @@ data: {}, | ||
{ | ||
nodeClass: 'block', | ||
nodeType: 'paragraph', | ||
nodeType: Contentful.BLOCKS.PARAGRAPH, | ||
content: [], | ||
@@ -70,3 +74,3 @@ data: { a: 1 }, | ||
object: 'block', | ||
type: 'paragraph', | ||
type: Contentful.BLOCKS.PARAGRAPH, | ||
isVoid: false, | ||
@@ -79,3 +83,2 @@ data: { a: 1 }, | ||
testAdapters('data in inline', { | ||
nodeClass: 'document', | ||
nodeType: Contentful.BLOCKS.DOCUMENT, | ||
@@ -85,9 +88,7 @@ data: {}, | ||
{ | ||
nodeClass: 'block', | ||
nodeType: 'paragraph', | ||
nodeType: Contentful.BLOCKS.PARAGRAPH, | ||
data: { a: 1 }, | ||
content: [ | ||
{ | ||
nodeClass: 'inline', | ||
nodeType: 'hyperlink', | ||
nodeType: Contentful.INLINES.HYPERLINK, | ||
data: { a: 2 }, | ||
@@ -105,3 +106,3 @@ content: [], | ||
object: 'block', | ||
type: 'paragraph', | ||
type: Contentful.BLOCKS.PARAGRAPH, | ||
isVoid: false, | ||
@@ -112,3 +113,3 @@ data: { a: 1 }, | ||
object: 'inline', | ||
type: 'hyperlink', | ||
type: Contentful.INLINES.HYPERLINK, | ||
isVoid: false, | ||
@@ -125,3 +126,2 @@ data: { | ||
testAdapters('data in text', { | ||
nodeClass: 'document', | ||
nodeType: Contentful.BLOCKS.DOCUMENT, | ||
@@ -131,9 +131,7 @@ data: {}, | ||
{ | ||
nodeClass: 'block', | ||
nodeType: 'paragraph', | ||
nodeType: Contentful.BLOCKS.PARAGRAPH, | ||
data: { a: 1 }, | ||
content: [ | ||
{ | ||
nodeClass: 'inline', | ||
nodeType: 'hyperlink', | ||
nodeType: Contentful.INLINES.HYPERLINK, | ||
data: { a: 2 }, | ||
@@ -143,3 +141,2 @@ content: [], | ||
{ | ||
nodeClass: 'text', | ||
nodeType: 'text', | ||
@@ -159,3 +156,3 @@ marks: [], | ||
object: 'block', | ||
type: 'paragraph', | ||
type: Contentful.BLOCKS.PARAGRAPH, | ||
isVoid: false, | ||
@@ -166,3 +163,3 @@ data: { a: 1 }, | ||
object: 'inline', | ||
type: 'hyperlink', | ||
type: Contentful.INLINES.HYPERLINK, | ||
isVoid: false, | ||
@@ -192,3 +189,2 @@ data: { | ||
testAdapters('data in block', { | ||
nodeClass: 'document', | ||
nodeType: Contentful.BLOCKS.DOCUMENT, | ||
@@ -198,4 +194,3 @@ data: {}, | ||
{ | ||
nodeClass: 'block', | ||
nodeType: 'voidnode', | ||
nodeType: Contentful.BLOCKS.EMBEDDED_ENTRY, | ||
content: [], | ||
@@ -211,3 +206,3 @@ data: { a: 1 }, | ||
object: 'block', | ||
type: 'voidnode', | ||
type: Contentful.BLOCKS.EMBEDDED_ENTRY, | ||
isVoid: true, | ||
@@ -219,4 +214,48 @@ data: { a: 1 }, | ||
}); | ||
test('removes empty text nodes from void nodes content', function () { | ||
var contentfulDoc = { | ||
nodeType: Contentful.BLOCKS.DOCUMENT, | ||
data: {}, | ||
content: [ | ||
{ | ||
nodeType: Contentful.BLOCKS.EMBEDDED_ENTRY, | ||
content: [], | ||
data: { a: 1 }, | ||
}, | ||
], | ||
}; | ||
var slateDoc = { | ||
object: 'document', | ||
type: 'document', | ||
data: {}, | ||
nodes: [ | ||
{ | ||
object: 'block', | ||
type: Contentful.BLOCKS.EMBEDDED_ENTRY, | ||
isVoid: true, | ||
data: { a: 1 }, | ||
nodes: [ | ||
{ | ||
object: 'text', | ||
data: {}, | ||
leaves: [ | ||
{ | ||
object: 'leaf', | ||
marks: [], | ||
text: '', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
var actualContentfulDoc = slatejs_to_contentful_adapter_1.default({ | ||
document: slateDoc, | ||
schema: schema, | ||
}); | ||
expect(actualContentfulDoc).toEqual(contentfulDoc); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=contentful-to-slatejs-adapter.test.js.map |
@@ -12,60 +12,82 @@ "use strict"; | ||
var lodash_flatmap_1 = require("lodash.flatmap"); | ||
var lodash_get_1 = require("lodash.get"); | ||
var Contentful = require("@contentful/rich-text-types"); | ||
var helpers_1 = require("./helpers"); | ||
var schema_1 = require("./schema"); | ||
function toSlatejsDocument(_a) { | ||
var document = _a.document, _b = _a.schema, schema = _b === void 0 ? {} : _b; | ||
var document = _a.document, schema = _a.schema; | ||
return { | ||
object: 'document', | ||
data: helpers_1.getDataOfDefault(document.data), | ||
nodes: lodash_flatmap_1.default(document.content, function (node) { return convertNode(node, schema); }), | ||
nodes: lodash_flatmap_1.default(document.content, function (node) { return convertNode(node, schema_1.fromJSON(schema)); }), | ||
}; | ||
} | ||
exports.default = toSlatejsDocument; | ||
// COMPAT: fixes the issue with void inline blocks in slate < v0.40 | ||
function getIsVoidValue(node, schema) { | ||
var root = node.nodeClass === 'block' ? 'blocks' : 'inlines'; | ||
return lodash_get_1.default(schema, [root, node.nodeType, 'isVoid'], false); | ||
} | ||
function convertNode(node, schema) { | ||
var nodes = []; | ||
switch (node.nodeClass) { | ||
case 'block': | ||
case 'inline': | ||
var contentfulBlock = node; | ||
var childNodes = lodash_flatmap_1.default(contentfulBlock.content, function (childNode) { | ||
return convertNode(childNode, schema); | ||
}); | ||
var slateBlock = { | ||
object: contentfulBlock.nodeClass, | ||
type: contentfulBlock.nodeType, | ||
nodes: childNodes, | ||
isVoid: getIsVoidValue(contentfulBlock, schema), | ||
data: helpers_1.getDataOfDefault(contentfulBlock.data), | ||
}; | ||
nodes.push(slateBlock); | ||
break; | ||
case 'text': | ||
var _a = node, _b = _a.marks, marks = _b === void 0 ? [] : _b, value = _a.value, data = _a.data; | ||
var slateText = { | ||
object: 'text', | ||
leaves: [ | ||
{ | ||
object: 'leaf', | ||
text: value, | ||
marks: marks.map(function (mark) { return (__assign({}, mark, { data: {}, object: 'mark' })); }), | ||
}, | ||
], | ||
data: helpers_1.getDataOfDefault(data), | ||
}; | ||
nodes.push(slateText); | ||
break; | ||
default: | ||
assertUnreachable(node); | ||
break; | ||
if (node.nodeType === 'text') { | ||
var slateText = convertTextNode(node); | ||
nodes.push(slateText); | ||
} | ||
else { | ||
var contentfulNode = node; | ||
var childNodes = lodash_flatmap_1.default(contentfulNode.content, function (childNode) { return convertNode(childNode, schema); }); | ||
var object = getSlateNodeObjectValue(contentfulNode.nodeType); | ||
var slateNode = void 0; | ||
if (object === 'inline') { | ||
slateNode = createInlineNode(contentfulNode, childNodes, schema); | ||
} | ||
else if (object === 'block') { | ||
slateNode = createBlockNode(contentfulNode, childNodes, schema); | ||
} | ||
else { | ||
throw new Error("Unexpected slate object '" + object + "'"); | ||
} | ||
nodes.push(slateNode); | ||
} | ||
return nodes; | ||
} | ||
function assertUnreachable(object) { | ||
throw new Error("Unexpected contentful object " + JSON.stringify(object)); | ||
function createBlockNode(contentfulBlock, childNodes, schema) { | ||
return { | ||
object: 'block', | ||
type: contentfulBlock.nodeType, | ||
nodes: childNodes, | ||
isVoid: schema.isVoid(contentfulBlock), | ||
data: helpers_1.getDataOfDefault(contentfulBlock.data), | ||
}; | ||
} | ||
function createInlineNode(contentfulBlock, childNodes, schema) { | ||
return { | ||
object: 'inline', | ||
type: contentfulBlock.nodeType, | ||
nodes: childNodes, | ||
isVoid: schema.isVoid(contentfulBlock), | ||
data: helpers_1.getDataOfDefault(contentfulBlock.data), | ||
}; | ||
} | ||
function convertTextNode(node) { | ||
var _a = node, _b = _a.marks, marks = _b === void 0 ? [] : _b, value = _a.value, data = _a.data; | ||
var slateText = { | ||
object: 'text', | ||
leaves: [ | ||
{ | ||
object: 'leaf', | ||
text: value, | ||
marks: marks.map(function (mark) { return (__assign({}, mark, { data: {}, object: 'mark' })); }), | ||
}, | ||
], | ||
data: helpers_1.getDataOfDefault(data), | ||
}; | ||
return slateText; | ||
} | ||
function getSlateNodeObjectValue(nodeType) { | ||
if (Object.values(Contentful.BLOCKS).includes(nodeType)) { | ||
return 'block'; | ||
} | ||
else if (Object.values(Contentful.INLINES).includes(nodeType)) { | ||
return 'inline'; | ||
} | ||
else { | ||
throw new Error("Unexpected contentful nodeType '" + nodeType + "'"); | ||
} | ||
} | ||
//# sourceMappingURL=contentful-to-slatejs-adapter.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var lodash_flatmap_1 = require("lodash.flatmap"); | ||
var Contentful = require("@contentful/structured-text-types"); | ||
var Contentful = require("@contentful/rich-text-types"); | ||
var helpers_1 = require("./helpers"); | ||
function toContentfulDocument(slateDocument) { | ||
var schema_1 = require("./schema"); | ||
function toContentfulDocument(_a) { | ||
var document = _a.document, schema = _a.schema; | ||
return { | ||
nodeClass: 'document', | ||
nodeType: Contentful.BLOCKS.DOCUMENT, | ||
data: helpers_1.getDataOfDefault(slateDocument.data), | ||
content: lodash_flatmap_1.default(slateDocument.nodes, convertNode), | ||
data: helpers_1.getDataOfDefault(document.data), | ||
content: lodash_flatmap_1.default(document.nodes, function (node) { return convertNode(node, schema_1.fromJSON(schema)); }), | ||
}; | ||
} | ||
exports.default = toContentfulDocument; | ||
function convertNode(node) { | ||
function convertNode(node, schema) { | ||
var nodes = []; | ||
@@ -20,10 +21,15 @@ switch (node.object) { | ||
case 'inline': | ||
var slateBlock = node; | ||
var content = lodash_flatmap_1.default(slateBlock.nodes, convertNode); | ||
var slateNode = node; | ||
var content = lodash_flatmap_1.default(slateNode.nodes, function (childNode) { return convertNode(childNode, schema); }); | ||
if (!slateNode.type) { | ||
throw new Error("Unexpected slate node " + JSON.stringify(slateNode)); | ||
} | ||
var contentfulBlock = { | ||
nodeClass: slateBlock.object, | ||
nodeType: slateBlock.type, | ||
content: content, | ||
data: helpers_1.getDataOfDefault(slateBlock.data), | ||
nodeType: slateNode.type, | ||
content: [], | ||
data: helpers_1.getDataOfDefault(slateNode.data), | ||
}; | ||
if (!schema.isVoid(contentfulBlock)) { | ||
contentfulBlock.content = content; | ||
} | ||
nodes.push(contentfulBlock); | ||
@@ -42,3 +48,2 @@ break; | ||
return node.leaves.map(function (leaf) { return ({ | ||
nodeClass: 'text', | ||
nodeType: 'text', | ||
@@ -45,0 +50,0 @@ value: leaf.text, |
@@ -1,2 +0,2 @@ | ||
import * as Contentful from '@contentful/structured-text-types'; | ||
import * as Contentful from '@contentful/rich-text-types'; | ||
export interface NodeProps { | ||
@@ -3,0 +3,0 @@ isVoid?: boolean; |
@@ -1,14 +0,7 @@ | ||
import * as Contentful from '@contentful/structured-text-types'; | ||
export interface SchemaValue { | ||
isVoid?: boolean; | ||
[k: string]: any; | ||
} | ||
export interface Schema { | ||
blocks?: Record<string, SchemaValue>; | ||
inlines?: Record<string, SchemaValue>; | ||
} | ||
import * as Contentful from '@contentful/rich-text-types'; | ||
import { SchemaJSON } from './schema'; | ||
export interface ToSlatejsDocumentProperties { | ||
document: Contentful.Document; | ||
schema?: Schema; | ||
schema?: SchemaJSON; | ||
} | ||
export default function toSlatejsDocument({ document, schema, }: ToSlatejsDocumentProperties): Slate.Document; |
@@ -1,2 +0,7 @@ | ||
import * as Contentful from '@contentful/structured-text-types'; | ||
export default function toContentfulDocument(slateDocument: Slate.Document): Contentful.Document; | ||
import * as Contentful from '@contentful/rich-text-types'; | ||
import { SchemaJSON } from './schema'; | ||
export interface ToContentfulDocumentProperties { | ||
document: Slate.Document; | ||
schema?: SchemaJSON; | ||
} | ||
export default function toContentfulDocument({ document, schema, }: ToContentfulDocumentProperties): Contentful.Document; |
@@ -1,3 +0,4 @@ | ||
import * as Contentful from '@contentful/structured-text-types'; | ||
import * as Contentful from '@contentful/rich-text-types'; | ||
export declare type ContentfulNode = Contentful.Block | Contentful.Inline | Contentful.Text; | ||
export declare type SlateNode = Slate.Block | Slate.Inline | Slate.Text; | ||
export declare type ContentfulNonTextNodes = Contentful.Block | Contentful.Inline; |
declare namespace Slate { | ||
type NodeObject = 'document' | 'block' | 'inline' | 'text'; | ||
interface Node { | ||
object: 'document' | 'block' | 'inline' | 'text'; | ||
object: NodeObject; | ||
type?: string; | ||
@@ -5,0 +6,0 @@ data?: object; |
{ | ||
"name": "@contentful/contentful-slatejs-adapter", | ||
"version": "3.3.0", | ||
"version": "5.0.0", | ||
"description": "", | ||
@@ -115,3 +115,3 @@ "keywords": [], | ||
"dependencies": { | ||
"@contentful/structured-text-types": "^3.4.0", | ||
"@contentful/rich-text-types": "^5.0.0", | ||
"lodash.flatmap": "^4.5.0", | ||
@@ -118,0 +118,0 @@ "lodash.get": "^4.4.2", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
65578
9175
35
851
+ Added@contentful/rich-text-types@5.0.0(transitive)
- Removed@contentful/structured-text-types@3.4.0(transitive)