@contentful/contentful-slatejs-adapter
Advanced tools
Comparing version 2.1.0 to 3.0.0
@@ -8,11 +8,18 @@ 'use strict'; | ||
var flatmap = _interopDefault(require('lodash.flatmap')); | ||
var get = _interopDefault(require('lodash.get')); | ||
var Contentful = require('@contentful/structured-text-types'); | ||
function toSlatejsDocument(ctfDocument) { | ||
function toSlatejsDocument(_a) { | ||
var document = _a.document, _b = _a.schema, schema = _b === void 0 ? {} : _b; | ||
return { | ||
object: 'document', | ||
nodes: flatmap(ctfDocument.content, function (node) { return convertNode(node); }), | ||
nodes: flatmap(document.content, function (node) { return convertNode(node, schema); }), | ||
}; | ||
} | ||
function convertNode(node) { | ||
// 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 = []; | ||
@@ -23,3 +30,5 @@ switch (node.nodeClass) { | ||
var contentfulBlock = node; | ||
var childNodes = flatmap(contentfulBlock.content, convertNode); | ||
var childNodes = flatmap(contentfulBlock.content, function (childNode) { | ||
return convertNode(childNode, schema); | ||
}); | ||
var slateBlock = { | ||
@@ -29,2 +38,3 @@ object: contentfulBlock.nodeClass, | ||
nodes: childNodes, | ||
isVoid: getIsVoidValue(contentfulBlock, schema), | ||
data: contentfulBlock.data, | ||
@@ -31,0 +41,0 @@ }; |
@@ -8,19 +8,28 @@ "use strict"; | ||
var contentful = require("./contentful-helpers"); | ||
describe('toSlatejsDocument', function () { | ||
var testFactory = function (message, contentfulDoc, expected) { | ||
it(message, function () { | ||
var actualSlateDoc = contentful_to_slatejs_adapter_1.default(contentfulDoc); | ||
expect(actualSlateDoc).toEqual(expected); | ||
var actualContentfulDoc = slatejs_to_contentful_adapter_1.default(actualSlateDoc); | ||
expect(actualContentfulDoc).toEqual(contentfulDoc); | ||
describe('adapters', function () { | ||
var testAdapters = function (message, contentfulDoc, slateDoc) { | ||
describe('toSlatejsDocument()', function () { | ||
it(message, function () { | ||
var actualSlateDoc = contentful_to_slatejs_adapter_1.default({ | ||
document: contentfulDoc, | ||
schema: { blocks: { voidnode: { isVoid: true } } }, | ||
}); | ||
expect(actualSlateDoc).toEqual(slateDoc); | ||
}); | ||
}); | ||
describe('toContentfulDocument()', function () { | ||
it(message, function () { | ||
var actualContentfulDoc = slatejs_to_contentful_adapter_1.default(slateDoc); | ||
expect(actualContentfulDoc).toEqual(contentfulDoc); | ||
}); | ||
}); | ||
}; | ||
describe('document', function () { | ||
testFactory('empty document', contentful.document(), slate.document()); | ||
testFactory('document with block', contentful.document(contentful.block('paragraph', contentful.text(''))), slate.document(slate.block('paragraph', slate.text(slate.leaf(''))))); | ||
testFactory('paragraph with inline', contentful.document(contentful.block('paragraph', contentful.inline('hyperlink'))), slate.document(slate.block('paragraph', slate.inline('hyperlink')))); | ||
testFactory('paragraph with text', contentful.document(contentful.block('paragraph', contentful.text('hi'))), slate.document(slate.block('paragraph', slate.text(slate.leaf('hi'))))); | ||
testFactory('text with marks', contentful.document(contentful.block('paragraph', contentful.text('this'), contentful.text('is', contentful.mark('bold')))), slate.document(slate.block('paragraph', slate.text(slate.leaf('this')), slate.text(slate.leaf('is', contentful.mark('bold')))))); | ||
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', contentful.mark('bold')))))); | ||
it('adds a default value to marks if undefined', function () { | ||
var slateDoc = slate.document(slate.block('paragraph', slate.text({ marks: undefined, object: 'leaf', text: 'Hi' }))); | ||
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); | ||
@@ -34,7 +43,7 @@ expect(ctflDoc).toEqual(contentful.document(contentful.block('paragraph', { | ||
}); | ||
testFactory('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', slate.text(slate.leaf('this')), slate.text(slate.leaf('is', slate.mark('bold'))), slate.text(slate.leaf('huge', slate.mark('bold'), slate.mark('italic')))))); | ||
testFactory('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', slate.text(slate.leaf('this is a test', slate.mark('bold'))), slate.text(slate.leaf('paragraph', slate.mark('underline')))), slate.block('block', slate.block('block', slate.text(slate.leaf('this is it')))))); | ||
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')))))); | ||
}); | ||
describe('converts additional data', function () { | ||
testFactory('data in block', { | ||
testAdapters('data in block', { | ||
nodeClass: 'document', | ||
@@ -56,2 +65,3 @@ nodeType: Contentful.BLOCKS.DOCUMENT, | ||
type: 'paragraph', | ||
isVoid: false, | ||
data: { a: 1 }, | ||
@@ -62,3 +72,3 @@ nodes: [], | ||
}); | ||
testFactory('data in inline', { | ||
testAdapters('data in inline', { | ||
nodeClass: 'document', | ||
@@ -87,2 +97,3 @@ nodeType: Contentful.BLOCKS.DOCUMENT, | ||
type: 'paragraph', | ||
isVoid: false, | ||
data: { a: 1 }, | ||
@@ -93,2 +104,3 @@ nodes: [ | ||
type: 'hyperlink', | ||
isVoid: false, | ||
data: { | ||
@@ -103,3 +115,3 @@ a: 2, | ||
}); | ||
testFactory('data in text', { | ||
testAdapters('data in text', { | ||
nodeClass: 'document', | ||
@@ -135,2 +147,3 @@ nodeType: Contentful.BLOCKS.DOCUMENT, | ||
type: 'paragraph', | ||
isVoid: false, | ||
data: { a: 1 }, | ||
@@ -141,2 +154,3 @@ nodes: [ | ||
type: 'hyperlink', | ||
isVoid: false, | ||
data: { | ||
@@ -163,3 +177,28 @@ a: 2, | ||
}); | ||
describe('sets isVoid from schema', function () { | ||
testAdapters('data in block', { | ||
nodeClass: 'document', | ||
nodeType: Contentful.BLOCKS.DOCUMENT, | ||
content: [ | ||
{ | ||
nodeClass: 'block', | ||
nodeType: 'voidnode', | ||
content: [], | ||
data: { a: 1 }, | ||
}, | ||
], | ||
}, { | ||
object: 'document', | ||
nodes: [ | ||
{ | ||
object: 'block', | ||
type: 'voidnode', | ||
isVoid: true, | ||
data: { a: 1 }, | ||
nodes: [], | ||
}, | ||
], | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=contentful-to-slatejs-adapter.test.js.map |
@@ -14,9 +14,10 @@ "use strict"; | ||
exports.document = document; | ||
function block(type) { | ||
function block(type, isVoid) { | ||
var nodes = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
nodes[_i - 1] = arguments[_i]; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
nodes[_i - 2] = arguments[_i]; | ||
} | ||
return { | ||
object: 'block', | ||
isVoid: isVoid, | ||
type: type, | ||
@@ -27,9 +28,10 @@ nodes: nodes, | ||
exports.block = block; | ||
function inline(type) { | ||
function inline(type, isVoid) { | ||
var nodes = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
nodes[_i - 1] = arguments[_i]; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
nodes[_i - 2] = arguments[_i]; | ||
} | ||
return { | ||
object: 'inline', | ||
isVoid: isVoid, | ||
type: type, | ||
@@ -36,0 +38,0 @@ nodes: nodes, |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var lodash_flatmap_1 = require("lodash.flatmap"); | ||
function toSlatejsDocument(ctfDocument) { | ||
var lodash_get_1 = require("lodash.get"); | ||
function toSlatejsDocument(_a) { | ||
var document = _a.document, _b = _a.schema, schema = _b === void 0 ? {} : _b; | ||
return { | ||
object: 'document', | ||
nodes: lodash_flatmap_1.default(ctfDocument.content, function (node) { return convertNode(node); }), | ||
nodes: lodash_flatmap_1.default(document.content, function (node) { return convertNode(node, schema); }), | ||
}; | ||
} | ||
exports.default = toSlatejsDocument; | ||
function convertNode(node) { | ||
// 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 = []; | ||
@@ -17,3 +24,5 @@ switch (node.nodeClass) { | ||
var contentfulBlock = node; | ||
var childNodes = lodash_flatmap_1.default(contentfulBlock.content, convertNode); | ||
var childNodes = lodash_flatmap_1.default(contentfulBlock.content, function (childNode) { | ||
return convertNode(childNode, schema); | ||
}); | ||
var slateBlock = { | ||
@@ -23,2 +32,3 @@ object: contentfulBlock.nodeClass, | ||
nodes: childNodes, | ||
isVoid: getIsVoidValue(contentfulBlock, schema), | ||
data: contentfulBlock.data, | ||
@@ -25,0 +35,0 @@ }; |
export declare function document(...nodes: Slate.Block[]): Slate.Document; | ||
export declare function block(type: string, ...nodes: Array<Slate.Block | Slate.Inline | Slate.Text>): Slate.Block; | ||
export declare function inline(type: string, ...nodes: Array<Slate.Inline | Slate.Text>): Slate.Inline; | ||
export declare function block(type: string, isVoid: false, ...nodes: Array<Slate.Block | Slate.Inline | Slate.Text>): Slate.Block; | ||
export declare function inline(type: string, isVoid: false, ...nodes: Array<Slate.Inline | Slate.Text>): Slate.Inline; | ||
export declare function text(...leaves: Slate.TextLeaf[]): Slate.Text; | ||
export declare function leaf(value: string, ...marks: Slate.Mark[]): Slate.TextLeaf; | ||
export declare function mark(type: string): Slate.Mark; |
import * as Contentful from '@contentful/structured-text-types'; | ||
export default function toSlatejsDocument(ctfDocument: Contentful.Document): Slate.Document; | ||
export interface SchemaValue { | ||
isVoid?: boolean; | ||
[k: string]: any; | ||
} | ||
export interface Schema { | ||
blocks?: Record<string, SchemaValue>; | ||
inlines?: Record<string, SchemaValue>; | ||
} | ||
export interface ToSlatejsDocumentProperties { | ||
document: Contentful.Document; | ||
schema?: Schema; | ||
} | ||
export default function toSlatejsDocument({ document, schema, }: ToSlatejsDocumentProperties): Slate.Document; |
@@ -6,2 +6,3 @@ declare namespace Slate { | ||
data?: object; | ||
isVoid?: boolean; | ||
} | ||
@@ -8,0 +9,0 @@ interface Document extends Node { |
{ | ||
"name": "@contentful/contentful-slatejs-adapter", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "", | ||
@@ -86,2 +86,3 @@ "keywords": [], | ||
"@types/lodash.flatmap": "^4.5.3", | ||
"@types/lodash.get": "^4.4.4", | ||
"@types/lodash.omit": "^4.5.3", | ||
@@ -125,4 +126,5 @@ "@types/node": "^10.0.3", | ||
"lodash.flatmap": "^4.5.0", | ||
"lodash.get": "^4.4.2", | ||
"lodash.omit": "^4.5.0" | ||
} | ||
} |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48321
611
4
34
+ Addedlodash.get@^4.4.2
+ Addedlodash.get@4.4.2(transitive)