1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-28 09:29:49 +02:00

Fix how plain text serializer serializes nested block (#1028)

* Fix how plain text serializer serializes nested blocks

* Updated test to check for more nested blocks

* Renamed getSerializedTextForNode to serializeNode, and added comment block

* Added a test for plain serializer: nested blocks with nested inlines

* Added additional test for simple inlines within block

* fixed expected test output

* fixed case where block has inlines but not other nested blocks
This commit is contained in:
Anuj
2017-09-05 17:48:06 -07:00
committed by Ian Storm Taylor
parent 9b5e7ba306
commit d21728ed86
7 changed files with 149 additions and 3 deletions

View File

@@ -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
}
}
/**

View File

@@ -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' }
])
}
])
}
])
}
])
}
])
}
])
}
])
})
})

View File

@@ -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' }
])
}
])
}
])
}
])
})
})

View File

@@ -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' }
])
}
])
}
])
}
])
}