Skip to content

Commit

Permalink
fix plain serializer handling empty blocks, fixes #1090 (#1091)
Browse files Browse the repository at this point in the history
* fix plain serializer handling empty blocks, fixes #1090

* clean up block list test
  • Loading branch information
ianstormtaylor authored Sep 8, 2017
1 parent 3e2e533 commit 1b406c3
Show file tree
Hide file tree
Showing 23 changed files with 326 additions and 20 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.txt]
insert_final_newline = false
trim_trailing_whitespace = false
11 changes: 3 additions & 8 deletions src/serializers/plain.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import Block from '../models/block'
import Raw from '../serializers/raw'

/**
Expand Down Expand Up @@ -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}
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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' },
])
}
])
},
])
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
one

three
47 changes: 47 additions & 0 deletions test/serializers/fixtures/plain/serialize/block-multiple/input.js
Original file line number Diff line number Diff line change
@@ -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' },
])
}
])
},
])
})
})
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
one
two
three
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -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' },
])
}
])
}
])
}
])
}
])
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
one
two

four
Original file line number Diff line number Diff line change
@@ -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' },
])
}
])
}
])
}
])
}
])
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
one
two
three
four
Original file line number Diff line number Diff line change
@@ -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' },
])
}
])
}
])
},
])
},
])
}
])
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
one
two
three
four
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
one
two
two
Original file line number Diff line number Diff line change
@@ -1 +1 @@
onetwo
onetwo
Original file line number Diff line number Diff line change
@@ -1 +1 @@
one
one
Original file line number Diff line number Diff line change
@@ -1 +1 @@


2 changes: 1 addition & 1 deletion test/serializers/fixtures/plain/serialize/block/output.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
one
one
Loading

0 comments on commit 1b406c3

Please sign in to comment.