Previous:
Saving to a Database
{children}
} }, }, ] ``` The `serialize` function should also feel familiar. It's just taking [Slate models](../reference/slate) and turning them into React elements, which will then be rendered to an HTML string. The `object` argument of the `serialize` function will either be a [`Node`](../reference/slate/node.md), a [`Mark`](../reference/slate/mark.md) or a special immutable [`String`](../reference/serializers/html.md#ruleserialize) object. And the `children` argument is a React element describing the nested children of the object in question, for recursing. Okay, so now our serializer can handle `paragraph` nodes. Let's add the other types of blocks we want: ```js // Refactor block tags into a dictionary for cleanliness. const BLOCK_TAGS = { p: 'paragraph', blockquote: 'quote', pre: 'code', } const rules = [ { // Switch deserialize to handle more blocks... deserialize(el, next) { const type = BLOCK_TAGS[el.tagName.toLowerCase()] if (type) { return { object: 'block', type: type, data: { className: el.getAttribute('class'), }, nodes: next(el.childNodes), } } }, // Switch serialize to handle more blocks... serialize(obj, children) { if (obj.object == 'block') { switch (obj.type) { case 'paragraph': return{children}
case 'quote': return{children}case 'code': return (
{children}
)
}
}
},
},
]
```
Now each of our block types is handled.
You'll notice that even though code blocks are nested in a `` and a `` element, we don't need to specifically handle that case in our `deserialize` function, because the `Html` serializer will automatically recurse through `el.childNodes` if no matching deserializer is found. This way, unknown tags will just be skipped over in the tree, instead of their contents omitted completely.
Okay. So now our serializer can handle blocks, but we need to add our marks to it as well. Let's do that with a new rule...
```js
const BLOCK_TAGS = {
blockquote: 'quote',
p: 'paragraph',
pre: 'code',
}
// Add a dictionary of mark tags.
const MARK_TAGS = {
em: 'italic',
strong: 'bold',
u: 'underline',
}
const rules = [
{
deserialize(el, next) {
const type = BLOCK_TAGS[el.tagName.toLowerCase()]
if (type) {
return {
object: 'block',
type: type,
data: {
className: el.getAttribute('class'),
},
nodes: next(el.childNodes),
}
}
},
serialize(obj, children) {
if (obj.object == 'block') {
switch (obj.type) {
case 'code':
return (
{children}
)
case 'paragraph':
return {children}
case 'quote':
return {children}
}
}
},
},
// Add a new rule that handles marks...
{
deserialize(el, next) {
const type = MARK_TAGS[el.tagName.toLowerCase()]
if (type) {
return {
object: 'mark',
type: type,
nodes: next(el.childNodes),
}
}
},
serialize(obj, children) {
if (obj.object == 'mark') {
switch (obj.type) {
case 'bold':
return {children}
case 'italic':
return {children}
case 'underline':
return {children}
}
}
},
},
]
```
Great, that's all of the rules we need! Now let's create a new `Html` serializer and pass in those rules:
```js
import Html from 'slate-html-serializer'
// Create a new serializer instance with our `rules` from above.
const html = new Html({ rules })
```
And finally, now that we have our serializer initialized, we can update our app to use it to save and load content, like so:
```js
// Load the initial value from Local Storage or a default.
const initialValue = localStorage.getItem('content') || ''
class App extends React.Component {
state = {
value: html.deserialize(initialValue),
}
onChange = ({ value }) => {
// When the document changes, save the serialized HTML to Local Storage.
if (value.document != this.state.value.document) {
const string = html.serialize(value)
localStorage.setItem('content', string)
}
this.setState({ value })
}
render() {
return (
)
}
renderNode = props => {
switch (props.node.type) {
case 'code':
return (
{props.children}
)
case 'paragraph':
return (
{props.children}
)
case 'quote':
return {props.children}
}
}
// Add a `renderMark` method to render marks.
renderMark = props => {
const { mark, attributes } = props
switch (mark.type) {
case 'bold':
return {props.children}
case 'italic':
return {props.children}
case 'underline':
return {props.children}
}
}
}
```
And that's it! When you make any changes in your editor, you should see the updated HTML being saved to Local Storage. And when you refresh the page, those changes should be carried over.