diff --git a/src/serializers/plain.js b/src/serializers/plain.js index 81491b63a..d750b283f 100644 --- a/src/serializers/plain.js +++ b/src/serializers/plain.js @@ -38,6 +38,21 @@ function deserialize(string, options = {}) { return options.toRaw ? raw : Raw.deserialize(raw) } +/** + * Checks if the block has other blocks nested within + * @param {Node} node + * @return {Boolean} +*/ + +function hasNestedBlocks(node) { + return node && + node.nodes && + node.nodes.first() && + node.nodes.first().kind && + node.nodes.first().kind == 'block' +} + + /** * Serialize a `state` to plain text. * @@ -46,9 +61,28 @@ function deserialize(string, options = {}) { */ function serialize(state) { - return state.document.nodes - .map(block => block.text) - .join('\n') + return serializeNode(state.document) +} + +/** + * Serialize a `node` to plain text. + * For blocks, or document, it recursively calls itself + * to aggregate the text. + * For other types of nodes, it uses the .text property + * + * @param {Node} node + * @return {String} + */ + +function serializeNode(node) { + if (node.kind == 'document' || (node.kind == 'block' && hasNestedBlocks(node))) { + return node.nodes + .map(childNode => serializeNode(childNode)) + .filter(text => text != '') + .join('\n') + } else { + return node.text + } } /** diff --git a/test/serializers/fixtures/plain/serialize/block-nested-with-inline-nested/input.js b/test/serializers/fixtures/plain/serialize/block-nested-with-inline-nested/input.js new file mode 100644 index 000000000..46fe09770 --- /dev/null +++ b/test/serializers/fixtures/plain/serialize/block-nested-with-inline-nested/input.js @@ -0,0 +1,53 @@ + +import { Block, Character, Document, Inline, State, Text } from '../../../../../..' + +export default State.create({ + document: Document.create({ + nodes: Block.createList([ + { + type: 'quote', + nodes: Block.createList([ + { + type: 'paragraph', + nodes: Text.createList([ + { + characters: Character.createList([ + { text: 'o' }, + { text: 'n' }, + { text: 'e' } + ]) + } + ]) + }, + { + type: 'paragraph', + nodes: Block.createList([ + { + type: 'paragraph', + nodes: Inline.createList([ + { + type: 'link', + nodes: Inline.createList([ + { + type: 'hashtag', + nodes: Text.createList([ + { + characters: Character.createList([ + { text: 't' }, + { text: 'w' }, + { text: 'o' } + ]) + } + ]) + } + ]) + } + ]) + } + ]) + } + ]) + } + ]) + }) +}) diff --git a/test/serializers/fixtures/plain/serialize/block-nested-with-inline-nested/output.txt b/test/serializers/fixtures/plain/serialize/block-nested-with-inline-nested/output.txt new file mode 100644 index 000000000..814f4a422 --- /dev/null +++ b/test/serializers/fixtures/plain/serialize/block-nested-with-inline-nested/output.txt @@ -0,0 +1,2 @@ +one +two diff --git a/test/serializers/fixtures/plain/serialize/block-nested-with-inlines/input.js b/test/serializers/fixtures/plain/serialize/block-nested-with-inlines/input.js new file mode 100644 index 000000000..883fe3884 --- /dev/null +++ b/test/serializers/fixtures/plain/serialize/block-nested-with-inlines/input.js @@ -0,0 +1,38 @@ + +import { Block, Character, Document, Inline, State, Text } from '../../../../../..' + +export default State.create({ + document: Document.create({ + nodes: Block.createList([ + { + type: 'paragraph', + nodes: Inline.createList([ + { + type: 'link', + nodes: Text.createList([ + { + characters: Character.createList([ + { text: 'o' }, + { text: 'n' }, + { text: 'e' } + ]) + } + ]) + }, + { + type: 'link', + nodes: Text.createList([ + { + characters: Character.createList([ + { text: 't' }, + { text: 'w' }, + { text: 'o' } + ]) + } + ]) + } + ]) + } + ]) + }) +}) diff --git a/test/serializers/fixtures/plain/serialize/block-nested-with-inlines/output.txt b/test/serializers/fixtures/plain/serialize/block-nested-with-inlines/output.txt new file mode 100644 index 000000000..1d202cae0 --- /dev/null +++ b/test/serializers/fixtures/plain/serialize/block-nested-with-inlines/output.txt @@ -0,0 +1 @@ +onetwo diff --git a/test/serializers/fixtures/plain/serialize/block-nested/input.js b/test/serializers/fixtures/plain/serialize/block-nested/input.js index 3e88fa872..b39a58392 100644 --- a/test/serializers/fixtures/plain/serialize/block-nested/input.js +++ b/test/serializers/fixtures/plain/serialize/block-nested/input.js @@ -18,6 +18,23 @@ export default State.create({ ]) } ]) + }, + { + type: 'paragraph', + nodes: Block.createList([ + { + type: 'paragraph', + nodes: Text.createList([ + { + characters: Character.createList([ + { text: 't' }, + { text: 'w' }, + { text: 'o' } + ]) + } + ]) + } + ]) } ]) } diff --git a/test/serializers/fixtures/plain/serialize/block-nested/output.txt b/test/serializers/fixtures/plain/serialize/block-nested/output.txt index 5626abf0f..814f4a422 100644 --- a/test/serializers/fixtures/plain/serialize/block-nested/output.txt +++ b/test/serializers/fixtures/plain/serialize/block-nested/output.txt @@ -1 +1,2 @@ one +two