Skip to content

Commit

Permalink
feat: 🎸 Explicitly declare nodeClass and nodeType in core types
Browse files Browse the repository at this point in the history
This is necessary to ensure disambiguation between block, inline, and
text elements. Additionally, this codifies the rich text specification
as provided by the CEP (alternatives wouldn't make it through our
validation layer, and should not be supported as such).

BREAKING CHANGE: Potentially breaks TypeScript libraries pulling this in as a dependency
if they are not explicitly stating nodeClass and nodeType.
  • Loading branch information
Chris Sloop committed Oct 30, 2018
1 parent 3a74778 commit 0749c61
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Document, BLOCKS } from '@contentful/rich-text-types';

export default function(entry: Object) {
return {
nodeType: 'document',
nodeClass: 'block',
data: {},
content: [
{
nodeType: BLOCKS.EMBEDDED_ENTRY,
nodeClass: 'block',
content: [],
data: {
target: entry,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { Document } from '@contentful/rich-text-types';

export default function(heading: string) {
return {
nodeType: 'document',
nodeClass: 'block',
data: {},
content: [
{
nodeType: heading,
nodeClass: 'block',
data: {},
content: [
{
nodeType: 'text',
value: 'hello world',
marks: [],
data: {},
},
],
},
Expand Down
8 changes: 8 additions & 0 deletions packages/rich-text-html-renderer/src/__test__/documents/hr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,41 @@ export default {
marks: [],
nodeType: 'text',
value: 'hello world',
data: {},
},
],
data: {},
nodeType: 'paragraph',
nodeClass: 'block',
},
{
content: [
{
marks: [],
nodeType: 'text',
value: '',
data: {},
},
],
data: {},
nodeType: 'hr',
nodeClass: 'block',
},
{
content: [
{
marks: [],
nodeType: 'text',
value: '',
data: {},
},
],
data: {},
nodeType: 'paragraph',
nodeClass: 'block',
},
],
data: {},
nodeType: 'document',
nodeClass: 'block',
} as Document;
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@ import { Document } from '@contentful/rich-text-types';

export default {
nodeType: 'document',
nodeClass: 'block',
data: {},
content: [
{
nodeType: 'paragraph',
nodeClass: 'block',
data: {},
content: [
{
nodeType: 'text',
value: 'Some text ',
marks: [],
data: {},
},
{
nodeType: 'hyperlink',
nodeClass: 'inline',
content: [
{
nodeType: 'text',
value: 'link',
marks: [],
data: {},
},
],
data: {
Expand All @@ -28,9 +35,9 @@ export default {
nodeType: 'text',
value: ' text.',
marks: [],
data: {},
},
],
data: {},
},
],
} as Document;
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function inlineEntity(entry: Object, inlineType: string) {
marks: [],
value: '',
nodeType: 'text',
data: {},
},
{
data: entry,
Expand All @@ -18,19 +19,25 @@ export default function inlineEntity(entry: Object, inlineType: string) {
marks: [],
value: '',
nodeType: 'text',
data: {},
},
],
nodeType: inlineType,
nodeClass: 'inline',
},
{
marks: [],
value: '',
nodeType: 'text',
data: {},
},
],
nodeType: 'paragraph',
nodeClass: 'block',
},
],
data: {},
nodeType: 'document',
nodeClass: 'block',
} as Document;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { Document } from '@contentful/rich-text-types';

export default {
nodeType: 'document',
nodeClass: 'block',
data: {},
content: [
{
nodeType: 'paragraph',
nodeClass: 'block',
data: {},
content: [
{
nodeType: 'text',
Expand All @@ -14,6 +18,7 @@ export default {
type: 'UNRECOGNIZED_MARK',
},
],
data: {},
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import { Document } from '@contentful/rich-text-types';

export default {
nodeType: 'document',
nodeClass: 'block',
data: {},
content: [
{
nodeType: 'UNRECOGNIZED_TYPE',
nodeClass: 'block',
data: {},
content: [
{
nodeType: 'text',
value: 'Hello world!',
marks: [],
data: {},
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import { Document } from '@contentful/rich-text-types';
export default function(mark: String) {
return {
nodeType: 'document',
nodeClass: 'block',
data: {},
content: [
{
nodeType: 'paragraph',
nodeClass: 'block',
data: {},
content: [
{
nodeType: 'text',
value: 'hello world',
marks: [{ type: mark }],
data: {},
},
],
},
Expand Down
11 changes: 11 additions & 0 deletions packages/rich-text-html-renderer/src/__test__/documents/ol.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Document } from '@contentful/rich-text-types';

export default {
data: {},
content: [
{
data: {},
Expand All @@ -12,15 +13,18 @@ export default {
data: {},
content: [
{
data: {},
marks: [],
value: 'Hello',
nodeType: 'text',
},
],
nodeType: 'paragraph',
nodeClass: 'block',
},
],
nodeType: 'list-item',
nodeClass: 'block',
},
{
data: {},
Expand All @@ -29,30 +33,37 @@ export default {
data: {},
content: [
{
data: {},
marks: [],
value: 'world',
nodeType: 'text',
},
],
nodeType: 'paragraph',
nodeClass: 'block',
},
],
nodeType: 'list-item',
nodeClass: 'block',
},
],
nodeType: 'ordered-list',
nodeClass: 'block',
},
{
data: {},
content: [
{
data: {},
marks: [],
value: '',
nodeType: 'text',
},
],
nodeType: 'paragraph',
nodeClass: 'block',
},
],
nodeType: 'document',
nodeClass: 'block',
} as Document;
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import { Document } from '@contentful/rich-text-types';

export default {
nodeType: 'document',
nodeClass: 'block',
data: {},
content: [
{
nodeType: 'paragraph',
nodeClass: 'block',
data: {},
content: [
{
nodeType: 'text',
value: 'hello world',
marks: [],
data: {},
},
],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import { Document } from '@contentful/rich-text-types';

export default {
data: {},
content: [
{
data: {},
content: [
{
data: {},
marks: [],
value: 'hello',
nodeType: 'text',
},
],
nodeType: 'paragraph',
nodeClass: 'block',
},
{
data: {},
content: [
{
data: {},
marks: [],
value: 'world',
nodeType: 'text',
},
],
nodeType: 'blockquote',
nodeClass: 'block',
},
],
nodeType: 'document',
nodeClass: 'block',
} as Document;

0 comments on commit 0749c61

Please sign in to comment.