mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-30 02:19:52 +02:00
Add possibility to specify a custom delimiter to slate-plain-serializer (#2266)
* Add possibility to specify a custom delimiter * Add custom delimiter to the serialize method * Fix lint
This commit is contained in:
committed by
Ian Storm Taylor
parent
0deb2e2695
commit
861f30a3f9
@@ -13,7 +13,12 @@ import { Set } from 'immutable'
|
||||
*/
|
||||
|
||||
function deserialize(string, options = {}) {
|
||||
let { defaultBlock = 'line', defaultMarks = [], toJSON = false } = options
|
||||
let {
|
||||
defaultBlock = 'line',
|
||||
defaultMarks = [],
|
||||
delimiter = '\n',
|
||||
toJSON = false,
|
||||
} = options
|
||||
|
||||
if (Set.isSet(defaultMarks)) {
|
||||
defaultMarks = defaultMarks.toArray()
|
||||
@@ -27,7 +32,7 @@ function deserialize(string, options = {}) {
|
||||
document: {
|
||||
object: 'document',
|
||||
data: {},
|
||||
nodes: string.split('\n').map(line => {
|
||||
nodes: string.split(delimiter).map(line => {
|
||||
return {
|
||||
...defaultBlock,
|
||||
object: 'block',
|
||||
@@ -60,8 +65,8 @@ function deserialize(string, options = {}) {
|
||||
* @return {String}
|
||||
*/
|
||||
|
||||
function serialize(value) {
|
||||
return serializeNode(value.document)
|
||||
function serialize(value, options = {}) {
|
||||
return serializeNode(value.document, options)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,12 +76,14 @@ function serialize(value) {
|
||||
* @return {String}
|
||||
*/
|
||||
|
||||
function serializeNode(node) {
|
||||
function serializeNode(node, options = {}) {
|
||||
const { delimiter = '\n' } = options
|
||||
|
||||
if (
|
||||
node.object == 'document' ||
|
||||
(node.object == 'block' && Block.isBlockList(node.nodes))
|
||||
) {
|
||||
return node.nodes.map(serializeNode).join('\n')
|
||||
return node.nodes.map(serializeNode).join(delimiter)
|
||||
} else {
|
||||
return node.text
|
||||
}
|
||||
|
@@ -0,0 +1,24 @@
|
||||
/** @jsx h */
|
||||
|
||||
import h from '../helpers/h'
|
||||
|
||||
export const input = `
|
||||
one
|
||||
same paragraph
|
||||
|
||||
two
|
||||
`.trim()
|
||||
|
||||
export const output = (
|
||||
<value>
|
||||
<document>
|
||||
<paragraph>one{'\n'}same paragraph</paragraph>
|
||||
<paragraph>two</paragraph>
|
||||
</document>
|
||||
</value>
|
||||
)
|
||||
|
||||
export const options = {
|
||||
defaultBlock: 'paragraph',
|
||||
delimiter: '\n\n',
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
/** @jsx h */
|
||||
|
||||
import h from '../helpers/h'
|
||||
|
||||
export const input = (
|
||||
<value>
|
||||
<document>
|
||||
<paragraph>one{'\n'}same paragraph</paragraph>
|
||||
<paragraph>two</paragraph>
|
||||
<paragraph>three</paragraph>
|
||||
</document>
|
||||
</value>
|
||||
)
|
||||
|
||||
export const output = `
|
||||
one
|
||||
same paragraph
|
||||
|
||||
two
|
||||
|
||||
three
|
||||
`.trim()
|
||||
|
||||
export const options = {
|
||||
delimiter: '\n\n',
|
||||
}
|
Reference in New Issue
Block a user