mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-13 02:34:05 +02:00
* fix plain serializer handling empty blocks, fixes #1090 * clean up block list test
This commit is contained in:
@@ -11,3 +11,7 @@ trim_trailing_whitespace = true
|
|||||||
|
|
||||||
[*.md]
|
[*.md]
|
||||||
trim_trailing_whitespace = false
|
trim_trailing_whitespace = false
|
||||||
|
|
||||||
|
[*.txt]
|
||||||
|
insert_final_newline = false
|
||||||
|
trim_trailing_whitespace = false
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
|
|
||||||
|
import Block from '../models/block'
|
||||||
import Raw from '../serializers/raw'
|
import Raw from '../serializers/raw'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -58,9 +59,6 @@ function serialize(state) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Serialize a `node` to plain text.
|
* 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
|
* @param {Node} node
|
||||||
* @return {String}
|
* @return {String}
|
||||||
@@ -69,12 +67,9 @@ function serialize(state) {
|
|||||||
function serializeNode(node) {
|
function serializeNode(node) {
|
||||||
if (
|
if (
|
||||||
(node.kind == 'document') ||
|
(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
|
return node.nodes.map(serializeNode).join('\n')
|
||||||
.map(n => serializeNode(n))
|
|
||||||
.filter(text => text != '')
|
|
||||||
.join('\n')
|
|
||||||
} else {
|
} else {
|
||||||
return node.text
|
return node.text
|
||||||
}
|
}
|
||||||
|
@@ -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' },
|
||||||
|
])
|
||||||
|
}
|
||||||
|
])
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
@@ -0,0 +1,3 @@
|
|||||||
|
one
|
||||||
|
|
||||||
|
three
|
@@ -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' },
|
||||||
|
])
|
||||||
|
}
|
||||||
|
])
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
@@ -1,2 +1,3 @@
|
|||||||
one
|
one
|
||||||
two
|
two
|
||||||
|
three
|
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..'
|
import { Block, Character, Document, State, Text } from '../../../../../..'
|
||||||
|
|
||||||
export default State.create({
|
export default State.create({
|
||||||
document: Document.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' },
|
||||||
|
])
|
||||||
|
}
|
||||||
|
])
|
||||||
|
}
|
||||||
|
])
|
||||||
|
}
|
||||||
|
])
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
})
|
})
|
@@ -0,0 +1,4 @@
|
|||||||
|
one
|
||||||
|
two
|
||||||
|
|
||||||
|
four
|
@@ -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' },
|
||||||
|
])
|
||||||
|
}
|
||||||
|
])
|
||||||
|
}
|
||||||
|
])
|
||||||
|
}
|
||||||
|
])
|
||||||
|
}
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
@@ -0,0 +1,4 @@
|
|||||||
|
one
|
||||||
|
two
|
||||||
|
three
|
||||||
|
four
|
@@ -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' },
|
||||||
|
])
|
||||||
|
}
|
||||||
|
])
|
||||||
|
}
|
||||||
|
])
|
||||||
|
},
|
||||||
|
])
|
||||||
|
},
|
||||||
|
])
|
||||||
|
}
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
@@ -0,0 +1,4 @@
|
|||||||
|
one
|
||||||
|
two
|
||||||
|
three
|
||||||
|
four
|
@@ -173,7 +173,7 @@ describe('serializers', () => {
|
|||||||
const input = require(resolve(innerDir, 'input.js')).default
|
const input = require(resolve(innerDir, 'input.js')).default
|
||||||
const expected = fs.readFileSync(resolve(innerDir, 'output.txt'), 'utf8')
|
const expected = fs.readFileSync(resolve(innerDir, 'output.txt'), 'utf8')
|
||||||
const serialized = Plain.serialize(input)
|
const serialized = Plain.serialize(input)
|
||||||
assert.deepEqual(serialized, expected.replace(/\n$/m, ''))
|
assert.deepEqual(serialized, expected)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user