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:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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' }
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
})
|
||||
})
|
@@ -0,0 +1,2 @@
|
||||
one
|
||||
two
|
@@ -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' }
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
})
|
||||
})
|
@@ -0,0 +1 @@
|
||||
onetwo
|
@@ -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' }
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
|
@@ -1 +1,2 @@
|
||||
one
|
||||
two
|
||||
|
Reference in New Issue
Block a user