1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-04-13 18:01:53 +02:00

fix plain serializer handling empty blocks, fixes #1090 (#1091)

* fix plain serializer handling empty blocks, fixes #1090

* clean up block list test
This commit is contained in:
Ian Storm Taylor 2017-09-08 00:09:46 -07:00 committed by GitHub
parent 3e2e533c21
commit 1b406c345f
23 changed files with 326 additions and 20 deletions

View File

@ -11,3 +11,7 @@ trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false
[*.txt]
insert_final_newline = false
trim_trailing_whitespace = false

View File

@ -1,4 +1,5 @@
import Block from '../models/block'
import Raw from '../serializers/raw'
/**
@ -58,9 +59,6 @@ function serialize(state) {
/**
* 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}
@ -69,12 +67,9 @@ function serialize(state) {
function serializeNode(node) {
if (
(node.kind == 'document') ||
(node.kind == 'block' && node.nodes.size > 0 && node.nodes.first().kind == 'block')
(node.kind == 'block' && Block.isBlockList(node.nodes))
) {
return node.nodes
.map(n => serializeNode(n))
.filter(text => text != '')
.join('\n')
return node.nodes.map(serializeNode).join('\n')
} else {
return node.text
}

View File

@ -0,0 +1,43 @@
import { Block, Character, Document, State, Text } from '../../../../../..'
export default State.create({
document: Document.create({
nodes: Block.createList([
{
type: 'paragraph',
nodes: Text.createList([
{
characters: Character.createList([
{ text: 'o' },
{ text: 'n' },
{ text: 'e' },
])
}
])
},
{
type: 'paragraph',
nodes: Text.createList([
{
characters: Character.createList()
}
])
},
{
type: 'paragraph',
nodes: Text.createList([
{
characters: Character.createList([
{ text: 't' },
{ text: 'h' },
{ text: 'r' },
{ text: 'e' },
{ text: 'e' },
])
}
])
},
])
})
})

View File

@ -0,0 +1,3 @@
one
three

View File

@ -0,0 +1,47 @@
import { Block, Character, Document, State, Text } from '../../../../../..'
export default State.create({
document: Document.create({
nodes: Block.createList([
{
type: 'paragraph',
nodes: Text.createList([
{
characters: Character.createList([
{ text: 'o' },
{ text: 'n' },
{ text: 'e' },
])
}
])
},
{
type: 'paragraph',
nodes: Text.createList([
{
characters: Character.createList([
{ text: 't' },
{ text: 'w' },
{ text: 'o' },
])
}
])
},
{
type: 'paragraph',
nodes: Text.createList([
{
characters: Character.createList([
{ text: 't' },
{ text: 'h' },
{ text: 'r' },
{ text: 'e' },
{ text: 'e' },
])
}
])
},
])
})
})

View File

@ -1,5 +1,5 @@
import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..'
import { Block, Character, Document, State, Text } from '../../../../../..'
export default State.create({
document: Document.create({
@ -37,6 +37,37 @@ export default State.create({
])
}
])
},
{
type: 'quote',
nodes: Block.createList([
{
type: 'paragraph',
nodes: Text.createList([
{
characters: Character.createList()
}
])
},
{
type: 'paragraph',
nodes: Block.createList([
{
type: 'paragraph',
nodes: Text.createList([
{
characters: Character.createList([
{ text: 'f' },
{ text: 'o' },
{ text: 'u' },
{ text: 'r' },
])
}
])
}
])
}
])
}
])
})

View File

@ -0,0 +1,4 @@
one
two
four

View File

@ -0,0 +1,80 @@
import { Block, Character, Document, 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: Text.createList([
{
characters: Character.createList([
{ text: 't' },
{ text: 'w' },
{ text: 'o' }
])
}
])
}
])
}
])
},
{
type: 'quote',
nodes: Block.createList([
{
type: 'paragraph',
nodes: Text.createList([
{
characters: Character.createList([
{ text: 't' },
{ text: 'h' },
{ text: 'r' },
{ text: 'e' },
{ text: 'e' },
])
}
])
},
{
type: 'paragraph',
nodes: Block.createList([
{
type: 'paragraph',
nodes: Text.createList([
{
characters: Character.createList([
{ text: 'f' },
{ text: 'o' },
{ text: 'u' },
{ text: 'r' },
])
}
])
}
])
}
])
}
])
})
})

View File

@ -0,0 +1,4 @@
one
two
three
four

View File

@ -0,0 +1,90 @@
import { Block, Character, Document, State, Text } from '../../../../../..'
export default State.create({
document: Document.create({
nodes: Block.createList([
{
type: 'quote',
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: Text.createList([
{
characters: Character.createList([
{ text: 't' },
{ text: 'w' },
{ text: 'o' },
])
}
])
}
])
},
])
},
{
type: 'quote',
nodes: Block.createList([
{
type: 'paragraph',
nodes: Block.createList([
{
type: 'paragraph',
nodes: Text.createList([
{
characters: Character.createList([
{ text: 't' },
{ text: 'h' },
{ text: 'r' },
{ text: 'e' },
{ text: 'e' },
])
}
])
}
])
},
{
type: 'paragraph',
nodes: Block.createList([
{
type: 'paragraph',
nodes: Text.createList([
{
characters: Character.createList([
{ text: 'f' },
{ text: 'o' },
{ text: 'u' },
{ text: 'r' },
])
}
])
}
])
},
])
},
])
}
])
})
})

View File

@ -0,0 +1,4 @@
one
two
three
four

View File

@ -173,7 +173,7 @@ describe('serializers', () => {
const input = require(resolve(innerDir, 'input.js')).default
const expected = fs.readFileSync(resolve(innerDir, 'output.txt'), 'utf8')
const serialized = Plain.serialize(input)
assert.deepEqual(serialized, expected.replace(/\n$/m, ''))
assert.deepEqual(serialized, expected)
})
}
})