@jswork/next-slate-serialize
v1.0.6
Published
Serializing/deserializing html/text for slate.
Downloads
12
Maintainers
Readme
next-slate-serialize
Serializing/deserializing html/text for slate.
installation
npm install -S @jswork/next-slate-serialize
apis
| api | params | description | | --- | ------------------ | ------------------------ | | parse | inNodes, inOptions | Transform nodes to html. |
usage
import NxSlateSerialize from '@jswork/next-slate-serialize';
// apis
const nodes = [
{
children: [
{
type: 'paragraph',
children: [
{ text: 'An opening paragraph with a ' },
{
type: 'link',
url: 'https://example.com',
children: [{ text: 'link' }]
},
{ text: ' in it.' }
]
},
{
type: 'quote',
children: [{ text: 'A wise quote.' }]
},
{
type: 'paragraph',
children: [{ text: 'A closing paragraph!' }]
}
]
}
];
const options = {
process: (node, children) => {
if (!children) return node.text;
switch (node.type) {
case 'quote':
return `<blockquote><p>${children}</p></blockquote>`;
case 'paragraph':
return `<p>${children}</p>`;
case 'link':
return `<a href="${node.url}">${children}</a>`;
default:
return children;
}
},
joined: ''
};
const html = NxSlateSerialize.parse(nodes, options);
// <p>An opening paragraph with a <a href="https://example.com">link</a> in it.</p><blockquote><p>A wise quote.</p></blockquote><p>A closing paragraph!</p>
resources
- https://docs.slatejs.org/concepts/09-serializing
license
Code released under the MIT license.