1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-21 23:53:50 +01:00

do not throw too early while looking for a rule with which to serialize (#205)

This commit is contained in:
Robin Berjon 2016-07-29 17:54:47 +02:00 committed by Ian Storm Taylor
parent d2647cde71
commit 9ab6267271
4 changed files with 53 additions and 2 deletions

View File

@ -245,8 +245,8 @@ class Html {
if (!rule.serialize) continue
const ret = rule.serialize(node, children)
if (ret) return addKey(ret)
throw new Error(`No serializer defined for node of type "${node.type}".`)
}
throw new Error(`No serializer defined for node of type "${node.type}".`)
}
/**
@ -265,8 +265,8 @@ class Html {
if (!rule.serialize) continue
const ret = rule.serialize(mark, children)
if (ret) return addKey(ret)
throw new Error(`No serializer defined for mark of type "${mark.type}".`)
}
throw new Error(`No serializer defined for mark of type "${mark.type}".`)
}, text)
}

View File

@ -0,0 +1,24 @@
import React from 'react'
export default {
rules: [
// the first one has no serialize()
{},
// the second has a serialize() that does not return anything
{
serialize(obj, children) {}
},
// then the real one
{
serialize(obj, children) {
if (obj.kind == 'block' && obj.type == 'paragraph') {
return <p>{children}</p>
}
if (obj.kind == 'inline' && obj.type == 'link') {
return <a>{children}</a>
}
}
}
]
}

View File

@ -0,0 +1,26 @@
import { Block, Character, Document, Inline, Mark, 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: 't' },
{ text: 'w' },
{ text: 'o' }
])
}
])
}
])
}
])
})
})

View File

@ -0,0 +1 @@
<p><a>two</a></p>